Skip to content

jonasrutishauser/cdi-test

Repository files navigation

A JUnit 5 extension for easy and efficient testing of CDI components.

GNU Lesser General Public License, Version 3, 29 June 2007 Maven Central Build Status Coverage

Reimplementation of guhilling/cdi-test

This is more or less a reimplementation of guhilling/cdi-test with some small differences:

  • Clean CDI Lifecycle:
    • @TestScoped is already active when CDI is bootstrapped
    • Use standard scope events like @Initialized
  • @ApplicationScoped beans are not destroyed after each test
  • @ApplicationScoped beans which need to be @TestScoped can be declared in a file testScoped.beans. Initialization using the @Initialized event will still work as long as the event type is Object.

Why should you use it?

If you're developing Jakarta EE (Java EE) or just plain CDI applications or libraries you'll probably want to unit test your code. If you don't you really should. With cdi-test there's no excuse not to do it ;-)

As CDI doesn't come with any "standard" unit test capabilities you need some way to test your components in your JUnit 5 tests.

Highlights

cdi-test is targeted at running unit, component and integration tests at scale. It accomplishes this with:

  • Only booting the cdi container once for all unit tests. This allows for running a huge number of tests even in big projects where booting might take some time.
  • cdi-test uses Weld (the cdi reference implementation) as cdi container. So you can use the exact same cdi container as in your application runtime in case you're running e.g. Wildfly, JBoss EAP, GlassFish, Oracle WebLogic or Open Liberty.
  • cdi-test supports mocks and test alternatives for CDI beans. These can be activated per test class. So you can freely choose what you want to test and need to mock test-by-test.

Compatibility

  • CDI 3 and CDI 4 with the relevant releases of Weld
  • Java 11+
  • Jakarta EE 9 and Jakarta EE 10
    • Includes using EJBs with some restrictions. Injection and creation of @Singleton and @Stateless ejbs.
    • Supports JPA even with multiple persistence units. Inject EntityManager via @PersistenceContext.
  • JUnit 5
  • Microprofile Config 3.x
    • Supports injecting test specific properties using annotations.
  • Mockito is supported.