weld / weld-testing

Set of test framework extensions (JUnit 4, JUnit 5, Spock) to enhance the testing of CDI components via Weld. Supports Weld 5.
http://weld.cdi-spec.org/
Apache License 2.0
102 stars 30 forks source link

Weld Testing Extensions

Maven Central License

The primary goal of this project is to provide simple and fast tools for CDI unit/component testing. The tools are implemented as JUnit 4, JUnit 5 and Spock extensions.

:warning: Version 5.x of this extension supports Weld 6.0 (CDI 4.1) along with jakarta namespace

The What

Extensions in this repository allow you to write unit tests for your beans regardless of the target environment (Java SE, Java EE, etc.). They start a real CDI container with minimal configuration meaning you can leverage all bean capabilities - injection, interception, events, etc. - without the need for mocking. These extensions boot up CDI container before each test container run and shut it down afterwards. You have the power to customize what beans, extensions, interceptors (and so on) are going to be in the container. Furthermore, you can @Inject directly in your test class - and the list goes on...

The Why

As a matter of fact, one could have a unit test for CDI bean without running a container, but that would present quite a few drawbacks. Simulating field injection to start with, then interceptors and/or decorators - all in all, quite a challenge. There are frameworks to make this easier such as Mockito; but use too many mocks and things get tangled real quick. So we came with JUnit/Spock extensions which allow you to use actual CDI container instead of complex simulations. There is no need to change the way you develop your CDI components if you have a real container to test it with. Besides, it's easy to combine this approach with mocking frameworks (see for instance Adding mock beans).

How To Use Each Extension

This project consists of three modules. Below is a list with links to detailed README of each extension: