JUnit5 Important Annotations

September 30, 2020 | No comments

JUnit5 Important Annotations

Junit 5 comes with some new annotations that were not available in previous releases. Understanding these Annotations will help in writing efficient tests. This article covers some of the most commonly used annotations. In the next section, we will example of those annotations.
The following are some of the important annotations in JUnit 5.

@Test

@Test : Denotes that a method is a test method. Without this annotation, JUnit will ignore the method

@ParameterizedTest

Denotes a method to be a parameterized test method. Parameterized tests make it possible to run a test multiple times with different arguments.

@RepeatedTest

@RepeatedTest : Test methods marked with @RepeatedTest can be executed repeatedly

@TestMethodOrder

@TestMethodOrder: Helps maintain order in which test methods are executed.

@DisplayName

@DisplayName : Helps in using a custom name to be printed either in the console or test report for the test method.

@DisplayNameGeneration

@DisplayNameGeneration : Declares a custom display name generator for the test class

@BeforeEach

@BeforeEach: Analogous to JUnit 4 @Before. Denotes the annotated method should be executed before each test. The method should not return any value, should not be private.

@AfterEach

@AfterEach : Analogous to JUnit 4 @After. Denotes the annotated method should be executed after each test. The method should not return any value, should not be private.

@BeforeAll

@BeforeAll: Denotes that the annotated method should be executed before all @Test, @RepeatedTest, @ParameterizedTest, and @TestFactory methods in the current class; analogous to JUnit 4’s @BeforeClass. Such methods are inherited (unless they are hidden or overridden) and must be static (unless the "per-class" test instance lifecycle is used).

@AfterAll

@AfterAll: Denotes that the annotated method should be executed after all @Test, @RepeatedTest, @ParameterizedTest, and @TestFactory methods in the current class; analogous to JUnit 4’s @AfterClass. A method marked with @AfterAll is inherited (unless they are hidden or overridden) and must be static.

@Nested

@Nested:Denotes that the annotated class is a non-static nested test class. @BeforeAll and @AfterAll methods cannot be used directly in a @Nested test class unless the "per-class" test instance lifecycle is used. Such annotations are not inherited.

@Tag

@Tag: Used to declare tags for filtering tests, either at the class or method level; analogous to test groups in TestNG or Categories in JUnit 4. Such annotations are inherited at the class level but not at the method level.

@Disabled

@Disabled : Used to disable a test class or test method; analogous to JUnit 4’s @Ignore. Such annotations are not inherited.

@Timeout

@Timeout: Used to fail a test, test factory, test template, or lifecycle method if its execution exceeds a given duration. Such annotations are inherited.

Next

No comments :

Post a Comment

Please leave your message queries or suggetions.