JUnit 5 Tutorial For Absolute Beginners
In this blog, we are going to talk about JUnit 5. JUnit has always been one of the most popular unit-testing frameworks when working on Java applications.
Note- JUnit 5 requires that you have at least Java 8 version installed.
Setting Up JUnit 5
<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api --> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-api</artifactId> <version>5.9.0</version> <scope>test</scope> </dependency>
<plugin> <artifactId>maven-surefire-plugin</artifactId> <version>2.19.1</version> <dependencies> <dependency> <groupId>org.junit.platform</groupId> <artifactId>junit-platform-surefire-provider</artifactId> <version>1.3.2</version> </dependency> </dependencies> </plugin>
JUnit 5 Architecture
1. JUnit Platform
Testing frameworks are launched on the JVM by the platform. It specifies a reliable and potent interface for JUnit's users, such as build tools. To find and run tests, the platform easily connects clients with JUnit.
Additionally, it specifies the TestEngine API for creating testing frameworks that utilize the JUnit software. We can easily integrate third-party testing libraries into JUnit by developing a new TestEngine.
2. JUnit Jupiter
3. JUnit Vintage
Basic Annotations
@BeforeAll
public class BeforeAndAfterAnnotationExample { @BeforeAll static void beforeAll() { System.out.println("Executed once before all test cases in this class"); } }
@AfterAll
public class BeforeAndAfterAnnotationExample { @AfterAll static void afterAll() { System.out.println("Executed after all test cases in this class"); } }
@BeforeEach
public class BeforeAndAfterAnnotationExample { @BeforeEach static void beforeEach() { System.out.println("Executed before each test cases in this class"); } }
@AfterEach
This annotation is just like @BeforeEach annotation and the only difference is that it will help you execute a logic once after(instead of before in @BeforeEach) each of the test cases gets executed within a class. Now, let's see an example.
public class BeforeAndAfterAnnotationExample {
@AfterEach
static void afterEach() {
System.out.println("Executed after each test cases in this class");
}
}
public class BeforeAndAfterAnnotationExample { @AfterEach static void afterEach() { System.out.println("Executed after each test cases in this class"); } }
@DisplayName
public class DisplayNameAnnotationExample { @Test @DisplayName("This is Test 1") void test_1() { System.out.println("Executing test 1"); } }
public class DisabledAnnotationExample { @Test @Disabled void test_dummy() { System.out.println("This is a dummy test"); } }
@Disabled public class DisabledAnnotationExample { @Test void test_dummy() { System.out.println("This is a dummy test"); } }
Assertions
@Test void test_assertion() {
List<Integer> nums = Arrays.asList(1,2,4); Assertions.assertEquals(7, nums.stream().mapToInt(Integer::intValue).sum(), "Sum is not equal to 7"); }
Assumptions
public class AssumptionsExample { @BeforeAll static void setup() { System.setProperty("APP_MODE", "DEV"); } @Test void test_1() { Assumptions.assumeTrue("DEV".equals(System.getProperty("APP_MODE")), "Aborting as the mode is not DEV"); Assertions.assertEquals(3, 1 + 2); } @Test void test_2() { Assumptions.assumingThat("DEV".equals(System.getProperty("APP_MODE")), () -> { Assertions.assertEquals(3, 1 + 2, "Sum is not as expected"); }); } }
public class AssumptionsExample { @BeforeAll static void setup() { System.setProperty("APP_MODE", "DEV1"); } @Test void test_1() { Assumptions.assumeTrue("DEV".equals(System.getProperty("APP_MODE")), "Aborting as the mode is not DEV"); Assertions.assertEquals(3, 1 + 2); } }
Nested Test Classes
public class NestedTestExample { @BeforeAll static void setup() { System.setProperty("APP_MODE", "DEV"); } @Nested @DisplayName("All DEV env tests") class DevEnvTest { @Tag("DEV") @Test @DisplayName("This is Test 1") void test_1() { Assumptions.assumeTrue("DEV".equals(System.getProperty("APP_MODE")), "Aborting as the mode is not DEV"); Assertions.assertEquals(3, 1 + 2); } @Tag("DEV") @Test @Disabled("Disabled until some requirement has been fulfilled") void test_2() { Assumptions.assumingThat("DEV".equals(System.getProperty("APP_MODE")), () -> { Assertions.assertEquals(3, 1 + 2, "Sum is not as expected"); }); } } }
public class ExceptionExample { @Test void test_exception() { Throwable ex = Assertions.assertThrows(RuntimeException.class, () -> { throw new RuntimeException("An Exception Occurred"); }); Assertions.assertEquals("An Exception Occurred", ex.getMessage()); } }
No comments:
Post a Comment