cdi-unit-junit5
JUnit 5 Extension for CDI-Unit.
CdiUnitExtension
of this project is the JUnit 5 counterpart of the JUnit 4 runner CdiRunner
of CDI-Unit.
Usage
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.
}
Adding cdi-unit-junit5 to your project
Prerequisites
- JUnit 5.4 or higher
- CDI-Unit 4.x
Maven
Add the cdi-unit-junit5 dependency:
<dependency>
<groupId>com.github.cschabl.cdi-unit-junit5</groupId>
<artifactId>cdi-unit-junit5</artifactId>
<version>0.4</version>
<scope>test</scope>
</dependency>
Make sure you've added the CDI-Unit dependency and the preferred Weld SE dependency:
<dependency>
<groupId>org.jglue.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>
Gradle
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:0.4"
...
}
Restrictions
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
@org.jglue.cdiunit.ProducerConfig
.
Liabilities
cdi-unit-junit5 has a transitive dependency on JUnit 4, because CDI-Unit has one, but assumes that the consuming project declares it explicitly (see CDI-Unit PR #155). To avoid unexpected exceptions at runtime with JUnit 5, cdi-unit-junit5 has an explicit dependency on JUnit 4.12.