Java怎样单测void类型的⽅法?
Java的Sevice层会有很多void类型的⽅法,⽐如save*、update*,这类⽅法只是做⼀些更新,不会有返回值,其单测不能根据⽅法的返回值来编写,只能采⽤特殊⽅法;
本⽅法环境:Mockito、testng
被测试的⽅法:
想要被测试的VOID⽅法
Java
@Override public void updateRuleName(Long ruleId, String newRuleName, Long ucId) { Null(ruleId, "规则ID不能为Null"); Null(newRuleName, "规则名称不能为Null"); Null(ucId, "操作⼈的UCID不能为Null"); String cleanNewRuleName = im(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.setUpda
teTime(new Date()); ruleDao.updateSelective(rule); }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24@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 ) ;
}
测试的⽅法:
void返回的⽅法测试
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) Arguments()[0];
Assert.RuleName().equals("newRuleName")); return null; } }).when(ruleDao).updateSelective(Mockito.any(Rule.class)); // 断点1:先执⾏到这⾥ruleService.updateRuleName(ruleId, newRuleName, ucId); }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28@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 ;
java怎么编写
}
} ) . when ( ruleDao ) . updateSelective ( Mockito . any ( Rule . class ) ) ;
// 断点1:先执⾏到这⾥
ruleService . updateRuleName ( ruleId , newRuleName , ucId ) ;
}
如注释所⽰,如果加了两个断点的话,执⾏的过程中,会先执⾏最后的调⽤⾏,端点1执⾏的过程中,
会执⾏到端点2的stub,这时候在断点2可以获取到⽅法执⾏的⼊参,对⼊参进⾏Assert校验,即可实现⽬的;
new Anwer是个接⼝,其中只有⼀个⽅法,⽤于设置⽅法调⽤的代理执⾏⼊⼝
doAnswer的实现
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; }
1 2 3 4 5 6 7 8 9 10public 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 ; }
当代码执⾏到“ruleDao.updateSelective(rule);”的时候,会触发针对mock对象调⽤的,在中,会创建⼀个动态代理,动态代理的invocation就是new Answer中覆盖的⽅法;
使⽤拦截、代理两种⽅法,实现了对mock对象⽅法的⼊参、出参的设定和获取,使⽤这种⽅式,就可以校验VOID⽅法内部的执⾏类调⽤的情况;

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。