void keyword
This section explains how to declare and call a void method.
The following example declares a method named printGrade and calls it to print the given grade.
Example
public class TestVoidMethod { public static void main(String[] args) { printGrade(78.5); } public static void printGrade(double score) { if (score >= 90.0) { System.out.println('A'); } else if (score >= 80.0) { System.out.println('B'); } else if (score >= 70.0) { System.out.println('C'); } else if (score >= 60.0) { System.out.println('D'); } else { System.out.println('F'); } }}
The compilation and running results of the above example are as follows:
C
The printGrade method here is a void type method and it does not return a value.
A call to a void method must be a statement. Therefore, it is called as a statement in the third line of the main method. Just like any statement that ends with a semicolon.
Single method to test void type
The Service layer of Java will have many void type methods, such as save* and update*. These methods only do some updates and will not have a return value. The single test cannot be written based on the return value of the method and can only use special methods. ;
Environment for this method: Mockito, testng
Tested methods:
The VOID method you want to test
@Override public void updateRuleName(Long ruleId, String newRuleName, Long ucId) { Assert.notNull(ruleId, "The rule ID cannot be Null"); Assert.notNull(newRuleName, "The rule name cannot be Null"); Assert.notNull( ucId, "The operator's UCID cannot be Null"); String cleanNewRuleName = StringUtils.trim(newRuleName); if (StringUtils.isBlank(cleanNewRuleName)) { throw new IllegalArgumentException("The new rule name cannot be empty"); } // Query rule object Rule rule = queryRuleById(ruleId); if (null = = rule) { throw new IllegalDataException("The rule was not found"); } rule.setRuleId(ruleId); rule.setRuleName(cleanNewRuleName); rule.setUpdateUcid(ucId); rule.setUpdateTime(new Date()); ruleDao.updateSelective(rule); }
Test method:
void return method test
@Test public void testUpdateRuleName() { Long ruleId = 1L; String newRuleName = "newRuleName"; Long ucId = 123L; List<Rule> rules = new ArrayList<Rule>(); Rule rule = new Rule(); rule.setRuleStatus ((byte) DBValueSetting.RULE_STATUS_TAKE_EFFECT); rules.add(rule); // Query rule object Map<String, Object> params = new HashMap<String, Object>(); params.put("ruleId", ruleId); Mockito.when(ruleDao.queryRulesByCondition(params)).thenReturn(rules); Mockito .doAnswer(new Answer<Object>() { public Object answer(InvocationOnMock invocation) { // Breakpoint 2: Rule rule = (Rule) invocation.getArguments()[0]; Assert.assertTrue(rule.getRuleName().equals("newRuleName")); return null; } }).when(ruleDao ).updateSelective(Mockito.any(Rule.class)); // Breakpoint 1: Execute here first ruleService.updateRuleName(ruleId, newRuleName, ucId); }
As shown in the comments, if two breakpoints are added, the last calling line will be executed first during execution. During the execution of endpoint 1, the stub of endpoint 2 will be executed. At this time, it can be obtained at breakpoint 2. Go to the input parameters of method execution and perform Assert verification on the input parameters to achieve the purpose;
new Anwer is an interface with only one method, which is used to set the proxy execution entry for method calls.
Implementation of doAnswer
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 "ruleDao.updateSelective(rule);", the 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 interception and proxy methods, the setting and obtaining of the input and output parameters of the mock object method are realized. Using this method, the execution class call inside the VOID method can be verified.