Search code examples
junitjunit4junit3

A query on Junit test methods


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());
  }

I have difficulty in understanding fail in Junit . Could anybody please tell me what is the use of fail in the above method ?? ( I want to know what it is responsible to do there )

And typically if i want to add this below line also in the above code . how could i add

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

Please help me .


Solution

  • The fail("sample failure"); -statement in the first case will cause the test to be reported as failed with reason "sample failure" when the statement is run. No idea why it's placed as first statement in the test case, as it will cause the test to fail immediately and the rest of the statements are never executed. As for the second case, simply returning from the method will cause the test to pass.