void keywords
This section explains how to declare and call a void method.
The following example declares a method called printGrade and calls it to print the given score.
Example
public class TestVoidMethod { public static void main(String[] args) { printGrade(78.5); } public static void printGrade(double score) { if (score >= 90.0) { ('A'); } else if (score >= 80.0) { ('B'); } else if (score >= 70.0) { ('C'); } else if (score >= 60.0) { ('D'); } else { ('F'); } } }
The above example compilation and operation results are as follows:
C
Here the printGrade method is a void type method, which does not return a value.
A void method call must be a statement. Therefore, it is called in statement form on the third line of the main method. Just like any statement that ends with a semicolon.
Methods for single-testing void type
There are many void-type methods in the Java Service layer, such as save* and update*. These methods are just updated and will not have a return value. The single test cannot be written based on the return value of the method, so they can only use special methods;
This method environment: Mockito, testng
Methods tested:
VOID method to be tested
@Override public void updateRuleName(Long ruleId, String newRuleName, Long ucId) { (ruleId, "Rule ID cannot be Null"); (newRuleName, "The rule name cannot be Null"); (ucId, "The operator's UCID cannot be Null"); String cleanNewRuleName = (newRuleName); if ((cleanNewRuleName)) { throw new IllegalArgumentException("The new rule name cannot be empty"); } // Query the rule object Rule rule = queryRuleById(ruleId); if (null == rule) { throw new IllegalDataException("No rule found"); } (ruleId); (cleanNewRuleName); (ucId); (new Date()); (rule); }
Test method:
Test of method return by void
@Test public void testUpdateRuleName() { Long ruleId = 1L; String newRuleName = "newRuleName"; Long ucId = 123L; List<Rule> rules = new ArrayList<Rule>(); Rule rule = new Rule(); ((byte) DBValueSetting.RULE_STATUS_TAKE_EFFECT); (rule); // Query the rule object Map<String, Object> params = new HashMap<String, Object>(); ("ruleId", ruleId); ((params)).thenReturn(rules); (new Answer<Object>() { public Object answer(InvocationOnMock invocation) { // Breakpoint 2: This is followed by Rule rule = (Rule) ()[0]; (().equals("newRuleName")); return null; } }).when(ruleDao).updateSelective(()); // Breakpoint 1: Execute here first (ruleId, newRuleName, ucId); }
As shown in the comments, if two breakpoints are added, the last call line will be executed first during the execution process. During the execution of endpoint 1, the stub of endpoint 2 will be executed. At this time, the entry parameter of the method execution can be obtained at breakpoint 2, and the entry parameter can be checked assert to achieve the purpose;
new Answer is an interface, which has only one method, which is used to set the proxy execution portal for method calls.
DoAnswer implementation
public interface Answer<T> { /** * @param invocation the invocation on the mock. * * @return the value to be returned * * @throws Throwable the throwable to be thrown */ T answer(InvocationOnMock invocation) throws Throwable; }
When the code is executed to "(rule);", an interceptor called for the mock object will be triggered. In the interceptor, a dynamic proxy will be created. The invocation of the dynamic proxy is the method covered in new answer;
Using two methods: intercept and proxy, the setting and obtaining of the incoming and outgoing parameters of the mock object method is realized. In this way, the execution class calls within the VOID method can be checked.