Introduction
You might have heard many developers speaking about tests often after they code. Have you wondered what they are and why do we need them? Don’t worry, you can learn about them in this article.
Testing is used to check whether the application achieves all the requirements, serves the user with expected functionality and the correct use cases. In other words, testing is done to check the behaviour of the application. The entire code is divided into a few parts based on different properties. These units are called Tests. Let’s explore what the JUnit tests are in this article.
Executing JUnit tests
There are multiple ways to execute the JUnit test.
Maven Dependencies
We need a few dependencies to execute JUnit tests in our local machine. So to download them, we use Maven. Maven will download dependencies for both JUnit 4 and JUnit 5 testing, from a remote repository and cache them in the local repository if they are not present in our local repository.
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-launcher</artifactId>
<version>1.2.0</version>
</dependency>
</dependencies>
// for JUnit 4
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
The code above installs the mentioned versions of JUnit dependencies when added to our code. The latest versions of JUnit4, JUnit5, and the JUnit platform launcher are Maven Central.
Creating a Class
We create a java class to test the code inside it. Let’s create a basic class MessageUtil.java with any code inside it. MessageUtil.java is just a basic class without any testing support yet.
public class MessageUtil {
public static void main() {
String msg = print(); // calling the method print
}
// printing the message
public static String print(){
System.out.println("Coding Ninjas");
return msg;
}
}Creating a Test Case Class
We have to create a test class to test the code inside it. Let’s create a class TestClassJunit.java with a testing method testPrintmsg() inside the class and add @Test annotation to the method. We import the annotation and assert methods from JUnit package as shown in the code. This implements the code inside the blocks and tests it.
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class TestClassJunit {
String msg = "Coding Ninjas";
@Test
public void testPrintmsg() {
assertEquals(msg,MessageUtil.printmsg());
}
}
In the above code, we are checking if the two objects or strings are equal using the assertEquals method. If they are equal, the tests will be passed successfully and a message "Coding Ninjas" is printed, or else it shows that the test cases are failed.
Creating a Test Runner case class
Let's create a test runner class named testRunner.java to execute test cases. It imports the test class to the test run class and prints the message given.
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 outcome = JUnitCore.runClasses(TestJunit.class);
for (Failure failing : outcome.getFailures()) {
System.out.println(failing.toString());
}
System.out.println(outcome.wasSuccessful());
}
}
In the above code, the TestRunner class executes the test cases in test file and if they are succesful it gives a result the test cases are performed succesfully or else it displays the test cases are failed and specifies the errors in every case.
Finally, we completed writing Test Case and Test Runner cases using Java.
Check out JUnit Interview Questions here.




