CDI-Unit-JUnit-Extension

JUnit 5 extension for running CDI-Unit tests

License

License

Categories

Categories

JUnit Unit Testing
GroupId

GroupId

com.github.cschabl.cdi-unit-junit5
ArtifactId

ArtifactId

cdi-unit-junit5
Last Version

Last Version

0.4
Release Date

Release Date

Type

Type

jar
Description

Description

CDI-Unit-JUnit-Extension
JUnit 5 extension for running CDI-Unit tests
Project URL

Project URL

https://github.com/cschabl/cdi-unit-junit5
Source Code Management

Source Code Management

https://github.com/cschabl/cdi-unit-junit5/tree/master

Download cdi-unit-junit5

How to add to project

<!-- https://jarcasting.com/artifacts/com.github.cschabl.cdi-unit-junit5/cdi-unit-junit5/ -->
<dependency>
    <groupId>com.github.cschabl.cdi-unit-junit5</groupId>
    <artifactId>cdi-unit-junit5</artifactId>
    <version>0.4</version>
</dependency>
// https://jarcasting.com/artifacts/com.github.cschabl.cdi-unit-junit5/cdi-unit-junit5/
implementation 'com.github.cschabl.cdi-unit-junit5:cdi-unit-junit5:0.4'
// https://jarcasting.com/artifacts/com.github.cschabl.cdi-unit-junit5/cdi-unit-junit5/
implementation ("com.github.cschabl.cdi-unit-junit5:cdi-unit-junit5:0.4")
'com.github.cschabl.cdi-unit-junit5:cdi-unit-junit5:jar:0.4'
<dependency org="com.github.cschabl.cdi-unit-junit5" name="cdi-unit-junit5" rev="0.4">
  <artifact name="cdi-unit-junit5" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.github.cschabl.cdi-unit-junit5', module='cdi-unit-junit5', version='0.4')
)
libraryDependencies += "com.github.cschabl.cdi-unit-junit5" % "cdi-unit-junit5" % "0.4"
[com.github.cschabl.cdi-unit-junit5/cdi-unit-junit5 "0.4"]

Dependencies

compile (2)

Group / Artifact Type Version
javax.inject : javax.inject jar 1
org.junit.jupiter : junit-jupiter-api jar 5.4.2

provided (2)

Group / Artifact Type Version
org.jglue.cdi-unit : cdi-unit jar 4.1.0
org.jboss.weld.se : weld-se-core jar 3.0.0.Final

runtime (1)

Group / Artifact Type Version
junit : junit jar 4.12

test (2)

Group / Artifact Type Version
org.junit.jupiter : junit-jupiter-engine jar 5.4.2
org.slf4j : slf4j-simple jar 1.7.29

Project Modules

There are no modules declared in this project.

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.

Maven Central

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.

Versions

Version
0.4
0.3
0.2
0.1