Plain unit tests for services
On This Lesson 4 topics
This lesson is planned. The outline below lists the topics it will cover. The full lesson has not been published yet.
In this lesson
test a service without Spring.
Planned topics
- Constructor injection means you can call
newwith fakes. - No annotations, no context: runs in milliseconds.
- Test one rule per test; name it by behavior.
- Use JUnit 5 and AssertJ assertions.
Draft outline details
Demonstration
@Test
void unknownProductThrowsNotFound() {
var service = new ProductService(new InMemoryProductRepository());
assertThatThrownBy(() -> service.get(999)).isInstanceOf(NotFoundException.class);
}Check your understanding
Why does a test need no Spring here?
Show answer
The class takes dependencies via its constructor.
Practice
Apply this lesson with the module's lab, exercises and quiz.
Marking a lesson complete is optional and you can undo it any time. Your progress stays in this browser.