JUnit 4 Tutorial: Eclipse Luna plugin - 2020
Create a project "JUnitTest". Now we can write our first test. We implement the test in a subclass of TestCase
. We can do so either using the standard Class wizard or the specialized Test Case wizard.
- Open the New wizard (File > New > JUnit Test Case).
- Select New JUnit 4 test:
- We will see a warning message asking us to add the
junit
library to the build path. Use the Clickhere
link to add thejunit
library automatically. - Click "Next" to select methods for which test method stubs should be created.
- Click "Finish" to create the test class.
- At this point, if we missed the "step 3. add the junit library to the build path", we may get the following:
- At OK, Eclipse will give us the following code (
ConcatenationTest.java
):import static org.junit.Assert.*; import org.junit.Test; public class ConcatenationTest { @Test public void testConcat() { fail("Not yet implemented"); } }
Unit tests are implemented as classes with test methods. Each test method usually tests a single method of the target class.
Here is our target class:
public class Concatenation { public String concat(String s1, String s2) { return s1 + s2; } }
We need to complete a test method of the class ConcatenationTest
that Eclipse's template:
import static org.junit.Assert.*; import org.junit.Test; public class ConcatenationTest { @Test public void testConcat() { Concatenation myConcat = new Concatenation(); String result = myConcat.concat("Hello", "World"); assertEquals("HelloWorld", result); } }
The unit test class is an ordinary class, with one method, testConcat()
. Notice how this method is annotated with the JUnit annotation @Test
. This is done to signal to the unit test runner, that this is method represents a unit test, that should be executed. Methods that are not annotated with @Test
are not executed by the test runner.
Inside the testConcat()
method an instance of myConcat
is created. Then it's concat()
method is called with two string values.
Finally, the assertEquals()
method is called. It is this method that does the actual testing. In this method we compare the output of the called method (concat()
) with the expected output. In other words, we compare "HelloWorld" (expected output) with the value returned by the concat()
method, which is kept in the variable result.
If the two values are equal, nothing happens. The assertEquals()
method returns normally. If the two values are not equal, an exception is thrown, and the test method stops executing here.
The assertEquals()
method is a statically imported method, which normally resides in the org.junit.Assert
class. Notice the static import of this class at the top of Concatenation
.
Once we have written our test methods, we want to run our JUnit test case:
A new pane will appear showing the test results for each method. We should see a green bar if all of the tests passed, or a red bar if any of the tests failed. If any tests fail, we can view the details about the failure by clicking on the failed test's name/icon and looking at the details in the pane below.
In our case, we passed the test.
JUnit & Maven Tutorial
Ph.D. / Golden Gate Ave, San Francisco / Seoul National Univ / Carnegie Mellon / UC Berkeley / DevOps / Deep Learning / Visualization