Junit 使用PwerMock模拟ArrayList
我试图使用Powermock模拟arraylist,如下所示 MockDao类 当我运行测试用例时,它抛出了错误,因为,我认为对列表的模拟是不正确的Junit 使用PwerMock模拟ArrayList,junit,powermock,Junit,Powermock,我试图使用Powermock模拟arraylist,如下所示 MockDao类 当我运行测试用例时,它抛出了错误,因为,我认为对列表的模拟是不正确的 有人能告诉我如何运行这个测试场景吗?…根据您的评论,测试失败了。(断言错误是JUnit表示测试失败的方式。) 如果使用,则可以获得更好的错误消息。因此,您必须更改代码的最后两行: assertThat(result.getEntity(), hasToString(containsString("\"result\": \"true\"")));
有人能告诉我如何运行这个测试场景吗?…根据您的评论,测试失败了。(断言错误是JUnit表示测试失败的方式。) 如果使用,则可以获得更好的错误消息。因此,您必须更改代码的最后两行:
assertThat(result.getEntity(), hasToString(containsString("\"result\": \"true\"")));
为org.hamcrest.matcherasert.assertThat
和org.hamcrest.Matchers.
添加一些静态导入
新的错误消息可能有助于您查找错误。您的模拟似乎没有问题 JUnit未通过测试,因为该行
assertEquals(true,output.contains("\"result\": \"true\""));
失败:这意味着您的字符串输出
不包含文本“结果”:“true”
也许,找出错误的一种方法是在调用assertEquals()
之前打印输出值,或者使用调试器查看输出值
作为旁注,
assertEquals(true,)
非常详细,您可以使用assertTrue()
来代替。请添加错误的堆栈跟踪。@StefanBirkner我得到了以下错误跟踪,java.lang.AssertionError:预期:但是是:在org.junit.Assert.fail(Assert.java:88)在org.junit.Assert.failNotEquals(Assert.java:743)位于org.junit.Assert.assertEquals(Assert.java:118)位于org.junit.Assert.assertEquals(Assert.java:144)这个标题似乎有点误导,因为我在任何地方都找不到模拟列表的定义。您能澄清一下您试图实现的目标吗,因为我怀疑您可能试图模拟以下调用:UtilityFunctions.processReportSchedule(scheduleId、jobId、new DailyReceiptsAndExceptionsExport(inputDto),resultsList,null,null);
@Test
public void testGetOutboundAvgCubeAndWeightUtilization_4()
throws Exception {
String dc = "5854";
String asn = "*";
String sort = "SKU";
boolean isExport = false;
String fileType = "";
BigDecimal scheduleId = new BigDecimal(100);
BigDecimal jobId = new BigDecimal(100);
DailyReceiptsAndExceptionsDetailsInputDto inputDto = new DailyReceiptsAndExceptionsDetailsInputDto ();
inputDto.setAsn(asn);
inputDto.setDc(dc);
inputDto.setSortBy(sort);
inputDto.setFileType(fileType);
inputDto.setExport(isExport);
DailyReceiptsAndExceptionsDetailsMockDAO.mockgetDailyExceptions(inputDto, scheduleId, jobId);
Response result = DailyReceiptsAndExceptionsDetailsService.getOutboundAvgCubeAndWeightUtilization(dc, asn, sort, isExport, fileType, scheduleId, jobId);
String output = result.getEntity().toString();
assertEquals(true,output.contains("\"result\": \"true\""));
}
assertThat(result.getEntity(), hasToString(containsString("\"result\": \"true\"")));
assertEquals(true,output.contains("\"result\": \"true\""));