Tutorial on the void method in Java programming

  • 2020-04-01 04:16:01
  • OfStack

Void keyword
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.
The sample


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 operation results of the above examples are as follows:


C

Here the printGrade method is a void type method that does not return a value.
A call to a void method must be a statement. So, it is called as a statement on the third line of the main method. Just like any statement that ends with a semicolon.

A method to single test the type void
Java Sevice layer will have many void type methods, such as save*, update*, this kind of methods just do some updates, will not return value, its single test can not be written according to the return value of the method, 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) {
    Assert.notNull(ruleId, " The rules ID Can't for Null");
    Assert.notNull(newRuleName, " The rule name cannot be Null");
    Assert.notNull(ucId, " Operation of UCID Can't for 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:

Method test returned 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();
    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: this is followed by execution
        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
    ruleService.updateRuleName(ruleId, newRuleName, ucId);
  }

As shown in the comment, if two breakpoints are added, the last call line will be executed first during execution. During the execution of endpoint 1, the stub of endpoint 2 will be executed. At this time, the input parameter of method execution can be obtained from breakpoint 2, and the input parameter can be asserted to achieve the purpose.

New Anwer is an interface with only one method that sets the proxy execution entry for the method call

The realization of the doAnswer


public interface Answer<T> {
  
  T answer(InvocationOnMock invocation) throws Throwable;
}

When the code executes to "ruledao.updateseleles (rule);" In the interceptor, a dynamic proxy is created. The invocation of the dynamic proxy is the method overridden in the new Answer.

Using interception, proxy two methods, to achieve the mock object method input, out of the parameter set and get, using this way, you can verify the VOID method inside the case of the execution class call.


Related articles: