0
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 .
}

请帮我 。

4

1 回答 1

1

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

于 2011-11-10T18:00:56.673 回答