Skip to content

guhilling/cdi-test

Repository files navigation

Build CDI Test CodeQL Coverage (Sonar) Status (Sonar) Maintainability (Sonar) Maven Central

cdi-test: A JUnit extension for easy and efficient testing of CDI components

cdi-test is available under the The Apache Software License, Version 2.0

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. This is not easily accomplished especially when compared with the junit integration of Spring Boot.

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 or Oracle WebLogic.
  • 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.
  • Well tested and maintained and used in real projects with thousands of unit tests.

Compatibility

  • CDI 2 and CDI 3 with the relevant releases of Weld
  • Java 8+
  • Jakarta EE 8 and Jakarta EE 9 (The new namespace introduced with Jakarta EE 9 will be supported starting with release 4.x. Pre-releases are already available.)
    • Includes using EJBs with some restrictions. Injection and creation of @Stateful and @Stateless ejbs.
    • Supports JPA even with multiple persistence units. Inject EntityManager via @Inject or @PersistenceContext.
  • JUnit 5 (starting with cdi-test 3.x)
  • Microprofile Config 2.x and 3.x
    • Supports injecting test specific properties using annotations.
  • Mockito is supported and used internally.

Short example, please!

Most basic:

@ExtendWith(CdiTestJunitExtension.class)
class SimpleTest {
@Inject
private Person person;
@Test
void testInjection() {
assertNotNull(person);
}
}

Using mocks:

@ExtendWith(CdiTestJunitExtension.class)
@ExtendWith(MockitoExtension.class)
class MockProxyTest {
@Inject
private SampleService sampleService;
@Test
void createPersonWithMockBackend(@Mock BackendService backendService) {
Person person = new Person();
sampleService.storePerson(person);
verify(backendService).storePerson(person);
}
}

In-depth Documentation

See the Documentation on github pages.

Contributing

Contribution guidelines for this project

LICENSE

Copyright 2015 Gunnar Hilling

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Sponsors

JProfiler supports open source projects with its full-featured Java Profiler. Click the JProfiler logo below to learn more.

JProfiler JetBrains Logo

About

JUnit extension for easy and efficient testing of CDI components

Topics

Resources

License

Security policy

Stars

Watchers

Forks

Packages

No packages published

Languages