In this post, we will be discussing Junit, its importance and testing your codes using Junit. Let’s get started with the basics first.
What is Junit?
According to Wikipedia, JUnit is a Unit testing framework for Java programming language. JUnit has been important in the development of test-driven development, and is one of a family of Unit testing frameworks, which is collectively known as xUnit, that originated with SUnit.
JUnit is linked as a JAR at compile-time; the framework resides under package junit.framework for JUnit 3.8 and earlier, and under package org.junit for JUnit 4 and later.
Installing Junit
Installation of Junit is very simple, you just need to download the two jars and add them to your project build path. If you are using Maven, then you can add the dependency of Junit in your pom.xml file.
You can download the jar files from the below links:
If you are using Maven, you can include the below dependency
1 2 3 4 5 6 |
<dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> |
Note:
Juni4.12 is the latest version at the time of writing this post.
With this, we can clarify that Junit is a Unit testing framework for testing Java codes.
Now, let’s see how it works and how to test the codes using Junit.
Junit uses annotations to classify the methods tested. Here is a list of annotations that are present in Junit.
Annotations:
Assert Statements:
Junit provides Assert statements, which starts with the text assert and evaluates the actual results and the expected results of your test case. Below is a list of assert statements provided by Junit.
Now, let’s looks at ways to test your Java codes using Junit.
Let’s begin by writing a simple method in Java, to add two numbers. The code for this is as shown below.
1 2 3 4 5 6 7 8 9 |
public class Addition { public static int findsum(int a,int b){ int sum=0; sum = a+b; return sum; } } |
Now, let’s add the two jar files which we have downloaded for installation, into your build path. If you do not add hamcrest core-1.3.jar with junit4, you will get an Intialization Error message as shown below.
Therefore, you need to add both the jars to run the test cases without any error.
Now, to write a test case for this in Eclipse, Right click on the class –> New –> Junit Test case.
Then, give a class name of your choice and a default method will be created with the annotation @Test in that class as shown below.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
import static org.junit.Assert.*; import org.junit.Test; public class AdditionTest { @Test public void test() { fail("Not yet implemented"); } } |
Now, in this method, you can remove the fail Assertion and can write your own assert statements with expected output and some random inputs. There are many types of assert statements that you can include in the method as your test case. Each assert method will become a test case after running and will give you the expected and the actual results.
If both the expected and the actual results matches, then the test case is successful. Otherwise, it is a failure.
You can refer to the below screenshot for the sample list of assert statements.
Now, we will write a simple assertEquals statement with some expected output and inputs, and check it.
Below are my test cases for the Addition method.
1 2 3 4 |
assertEquals(4,Addition.findsum(2, 2)); assertEquals(4,Addition.findsum(3, 2)); assertEquals(4,Addition.findsum(3, 1)); assertEquals(4,Addition.findsum(1, 2)); |
Now to run it, Right click –> Run As –>Junit Test
In the above 4 assert statements, I have given two of them wrongly. So, the 2nd and 4th statements’ expected results will not match with the executed results. Now let us see what Junit says.
You can see the result in the below screenshot.
In the Failures, you can see the number of failure test cases and in the Failure Trace you can see what is the failure. In the above screenshot, it has asserted correctly for the test case 2 i.e., expected 4 but the executed 5 does not match, so it is a failure.
However, it does not specify the 4th test case. The reason is Junit stops testing after the first failure it encounters.
Why does JUnit only report the first failure in a single test?
(Submitted by: J. B. Rainsberger)
Reporting multiple failures in a single test is generally a sign that the test does too much, compared to what a Unit test ought to do. Usually, this means either that the test is really a functional/acceptance/customer test or, if it is a Unit test, then it is too big of a Unit test.
Designed to work best with a number of small tests, JUnit executes each test within a separate instance of the test class and reports failure on each test. Shared setup code is most natural when sharing between tests. This is a design decision that permeates JUnit, and when you decide to report multiple failures per test, you begin to fight against JUnit. This is highly not recommended!
Long tests are a design smell that indicates the likelihood of a design problem. In this case, Kent Beck is fond of saying that “there is an opportunity to learn something about your design.”
Finally, note that a single test with multiple assertions is isomorphic to a test case with multiple tests.
You can refer here for the FAQs on Junit.
Let’s remove the failure test cases and check how it reports.
In the above screenshot, you can see that it does not specify any failures and has returned with a green symbol.
This is how we can test our Java codes using Junit in simple terms. For more details on Junit, you can visit www.junit.org.
There are many more frameworks for testing Java codes, few of them are listed below.
1. Aqruillian
2. Jtest
3. The Grinder
4. TestNG
5. Jwalk
6. Mockito
7. Powermock
Amongst them, Junit is the most famous framework for testing Java code. Let’s see why.
Why is Junit so famous?
-
Open-source framework.
-
Easy for developers to test codes at the time of development itself.
-
Can write more number of test cases in very less time.
-
Easy to understand.
- Can be integrated with any IDE.
We hope this post had been helpful in understanding Junit and using it to test your Java codes. In case of any queries, feel free to comment below and we will get back to you at the earliest.
Keep visiting our site www.acadgild.com for more updates on Big Data and other technologies.
Leave a Reply