-
In
Package Explorerview, click to select the project where you want to add a test -
Use
File> Newto create a new source folder calledtest -
Click to select the class that you want to test (Known as Class Under Test or
CUT) -
Use
File> Newto create a newJUnit Test Caseas follows:-
Choose
New JUnit 4 test(not JUnit 3) -
Source folder: The
testfolder that you created above. -
Package: Same as the CUT
-
Name: Your test class should be named as per your CUT followed by a suffix of
TestegDogTest -
Probably tick to create
setUp()method -
Press
Nextand optionally tick to generate test methods
-
-
Then run the test class by right-clicking it in the
Package Explorerview and choosingRun As > JUnit Test -
The code in your test methods should be as simple as possible (complexity requires tests and we don’t want recursive hell!) The three essential elements are
SetupInvokeandVerifyeg:Dog myDog= new Dog(); // Setup myDog.eat( 10 ); // Invoke assertEquals( 17, myDog.getWeight() ); // Verify
-
As an alternative to the Eclipse wizard, code your test method manually, such as:
@Test public void testNewPerson_negativeAge_throwsException() { ... } -
Where there is common setup code we can remove it from the test methods and place it in the @Before method, which is called before every test method gets invoked. Similarly the static @BeforeClass method which is called just once, before any of the test methods are invoked.
-
Where the
verifylogic becomes complex, this can be simplified by using the Hamcrest library eg:
assertThat( i, anyOf( greaterThan(j), lessThan(k) ) );
Hamcrest requires static imports, which some IDEs cannot generate automatically, eg the above code requires:import static org.junit.Assert.assertThat; import static org.hamcrest.CoreMatchers.anyOf; import static org.hamcrest.number.OrderingComparison.greaterThan; import static org.hamcrest.number.OrderingComparison.lessThan;
-
If you need to check that a method threw an exception, research the optional parameters for the
@Testannotation, such as:@Test(expected=IllegalArgumentException.class)
Happy hacking!
Mike [dot] Burton [at] MycoSystems.co.uk