Table of contents
1.
Introduction 
2.
Executing JUnit tests
2.1.
Maven Dependencies
2.2.
Creating a Class
2.3.
Creating a Test Case Class 
2.4.
Creating a Test Runner case class
3.
FAQs
4.
Key Takeaways
Last Updated: Mar 27, 2024

JUnit Executing Tests

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

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;
   }   
}
You can also try this code with Online Java Compiler
Run Code

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());
   }
}
You can also try this code with Online Java Compiler
Run Code


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());
   }
} 
You can also try this code with Online Java Compiler
Run Code


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.

FAQs

  1. 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.
     
  2. What does @Test annotation do?
    The @Test annotation indicates that the method or class is a test block, and the code inside it is run to test the code.
     
  3. What do Maven Dependencies do?
    Maven will download dependencies from a remote repository and cache them in the local repository if they are not present in our local repository.
     
  4. How is JUnit testing done?
    JUnit testing can be done in two ways; manual testing and automated testing. Executing the tests manually without using any tools is known as manual testing. Using automated tool support to execute the test cases is known as automation testing. 
     
  5. 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.

Key Takeaways

We discussed JUnit tests and how to represent a test in JUnit. Then we learned how to create the test classes using @Test annotation to execute the test cases.

Hey Ninjas! Don’t stop here; check out Coding Ninjas for more unique courses and guided paths. Also, try Coding Ninjas Studio for more exciting articles, interview experiences, and fantastic Data Structures and Algorithms problems.

Happy Learning!

Live masterclass