Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Junit 使用PwerMock模拟ArrayList_Junit_Powermock - Fatal编程技术网

Junit 使用PwerMock模拟ArrayList

Junit 使用PwerMock模拟ArrayList,junit,powermock,Junit,Powermock,我试图使用Powermock模拟arraylist,如下所示 MockDao类 当我运行测试用例时,它抛出了错误,因为,我认为对列表的模拟是不正确的 有人能告诉我如何运行这个测试场景吗?…根据您的评论,测试失败了。(断言错误是JUnit表示测试失败的方式。) 如果使用,则可以获得更好的错误消息。因此,您必须更改代码的最后两行: assertThat(result.getEntity(), hasToString(containsString("\"result\": \"true\"")));

我试图使用Powermock模拟arraylist,如下所示

MockDao类 当我运行测试用例时,它抛出了错误,因为,我认为对列表的模拟是不正确的


有人能告诉我如何运行这个测试场景吗?…

根据您的评论,测试失败了。(断言错误是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\""));