【问题标题】:JUnit testing using ContainsAll-assertion使用 ContainsAll-assertion 的 JUnit 测试
【发布时间】:2014-12-13 17:30:37
【问题描述】:

我正在测试我最近为我的第一学期项目编写的预订系统中的搜索功能。我对此进行测试的想法是:我让原始搜索方法返回搜索中找到的客户的数组列表,获取 所有 客户的列表,如果所有客户的列表包含找到的客户的数组列表在搜索中,我断言这是有效的。

注意:我之前创建了一个我知道我会找到的特定条目(希望,对吧?)。

我的问题是当我打电话时-

assertTrue(allCustomers.containsAll(searchResults));

- 我收到一个错误:

junit.framework.AssertionFailedError
at TestPackages.newBookingControllerTest.testOnEnter(newBookingControllerTest.java:98)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:86)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:74)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:211)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:67)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)

谁能详细说明为什么会这样?如果您需要,我可以编辑一些源代码。谢谢!

【问题讨论】:

    标签: mysql junit assertion


    【解决方案1】:

    AssertEquals 有两个参数,所以你可能会看到编译错误。

    你应该使用

    assertTrue(allCustomers.contains(searchResults));//meaning allCustomers indeed contains searchResults
    

     assertEquals(allCustomers.contains(searchResults), true);//Meaning boolean returned by contains api is going to be compared with true
    

    【讨论】:

    • 对不起,阿尔马斯!事实上,我从一开始就使用了 assertTrue。我现在纠正了这一点。感谢您的回复。
    • Nps :) 但是我的回答是否帮助您理解为什么会出现断言错误?
    • 我尝试使用 assertEquals 查看输出。它告诉我我的陈述是错误的:junit.framework.AssertionFailedError: Expected :false Actual :true 这是否意味着我的原始陈述是错误的?
    • 正是这意味着您的数组列表不包含搜索结果。没有你原来的陈述是完美的。看看如果你使用 assertTrue 然后相应地创建一个数据集,这样你的数组列表将包含 searchResults。
    • 我做了一个 for 循环,打印出所有客户 ID 和搜索结果客户 ID,我能够从客户 ID 列表中的搜索结果中找到每个 ID。这不应该意味着它是真的吗?还是我误解了 Contains.all 函数?我的意思是,如果 list1 = {A,B,C} 和 list2 = {A,C}。 list1.containsAll(list2) 不应该是真的吗?
    猜你喜欢
    • 1970-01-01
    • 2010-09-06
    • 2011-09-22
    • 2018-01-19
    • 1970-01-01
    • 2014-06-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多