Home > Java > javaTutorial > Detailed explanation of the method of single testing void type in Java

Detailed explanation of the method of single testing void type in Java

高洛峰
Release: 2017-01-19 14:32:43
Original
1587 people have browsed it

Preface

When we were learning Java, teachers or general books wrote that there are eight basic types of Java. They are: byte, int, short, long, float, double, char, boolean. However, this morning when I was reading the Java bible - "Thinking in Java", I found that the author also put void when explaining the data types. So there are nine kinds. After searching on Baidu, some books also said that Java has nine basic types.

Java's Service layer will have many void type methods, such as save* and update*. These methods only do some updates and will not have a return value. Its single test cannot be written based on the return value of the method. , only special methods can be used;

This method environment: Mockito, testng

Tested method:

The VOID method you want to test Java

@Override
 public void updateRuleName(Long ruleId, String newRuleName, Long ucId) {
 Assert.notNull(ruleId, "规则ID不能为Null");
 Assert.notNull(newRuleName, "规则名称不能为Null");
 Assert.notNull(ucId, "操作人的UCID不能为Null");
  
 String cleanNewRuleName = StringUtils.trim(newRuleName);
 if (StringUtils.isBlank(cleanNewRuleName)) {
  throw new IllegalArgumentException("新的规则名称不能为空");
 }
  
 // 查询规则对象
 Rule rule = queryRuleById(ruleId);
 if (null == rule) {
  throw new IllegalDataException("没有查到该规则");
 }
  
 rule.setRuleId(ruleId);
 rule.setRuleName(cleanNewRuleName);
 rule.setUpdateUcid(ucId);
 rule.setUpdateTime(new Date());
  
 ruleDao.updateSelective(rule);
 }
Copy after login

Testing method:

void returned method test Java

@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);
 
// 查询规则对象
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) {
 // 断点2:这里随后执行
 Rule rule = (Rule) invocation.getArguments()[0];
 Assert.assertTrue(rule.getRuleName().equals("newRuleName"));
 return null;
 }
}).when(ruleDao).updateSelective(Mockito.any(Rule.class));
 
// 断点1:先执行到这里
ruleService.updateRuleName(ruleId, newRuleName, ucId);
}
Copy after login

As shown in the comments, if two breakpoints are added, the last call will be executed first during the execution process OK, during the execution of endpoint 1, the stub of endpoint 2 will be executed. At this time, the input parameters of method execution can be obtained at breakpoint 2, and the input parameters can be Assert verified 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.

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;
}
Copy after login

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;

Use interception Two methods, proxy and proxy, realize the setting and obtaining of the input and output parameters of the mock object method. Using this method, you can verify the execution class call inside the VOID method;

Summary

The above is the entire content of this article. I hope the content of this article can bring some help to everyone's study or work. If you have any questions, you can leave a message to communicate.

For more detailed explanations of Java single-test void type methods, please pay attention to the PHP Chinese website for related articles!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template