SE450: Testing: Junit Assertions [49/63] Previous pageContentsNext page

junit.framework.Assert is a static class.

The basic assertion method is assertTrue, which comes in two forms:

Assert.assertTrue(boolean):void
Assert.assertTrue(String,boolean):void

If the boolean value is false, then the enclosing test will fail.

In the second form, the String is printed when the assertion is false.

public void testA() { ...
  Assert.assertTrue(x.getColor() == Color.RED)
  Assert.assertTrue("Wrong color!", x.getColor() == Color.RED)
}

I prefer the version without the String. The messages from junit are already informative.

If there are multiple assertions in a single test, then the enclosing test fails if any assertion is false.

Previous pageContentsNext page