Skip to content
Ameen Abozaid
Planned lessonJNR-043-L03

Parameterized tests

  • Lesson 3 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

run one test body against many inputs.

Planned topics

  • @ParameterizedTest replaces copy-pasted tests that differ only in data.
  • @ValueSource for a list of single values.
  • @CsvSource for several columns per row (input and expected result).
  • @MethodSource for complex objects built in a static method.
  • Use {0}-style placeholders in the name attribute so failures say which row broke.
Draft outline details

Demonstration

java
@ParameterizedTest(name = "{0}% off {1} = {2}")
@CsvSource({"10,100.00,90.00", "0,100.00,100.00", "50,80.00,40.00"})
void discountTable(int pct, BigDecimal price, BigDecimal expected) {
    assertEquals(0, expected.compareTo(calc.discounted(price, pct)));
}

Check your understanding

Which source fits a table of input/expected pairs?

Show answer

@CsvSource.

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.