Skip to content
Ameen Abozaid
Planned lessonJNR-016-L02

Plain unit tests for services

  • Lesson 2 of 7
  • Junior
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 new with 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

java
@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.

Lab and exercises Quiz

Marking a lesson complete is optional and you can undo it any time. Your progress stays in this browser.

Continue to next lesson

Type to search levels, phases, modules and lessons.