关于Java中void方法的详细解析
今天有位朋友遇到一个在Java中void方法,卡了许久,我也没有第一时间看出问题,现在把问题梳理一下,并给出解决方案,下面一起进入关于Java中void方法的详细解析了解一下吧!
示例
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');
}
}
}
以上实例编译运行结果如下:
C
这里printGrade方法是一个void类型方法,它不返回值。
一个void方法的调用一定是一个语句。 所以,它被在main方法第三行以语句形式调用。就像任何以分号结束的语句一样。
单测void类型的方法
Java的Sevice层会有很多void类型的方法,比如save*、update*,这类方法只是做一些更新,不会有返回值,其单测不能根据方法的返回值来编写,只能采用特殊方法;
本方法环境:Mockito、testng
被测试的方法:
想要被测试的VOID方法
@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返回的方法测试
@Test
public void testUpdateRuleName() {
Long ruleId = 1L;
String newRuleName = "newRuleName";
Long ucId = 123L;
List rules = new ArrayList();
Rule rule = new Rule();
rule.setRuleStatus((byte) DBValueSetting.RULE_STATUS_TAKE_EFFECT);
rules.add(rule);
// 查询规则对象
Map params = new HashMap();
params.put("ruleId", ruleId);
Mockito.when(ruleDao.queryRulesByCondition(params)).thenReturn(rules);
Mockito.doAnswer(new Answer
如注释所示,如果加了两个断点的话,执行的过程中,会先执行最后的调用行,端点1执行的过程中,会执行到端点2的stub,这时候在断点2可以获取到方法执行的入参,对入参进行Assert校验,即可实现目的;
new Anwer是个接口,其中只有一个方法,用于设置方法调用的代理执行入口
doAnswer的实现
public interface Answer{ /** * @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方法内部的执行类调用的情况。
以上就是关于Java中void方法的详细解析了,想必都了解了吧,更多相关内容请继续关注爱站技术频道。
