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.
jakarta
namespace
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...
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).
This project consists of three modules. Below is a list with links to detailed README of each extension:
- JUnit 4 extension
- JUnit 5
- JUnit-common
- Houses the parts of code shared by both Junit extensions
- Spock