【问题标题】:A query on Junit test methods一个关于junit测试方法的查询
【发布时间】:2011-11-10 17:47:27
【问题描述】:
public void testNullsInName() {
    fail("sample failure");
    Person p = new Person(null, "lastName");
    assertEquals("lastName", p.getFullName());
    p = new Person("Tanner", null);
    assertEquals("Tanner ?", p.getFullName());
  }

我很难理解 Junit 中的失败。 谁能告诉我上述方法中失败的用途是什么? (我想知道它在那里负责做什么)

通常,如果我想在上面的代码中添加以下行。我怎么能添加

Person p = new Person(null, "lastName"); // After this statement 

if(p==null)
{
// then dont proceed further to the further execution 
// Show the Junit Test case as PASS .
}

请帮帮我。

【问题讨论】:

    标签: junit junit4 junit3


    【解决方案1】:

    第一种情况下的fail("sample failure"); 语句将导致在运行语句时报告测试失败,原因是“样本失败”。不知道为什么将它作为测试用例中的第一个语句放置,因为它会导致测试立即失败并且其余语句永远不会执行。至于第二种情况,只需从方法中returning 即可使测试通过。

    【讨论】:

    • 非常感谢,你拯救了我的一天(实际上是在印度的夜晚),但仍然如此。
    猜你喜欢
    • 2013-11-09
    • 1970-01-01
    • 2021-12-23
    • 1970-01-01
    • 1970-01-01
    • 2017-09-30
    • 1970-01-01
    • 2014-01-07
    • 1970-01-01
    相关资源
    最近更新 更多