This section explains how to write and run a simple JUnit 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
value, and returns aboolean
value if it is an odd number (usingvalue % 2 == 1
).Create a new class called
MathUtilTest
in the packagecom.packtpub.e4.junit.example
.Create a method called
testOdd()
with an annotation@Test
, which is how JUnit 4 signifies that this method is a test case.Click on the quick-fix saying
Add JUnit 4 library to the build path
, or edit the build path manually to point to Eclipse'splugins/org.junit_4.*.jar
file.Implement the
testOdd()
method as follows:assertTrue(MathUtil.isOdd(3)); assertFalse(MathUtil.isOdd(4));
Add a
static import
toorg.junit.Assert.*
to fix the compile-time errors.Right-click on the project and go to Run As | JUnit Test, and the JUnit...