Java Reference
In-Depth Information
// 7 > 5
assertTrue(myIntC > myIntA);
}
@Test
public void testAssertFalse() {
// myArrayA.length == 3, 3 != 4
assertFalse(myArrayA.length == 4);
}
@Test
public void testAssertNull() {
// myObject has not been intialized
assertNull(myObject);
}
@Test
public void testAssertNotNull() {
String newString = "Hello";
// newString is intialized
assertNotNull(newString);
}
@Test
public void testAssertSame() {
myObject = new Object();
Object pointerA = myObject;
Object pointerB = myObject;
// both pointerA and pointerB reference myObject
assertSame(pointerA,pointerB);
}
@Test
public void testAssertNotSame() {
myObject = new Object();
Object pointerA = new Object();
Object pointerB = myObject;
// pointerA is a new Object,
// pointerB references myObject
assertNotSame(pointerA,pointerB);
}
}
Once your test methods are written, you can run the test class the same way you run a normal pro-
gram. You can also specify “Run as JUnit Test” if Eclipse does not automatically recognize it as a
JUnit test and run it appropriately. The JUnit panel will take the place of the Navigator, and you
will see a report of how many tests have been run, the number of errors, and the number of fail-
ures. If there is a failure, you can also see more information about the failure in the Failure Trace.
In Figure 6-18, you can see a comparison of two runs: one with a failure and the other without any
failures. In the failure case, the test method that resulted in failure— testAssertArrayEquals —is
highlighted. Figure 6-18 also shows why it failed: element[1] in the first array was 1 , but in the
second array it was 2 .
Search WWH ::




Custom Search