Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
junit

How to use common JUnit annotations

Educative Answers Team
widget

JUnit annotations are specialized syntactic meta-data used in Java to improve readability and code structure – they can be applied to variables, classes, methods, packages, and parameters.

Common Annotations

@Test

  • Used to suggest that the following method is a test method.
  • The method can’t have a return value.
  • It shouldn’t be private or static.
  • The method may declare parameters.

@Test(timeout = 50)

  • This annotation is used to automatically stop tests from running after a set duration of time.

@ParametrizedTest

  • It is used to run a test method multiple times with different parameters.
  • It requires a source for the method argument, e.g., Value Source.

@RepeatedTest

  • This annotation takes in value x: the number of times the test should be repeated.

@TestInstance

  • JUnit creates an instance of each test class when using @Test. @TestInstance changes the method to ‘per-class’ as test instances are created once within a class.

@Nested

  • By default, JUnit does not allow any nested classes in the test method, but @Nested does.
  • Nested classes shouldn’t be static.

@BeforeEach

  • This annotation implements the following method before performing each test.
  • It may be used to set up preconditions for the test.

@AfterEach

  • This annotation implements the following method after performing each test.

@BeforeAll

  • This annotation implements the following method before performing any test.

@AfterAll

  • This annotation implements the following method after performing any test.

@BeforeClass

  • This annotation implements the following method before all the test cases within a class.

@AfterClass

  • This annotation implements the following method after all the tests within a class.

@Disabled

  • Rather than commenting out a test, use the @Disabled annotation to ensure that the test is not run in the following implementation.

Code

The code below suggests how different annotations can be used.

class HelloWorld {

    @AfterTest
    public static void a_t( String args[] ) {
        System.out.println( "Run this after running Test!!" );
    }
    @AfterClass
    public static void a_c( String args[] ) {
        System.out.println( "Run this after running all Tests in a class!!" );
    }
    @Test  
    public static void _t( String args[] ) {
        System.out.println( "Test method" );
    }
    @BeforeTest
    public static void b_t( String args[] ) {
        System.out.println( "Run this before running Test!!" );
    }
    @BeforeClass
    public static void b_c( String args[] ) {
        System.out.println( "Run this before running all Tests in a class!!" );
    }
}

Most of these annotations can be found in the org.junit.jupiter.api package in the junit-jupiter-api module.

RELATED TAGS

java
junit
Copyright ©2022 Educative, Inc. All rights reserved
RELATED COURSES

View all Courses

Keep Exploring