JUnit is an open source testing framework which is used to write and run repeatable automated tests, so that we can be ensured that our code works as expected. JUnit is widely used in industry and can be used as stand alone Java program (from the command line) or within an IDE such as Eclipse.
JUnit provides:
• Assertions for testing expected results.
• Test features for sharing common test data.
• Test suites for easily organizing and running tests.
• Graphical and textual test runners. JUnit is used to test:
• an entire object
• part of an object - a method or some interacting methods
• interaction between several objects
JUnit Simple Example using Eclipse
In this section we will see a simple JUnit example. First we will present the class we would like to test:
Calculate.java package com.javacodegeeks.junit;
public class Calculate { public int sum(int var1, int var2)
{
System.out.println("Adding values: " + var1 + " + " + var2); return var1 + var2;
}
}
In the above source code, we can notice that the class has one public method named sum(), which gets as inputs two integers, adds them and returns the result. So, we will test this method. For this purpose, we will create another class including methods that will test each one of the methods of the previous class (in this case, we have only one method to be tested). This is the most JUnit Tutorial 3 / 26 common way of usage. Of course, if a method is very complex and extended, we can have more than one test methods for this complex method. The details of creating test cases will be presented in the next sections. Below, there is the code of the class named CalculateTest.java, which has the role of our test class:
CalculateTest.java package com.javacodegeeks.junit; import static org.junit.Assert.*;
import org.junit.Test; public class CalculateTest { Calculate calculation = new Calculate(); int sum = calculation.sum(2, 5); int testSum = 7;
@Test public void testSum() { System.out.println("@Test sum(): " + sum + " = " + testSum); assertEquals(sum, testSum); } }
Let’s explain the above code. Firstly, we can see that there is a @Test annotation above the testSum() method. This annotation indicates that the public void method to which it is attached can be run as a test case. Hence, the testSum() method is the method that will test the sum() public method. We can also observe a method called assertEquals(sum, testsum). The method assertEquals ([String message], object expected, object actual) takes as inputs two objects and asserts that the two objects are equal. If we run the test class, by right-clicking in the test class and select Run As → Junit Test, the program output will look like that: Adding values: 2 + 5 @Test sum(): 7 = 7 To see the actual result of a JUnit test, Eclipse IDE provides a JUnit window which shows the results of the tests. In this case where the test succeeds, the JUnit window does not show any errors or failures, as we can see in the image below: JUnit Tutorial 4 / 26 Figure 2.1: JUnit window after a successful test. Now, if we change this line of code: int testSum = 10; so that the integers to be tested are not equal, the output will be: Adding values: 2 + 5 @Test sum(): 7 = 10 And in the JUnit window, an error will appear and this message will be displayed: java.lang.AssertionError: expected: but was: at com.javacodegeeks.junit.CalculateTest.testSum(CalculateTest.java:16)