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, when I was reading the Java Bible, "Thinking in Java" this morning, I found that the author put void on it when explaining the data type. There are nine kinds of this. I read on Baidu and found that some books also write nine basic types of Java.
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;
The environment of this method: Mockito, testng
Methods tested:
Java
@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 the rule object Rule rule = queryRuleById(ruleId); if (null == rule) { throw new IllegalDataException("No rule found"); } rule.setRuleId(ruleId); rule.setRuleName(cleanNewRuleName); rule.setUpdateUcid(ucId); rule.setUpdateTime(new Date()); ruleDao.updateSelective(rule); }Test method:
Test Java by void return method
@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 the 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 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 method execution entry parameter can be obtained at breakpoint 2, and the incoming parameter can be performed assert verification 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 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); ", 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;
Summarize
The above is the entire content of this article. I hope the content of this article will be of some help to your study or work. If you have any questions, you can leave a message to communicate.