JUnit 5 Extension for CDI-Unit.
CdiUnitExtension
of this project is the JUnit 5 counterpart of the JUnit 4 runner CdiRunner
of CDI-Unit.
import com.github.cschabl.cdiunit.junit5.CdiUnitExtension;
// PER_CLASS lifecycle is supported:
// @TestInstance(TestInstance.Lifecycle.PER_CLASS)
@ExtendWith(CdiUnitExtension.class) // Runs the test with CDI-Unit
class MyTest {
@Inject
MyBean beanUnderTest; // This will be injected before the tests are run!
//The rest of the test goes here.
}
- JUnit 5.4 or higher
- CDI-Unit 5.0.0-EA2 or higher
Add the cdi-unit-junit5 dependency:
<dependency>
<groupId>com.github.cschabl.cdi-unit-junit5</groupId>
<artifactId>cdi-unit-junit5</artifactId>
<version>2.0-EA2</version>
<scope>test</scope>
</dependency>
Make sure you've added the CDI-Unit dependency and the preferred Weld SE dependency:
<dependency>
<groupId>io.github.cdi-unit</groupId>
<artifactId>cdi-unit</artifactId>
<version>${cdi-unit-version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.weld.se</groupId>
<!-- or weld-se -->
<artifactId>weld-se-core</artifactId>
<!-- Your preferred Weld version: -->
<version>${weld.version}</version>
<scope>test</scope>
</dependency>
And the JUnit 5 dependencies:
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${junit5-version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit5-version}</version>
<scope>test</scope>
</dependency>
dependencies {
...
testImplementation "org.junit.jupiter:junit-jupiter-api:${junit5-version}"
testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:${junit5-version}"
testImplementation "org.jglue.cdi-unit:cdi-unit:${cdi-unit-version}"
testImplementation "org.jboss.weld.se:weld-se-core:${weld-version}"
testImplementation "com.github.cschabl.cdi-unit-junit5:cdi-unit-junit5:2.0-EA2"
...
}
The following features aren't supported:
- Nested Tests (@Nested)
- Test class constructors with parameters, i.e. JUnit 5 dependency injection to constructors.
- Probably further JUnit-5-specific features.
- CDI extension
@io.github.cdiunit.ProducerConfig
.
- 2.x releases are based on CDI Unit 5.x and therefore on Jarkarta EE and Java 11
- Releases < 2.0 are based on CDI Unit 4.x and therefore on Java EE and Java 1.8