Skip to content
Ameen Abozaid
Planned lessonJNR-043-L01

What a unit is

  • Lesson 1 of 6
  • 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

decide what to test in a unit test and what to leave out.

Planned topics

  • A unit is a small piece of behavior: usually one class or one method with its logic.
  • A unit test runs in milliseconds, with no database, network, or files.
  • Test the public behavior (inputs and outputs), not private methods. If a private method seems to need a test, the logic probably belongs in its own class.
  • A good test fails for one reason only, and its name says which.
  • Verify the current JUnit Jupiter version before recording.
Draft outline details

Demonstration

java
class PriceCalculatorTest {
    @Test
    void appliesTenPercentDiscount() {
        var calc = new PriceCalculator();
        assertEquals(new BigDecimal("90.00"), calc.discounted(new BigDecimal("100.00"), 10));
    }
}

Check your understanding

Should a unit test talk to PostgreSQL?

Show answer

No; that is an integration test.

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.