I am using weld-junit to run unit tests on CDI components. In the past I have been using cdiunit too which is great, but it does not support JUnit 5. The idea of both is to launch the tests inside a real CDI container, in a minimal configuration. The important part is that you control explicitly what beans are available for injection, so the system skips the bean discovery part. This makes the tests fairly fast: the CDI overhead is only a few seconds. Both systems allow you to provide mock instances for any dependencies you want - I have been using Mockito for managing the mocks.
The minimal Maven setup (see here is to use a version of the Maven surefire plugin that enables JUnit 5, e.g.:
<pluginManagement>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
<plugin>
</pluginManagement>
And the other dependencies:
<dependency>
<groupId>org.jboss.weld</groupId>
<artifactId>weld-junit5</artifactId>
<version>${version.weld-junit5}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${version.junit.jupiter}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${version.junit.jupiter}</version>
<scope>test</scope>
</dependency>
<!-- If you want Mockito: -->
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>${version.mockito}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>${version.mockito}</version>
<scope>test</scope>
</dependency>
Having these in place, you can write & run a test (an example) for your CDI beans (example):
// weld-junit5 annotation - there are other ways to activate weld-junit5
// in your tests, and more features for controlling the container,
// read the full docs!
@EnableAutoWeld
// add real bean implementations in the DI container
@AddBeanClasses(RealBeanDependency.class)
// add extensions if you need
@AddExtensions({Extension1.class, Extension2.class})
// activate scopes, if you need
@ActivateScopes(RequestScoped.class)
// add automatic creation of mocks with Mockito (@Mock)
@ExtendWith(MockitoExtension.class)
public class MyClassTest {
// an example of introducing mocks to the DI container
@Produces @Mock
private ArticleService articleService;
// injected the "System Under Test"
// weld-junit will automatically add it to the available beans
@Inject
private MyClass sut;
@Test
void test() {
// this runs inside the minimal CDI container
// read the docs about details, e.g. it starts
// a fresh container for each test (I think)
}
}
internalin Kotlin), and it doesn't set up Hibernate/Panache. I've tried to setup an example at github.com/danelowe/quarkus-example/tree/… (the repository test, and the domain/service test error), and I'd be grateful for any working examples. - Dane Lowe