Java EasyMock TestCase在检查异常时失败
我已经为我的代码编写了一个测试用例,使用EasyMock检查BusinessServiceException。但是测试用例失败了(不是错误)。有人能告诉我为什么会发生这种情况吗?这就是为什么这个测试用例失败的原因 Java代码:Java EasyMock TestCase在检查异常时失败,java,easymock,Java,Easymock,我已经为我的代码编写了一个测试用例,使用EasyMock检查BusinessServiceException。但是测试用例失败了(不是错误)。有人能告诉我为什么会发生这种情况吗?这就是为什么这个测试用例失败的原因 Java代码: public class ListHelper { @Resource(name = "shoppingListService") private ShoppingListService shoppingListService; public
public class ListHelper {
@Resource(name = "shoppingListService")
private ShoppingListService shoppingListService;
public void setShoppingListService(ShoppingListService shoppingListService) {
this.shoppingListService = shoppingListService;
}
Public Map<String, String> getShoppingListCount(String partnerId, String userId){
// Shopping List Section
Map<String, String> shoppingListDetails = null;
try {
shoppingListDetails = shoppingListService.getShoppingListTotal(partnerId, userId);
} catch (BusinessServiceException e) {
}
return shoppingListDetails;
}
- 您的方法不会抛出异常,因为您在空catch块中丢弃了它。
public Map<String, String> getShoppingListCount(String partnerId, String userId) throws BusinessServiceException {
// Shopping List Section
Map<String, String> shoppingListDetails = shoppingListService.getShoppingListTotal(partnerId, userId); // do not catch the possible exception
return shoppingListDetails;
}
@Test(expected= BusinessServiceException.class)
public void testGetShoppingListCountBusinessServiceException() throws BusinessServiceException {
EasyMock.expect(shoppingListService.getShoppingListTotal("p120-90", "2012")).andThrow(new BusinessServiceException("Failure"));
EasyMock.replay(shoppingListService);
// pass the mock!
ListHelper listHelper = new ListHelper(shoppingListService);
try{
listHelper.getShoppingListCount("p120-90", "2012");
}finally{
EasyMock.verify(shoppingListService);
}
}
为什么
listHelper
而不是shoppingListService
?您的测试用例是如何失败的?没有异常?ShoppingListService是一个接口。我正在测试的方法在ListHelper中。在失败跟踪中,它显示“java.lang.AssertionError:Expected Exception:com.优惠券.nextgen.Exception.BusinessServiceException”,您能给我们看更多代码吗?我看不出您在哪里将mock(shoppingListService)传递给测试对象(listHelper)。我会怀疑这样的事情:listHelper=newlistHelper(shoppingListService)代码>嗨,我上传了完整的代码…请检查一下。
@Test(expected= BusinessServiceException.class)
public void testGetShoppingListCountBusinessServiceException() throws BusinessServiceException {
EasyMock.expect(shoppingListService.getShoppingListTotal("p120-90", "2012")).andThrow(new BusinessServiceException("Failure"));
EasyMock.replay(shoppingListService);
// pass the mock!
ListHelper listHelper = new ListHelper(shoppingListService);
try{
listHelper.getShoppingListCount("p120-90", "2012");
}finally{
EasyMock.verify(shoppingListService);
}
}