Exercise: Dependency Injection Using CDI

Write an extension that supports dependency injection using CDI.

Problem statement

Dependency injection makes service objects easier to use when writing tests.

Contexts and Dependency Injection (CDI) is a standard dependency injection framework with Java Enterprise Edition 6 and higher. It enables us to control the lifecycle of stateful components using domain-specific lifecycle contexts and type-safely inject components (services) into client objects.

Write an extension that supports dependency injection using CDI.

Coding challenge

Take some time to determine the logic behind this problem before jumping to the implementation. The function skeleton has been created. You only need to write code in its body.

Task 1

Create a UserService class and add find(), createOrUpdate(), and delete() methods in it.

Task 2

Write functions in the test class UserServiceTest to test methods of the UserService class using the assertTrue and assertFalse assertions.

If you get stuck, feel free to look at the solution review in the next lesson.

Good luck!

Get hands-on with 1200+ tech skills courses.