Java 测试使用spring服务的方法
我在Java中测试一个方法时遇到问题(我使用jUnit 4),该方法用于测试调用存储库并应引发异常。如果出现问题,该方法如下所示:Java 测试使用spring服务的方法,java,spring,junit,junit4,qa,Java,Spring,Junit,Junit4,Qa,我在Java中测试一个方法时遇到问题(我使用jUnit 4),该方法用于测试调用存储库并应引发异常。如果出现问题,该方法如下所示: //Method located in a Spring service and use a repository to make the query public void updateCustomer(CustomerEntity customer) throws CustomException { try { getDa
//Method located in a Spring service and use a repository to make the query
public void updateCustomer(CustomerEntity customer) throws CustomException {
try {
getDataDao().update(customer);
} catch (Exception e) {
throw new CustomException(e.getMessage());
}
}
问题是,如何让测试用例进入catch块并抛出异常?我正在将客户设置为null,但不起作用
谢谢您的帮助。您需要模拟DAO调用并从那里抛出和异常 您将对@Mock、@InjectMock和framework有更多的了解
您可以使用mockito框架来实现这一点。 @Ed好极了。。!!
Mockito.when(getDataDao().update(any()))
.thenThrow(Exception.class);