Table of contents
1.
Introduction
2.
JUnit Test cases
3.
Annotations
4.
Frequently Asked Questions
4.1.
What is JUnit Testing?
4.2.
What are JUnit tests?
4.3.
What does @Test annotation do?
4.4.
Can we give the test annotations to classes?
4.5.
How do I run a JUnit test?
5.
Conclusion
Last Updated: Mar 27, 2024
Easy

JUnit Basic Test cases and Syntax

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

Introduction

Testing checks 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 application's behavior. The entire code is divided into a few parts based on different properties. These units are called Tests. 

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 Test cases

JUnit provides essential features like

  1. Fixtures
  2. Test suites
  3. Test runners
  4. JUnit classes

The syntax of the JUnit test methods is as follows.

Public class JUnitSyntax {
   @Annotation // Any annotation according to testing requirements
   Public void method() { 
       // Method body
   }
}
You can also try this code with Online Java Compiler
Run Code

The access specifiers like public, private, etc., can be given according to the code requirements, and the tester can choose any return type among all the existing types. 
Let's take a look at basic test cases with code now.

import org.junit.After;             
import org.junit.Before;                     
import org.junit.Test;        

public class JunitAnnotations {    

    @Test        
    public void test() {                  
        System.out.println("Inside @Test annotation method")            
    }                        

    @After        
    public void after() {                            
        System.out.println("Inside @After annotation method");    
    }    

    @Before        
    public void before() {                    
        System.out.println("Inside @Before annotation method");                    
    }    
}
You can also try this code with Online Java Compiler
Run Code

We used the annotations @Test, @After, @Before in the above code to create the test methods. It gives us the following output. 

Output

Inside @Before annotation method 
Inside @Test annotation method 
Inside @After annotation method

  • The test method before() with @Before annotation gets executed before all the other methods.
  • The method test() with @Test gets executed and prints the output.
  • After all the methods are executed, the @After annotation executes the method after().

Annotations

We must know the annotations and how they work to write the test cases for our application. The most used annotations are listed below, along with their descriptions

Annotations Description
@Test Represents the method or class as a test block, also accepts parameters.
@Before The method with this annotation gets executed before all the other tests.
@After The method with this annotation gets executed after all the other tests are executed.
@Ignore It is used to ignore a few test statements during execution.
@Disabled Used to disable the tests from execution, but the corresponding reports of the tests are still generated.

Frequently Asked Questions

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. 

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. It helps the JVM recognize the block as a test block and perform the testing on that particular method.

Can we give the test annotations to classes?

Yes, we can specify any block as a test block using the test annotations. It includes classes, methods, etc. 

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.

Conclusion

In this article, we have discussed JUnit, its syntax and learned to create a few tests using the annotations. 
Hey Ninjas! We hope this blog helped you enhance your knowledge of JUnit and testing. If you want to learn more, 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. Do upvote our blog to help other ninjas grow.

Check out JUnit Interview Questions here.

Happy Coding!

Live masterclass