In this post, We will talk and learn about Dependency Injection for Constructors and Methods
In all previous JUnit versions, test constructors or methods were not allowed to have parameters. We have one of the major changes in JUnit Jupiter, both test constructors and methods are now permitted to have parameters. These allow for greater flexibility and enable Dependency Injection for constructors and methods.
In case if a test class method or constructor, or a lifecycle method accepts TestInfo as a parameter then the parameter must be resolved at runtime by automatically registered ParameterResolver.
We can use TestInfo to retrieve information about the current container or test such as the display name, the test class, the test method, and associated tags.
Let’s try to understand the above concept using a demo project:
pom.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.kkjavatutorials</groupId> <artifactId>JUnit5DIForConstructorAndMethodExample</artifactId> <version>0.0.1-SNAPSHOT</version> <properties> <maven.compiler.target>8</maven.compiler.target> <maven.compiler.source>8</maven.compiler.source> <junit.jupiter.version>5.6.2</junit.jupiter.version> </properties> <dependencies> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-api</artifactId> <version>${junit.jupiter.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-engine</artifactId> <version>${junit.jupiter.version}</version> <scope>test</scope> </dependency> </dependencies> </project> |
MyUtils.java
1 2 3 4 5 6 7 8 |
package com.kkjavatutorials; public class MyUtils { public int add(int n1, int n2) { return n1+n2; } } |
DIForConstructorAndMethodTest.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
package com.kkjavatutorials; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue; import java.util.Set; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Tag; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.TestInfo; @DisplayName("Dependency Injection for Constructors and Methods") public class DIForConstructorAndMethodTest { private static MyUtils myUtils; public DIForConstructorAndMethodTest(TestInfo testInfo) { System.out.println("Inside DIForConstructorAndMethodTest Constructor.."); String displayName = testInfo.getDisplayName(); assertEquals("Dependency Injection for Constructors and Methods", displayName); } @BeforeAll static void setUp(TestInfo testInfo) { myUtils = new MyUtils(); System.out.println("Test data set up is done.."); //System.out.println(testInfo.getTags()); } @BeforeEach void beforeEach() { System.out.println("@BeforeEach is executed.."); } @Test @Tag(value = "tag_1") @DisplayName(value = "adding two positive numbers") void test_add_two_positive_numbers(TestInfo testInfo) { int actualResult = myUtils.add(20, 10); assertEquals(30, actualResult); Set<String> tags = testInfo.getTags(); System.out.println(tags); assertTrue(tags.contains("tag_1")); } @Test @Tag(value = "tag_2") @DisplayName(value = "adding two negtaive numbers") void test_add_two_negative_numbers(TestInfo testInfo) { int actualResult = myUtils.add(-20, -10); assertEquals(-30, actualResult); } @Test @DisplayName(value = "adding one positive and one negative number") void test_add_one_positive_and_one_negative_number() { int actualResult = myUtils.add(20, -10); assertEquals(10, actualResult); } @AfterEach void afterEach() { System.out.println("@AfterEach is executed.."); } @AfterAll static void tearDown(TestInfo testInfo) { myUtils = null; System.out.println("Test data teardown is done.."); //System.out.println(testInfo.getTags()); } } |
The output of the above project:
You May Also Like:
Junit 5 Architecture
JUnit 5 Annotations
JUnit 5 Maven Dependency
JUnit 5 with Gradle Dependency
JUnit 5 Test Lifecycle
JUnit 5 @BeforeAll annotation example
Unit 5 @AfterAll annotation example
JUnit 5 @BeforeEach and @AfterEach annotation Example
JUnit 5 Display Names
Assertions in JUnit 5 Examples
Third-party Assertion Libraries support in JUnit 5
JUnit 5 Assumptions Examples
Conditional Test Execution in JUnit 5
JUnit 5 Nested Tests Example
JUnit 5 @Tag Annotation example
Test Execution Order in Junit 5
That’s all about Dependency Injection and Testing in JUnit 5
If you have any feedback or suggestion please feel free to drop in below comment box.