Java method for single measurement of void type

  • 2020-05-27 05:42:01
  • OfStack


When we study Java, the teacher or the 1-like book says there are eight basic types of Java. They are: byte, int, short, long, float, double, char, boolean. But this morning when I was reading Java's bible, Thinking in Java, I noticed that the author had put void in when he was explaining the data types. So we have nine. Baidu 1, some books are also written Java has 9 basic types.

There are many methods of void type in Sevice layer of Java, such as save* and update*. Such methods only make some updates without any return value. The single test cannot be written according to the return value of the method, but only special methods can be used.

This method environment: Mockito, testng

Methods tested:

The VOID method Java that you want to test

 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.setUpdateTime(new Date());

Test methods:

void returns a method that tests Java

 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);
 //  Query rule object 
 Map<String, Object> params = new HashMap<String, Object>();
 params.put("ruleId", ruleId);
 Mockito.doAnswer(new Answer<Object>() {
  public Object answer(InvocationOnMock invocation) {
  //  The breakpoint 2 : this is followed by execution 
  Rule rule = (Rule) invocation.getArguments()[0];
  return null;
 //  The breakpoint 1 : so much for execution 
 ruleService.updateRuleName(ruleId, newRuleName, ucId);

As shown in the comments, if two breakpoints are added, the last call line will be executed first during execution. During the execution of endpoint 1, stub of endpoint 2 will be executed. At this time, input parameters of method execution can be obtained at breakpoint 2, and Assert verification of input parameters can be performed to achieve the purpose.

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

doAnswer implementation Java

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); In the interceptor, a dynamic proxy will be created. invocation of the dynamic proxy is the method overridden in new Answer.

Two methods, interception and proxy, are used to set and obtain the input and output parameters of the mock object method. In this way, the implementation of class calls inside the VOID method can be verified.


The above is the whole content of this article, I hope the content of this article to your study or work can bring 1 definite help, if you have questions you can leave a message to communicate.

Related articles: