Trending October 2023 # Various Annotations Of Junit With Examples # Suggested November 2023 # Top 15 Popular |

Trending October 2023 # Various Annotations Of Junit With Examples # Suggested November 2023 # Top 15 Popular

You are reading the article Various Annotations Of Junit With Examples updated in October 2023 on the website We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested November 2023 Various Annotations Of Junit With Examples

Introduction on JUnit Annotations

Web development, programming languages, Software testing & others

JUnit Annotations with Example

Below given are some of the JUnit annotations:

1. @Before

There are scenarios when some test cases or test data need to be created before executing the actual test case. In that case, @Before annotations came into the picture. Annotating any public method with this annotation allows the code/ method to run before every actual @test method. In the case of inheritance, @before methods of the superclass are called first and then the @before methods of the current class.

2. @After

It is just the opposite of @Before annotation. It is used in cases when some actions need to be performed like releasing resources, cleaning up memory, printing anything on the console after the execution of the @test method (after the execution of every test case). One important point that should be noted for @After annotation is that it will execute even when the @test or @before methods throw an exception. For example, in the case of the sub and superclass relationship, the @after method of subclass/ current class is executed before the @after method of the superclass.

3. @BeforeClass 4. @AfterClass

All the resources allocated in the @BeforeClass method need to be released after the execution of all the @test methods of the current class. This deallocation of resources or any important task that is to be done after the execution of the whole class is done by the @AfterClass method. The @AfterClass method runs after all the @test method of the current class is executed in simple terms. It is executed only once. @AfterClass methods are run mandatory even if the @BeforeClass methods throw an exception.

5. @Test

This annotation specifies that the public method under this annotation is a part of the main test case that needs to be executed. The method under the @test defines the test case as passed or failed depending on whether any exception/ error occurs on executing it.

It can also be used in 2 ways:

@Test(timeout= 500): It takes the parameter as timeout, which accepts milliseconds’ values. It considers the test case as failed when it takes longer than the expected time to execute and pass when it executes successfully within the specified time limit.

@Test(expected= Exception.class): There are situations when we want some methods to throw a specific exception. The test case fails if the method does not throw any exception or the above-mentioned exception.

6. @Ignore

There are scenarios when we want to ignore a few test cases and don’t want to run them. @Ignore helps for the same. Methods under the @Ignore annotations are not executed and are ignored during the execution of the code. Even in TestNG reports, test cases are ignored, and the number of test cases passed.


package demo; import org.junit.*; public class JUnitAnnotationTest { @BeforeClass public static void bClass() { System.out.println("BeforeClass method is called"); } @AfterClass public static void aClass() { System.out.println("AfterClass method is called"); } @Before public void bMethod() { System.out.println("before method is called"); } @After public void aMethod() { System.out.println("after method is called"); } @Test public void testMethod() { System.out.println("Test method is called"); } }


Advantages of JUnit Annotations

JUnit provides an easy way to execute the test cases in a specific way according to the requirements of the user.

It is easy to execute the multiple test cases parallelly using the JUnit by combining them in a test suite.

JUnit annotations used for automating test cases in Selenium provide a very detailed and interactive, graphical report to the user, which is very user-friendly.

JUnit allows to pass the parameters in the method using a very simple way.

JUnit annotation helps to perform clean coding, which is very easy to understand for both testers and programmers.


The above explanation clearly describes the importance of the JUnit framework and the order in which various annotations of JUnit are invoked. It is very important to understand the various annotations before using them in a program so that it will not create any issue in controlling the flow of execution.

Recommended Articles

This is a guide to JUnit Annotations. Here we discuss the introduction and Annotations of JUnit with Example, which includes @Before,@After,@Test, and @Ignore, etc. You can also go through our other suggested articles to learn more –

You're reading Various Annotations Of Junit With Examples

Update the detailed information about Various Annotations Of Junit With Examples on the website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!