This part explains how to write and run a simple JUnit 4 test case in Eclipse.
Create a new Java project called
com.packtpub.e4.junit.example
.Create a class called
MathUtil
incom.packtpub.e4.junit.example
.Create a
public static
method calledisOdd
that takes anint
and returns aboolean
if it is an odd number (usingvalue % 2 == 1
).Create a new class called
MathUtilTest
in a packagecom.packtpub.e4.junit.example
.Create a method called
testOdd
with an annotation@Test
, which is the way JUnit 4 signifies that this method is a test case.Click on the quick-fix to Add JUnit 4 library to the build path, or edit the build path manually to point to Eclipse's
plugins/org.junit_4.*.jar
.Implement the
testOdd
method as follows:assertTrue(MathUtil.isOdd(3)); assertFalse(MathUtil.isOdd(4));
Add an
import static
oforg.junit.Assert.*
to fix the compiler errors.Right-click on the project and choose Run As | JUnit Test, and the JUnit test view should be...