Introduction
JUnit is an open-source Regression testing framework for Java. A test in JUnit is a method or class used only for testing. These are called Test methods or Tests classes. To define a method as a test method, you can add @Test annotation before it. There are a few other annotations like @After, @Before, @Ignore, etc., in JUnit. Let’s explore what the JUnit test cases are in this article.
JUnit provides essential features like
- Fixtures
- Test suites
- Test runners
- JUnit classes
To write test cases for a class or method, we have to write the code inside the block with a return type other than void. Now, let’s create a class named JavaClassExample and name the file JavaClassExample.java.
public class JavaClassExample {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
System.out.println(add(a,b));
System.out.println(multiply(a,b));
}
public static int add(int a, int b) {
return a+b;
}
public static int multiply(int a, int b) {
return a*b;
}
}
We created a class JavaClassExample in the above code, with two methods add() and multiply(). They return the sum and multiplication of the variables a and b given by the user to the main method, and the result is printed in the main method.
Now let’s create a test class for the class we created.
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
public class JavaTestClassExample {
//test to check addition method
@Test
public void testAddition() {
assertEquals("18", JavaClassExample.add(9,9));
assertNotEquals("14",JavaClassExample.add(9,7));
}
// test to check multiplication method
@Test
public void testMultiplication() {
assertEquals("18", JavaClassExample.add(9,2));
assertNotEquals("15",JavaClassExample.add(5,3));
}
}
In the above code, we create a test class named JavaTestClassExample with two test methods, testAddition and testMultiplication. We made two test cases using assertEquals and assertNotEquals to test the method for a few input values given by the user. assertEquals method checks whether two objects are equal and evaluates to true if they are equal. Otherwise false. Similarly, the assertNotEquals checks whether the objects are not equal.
Now let’s create a testRunner class to execute the tests.
import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;
public class TestRunner {
public static void main(String[] args) {
Result result = JUnitCore.runClasses(JavaTestClassExample.class);
for (Failure failure : result.getFailures()) {
System.out.println(failure.toString());
}
System.out.println(result.wasSuccessful());
}
}
The above code runs the test cases written in the test class file. The method getFailures() gets the reason for the failure of the tests and prints it by converting it into a string. If the tests are executed successfully if prints the tests are executed successfully.
You can either use an IDE to execute the Java tests, which makes coding easier for you, or use the notepad and run the code using the terminal. If you like to run the code in the terminal, run the following command.
javac JavaClassExample.java JavaTestClassExample.java TestRunner.java
The javac command above compiles the code in the java files JavaClassExample.java, JavaTestClassExample.java, and TestRunner.java. Now that the compilation is done, let’s execute the code.
java TestRunner
The java code executes the compiled code gives us the output. You will get the output as true because all the test cases we give will pass.
Check out JUnit Interview Questions here.
FAQs
-
What is JUnit testing?
JUnit is an open-source Regression testing framework for Java. We can create test cases for our code using the JUnit framework.
-
What are JUnit tests?
A test in JUnit is a method or class used only for testing. These are called Test methods or Tests classes. The tests contain the code used for testing the behavior of the application.
-
How do I run a JUnit test?
To run the JUnit tests right, click on the class file in the Script view and select the Run As option. Then select the JUnit Test inside the Run As option. This will execute the class or method that is defined as a test. If you use notepad and terminal, you can use the commands mentioned in this article.
-
Which methods Cannot be tested by the JUnit test class?
The methods declared using the specifier “private” cannot be accessed from other classes in Java. So these methods cannot be tested by the JUnit test classes.
-
Can we re-run failed test cases in JUnit?
Yes, Java allows you to re-run the failed test cases without affecting the current test flow or workflow. JUnit provides us with a class called TestRule that allows you to perform the failed tests again.