Sobes.tech
Junior — Middle

Can you explain what parameterized tests are and why they are used?

sobes.tech AI

Answer from AI

Parameterized tests are tests that are run multiple times with different sets of input data. This allows testing the same logic with different values without duplicating code.

Why use them:

  • They increase test coverage.
  • They simplify maintenance, as one test checks multiple cases.
  • They make tests more readable and compact.

Example in JUnit 5:

import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

class ExampleTest {
    @ParameterizedTest
    @ValueSource(strings = {"racecar", "radar", "level"})
    void testPalindrome(String candidate) {
        assertTrue(isPalindrome(candidate));
    }

    boolean isPalindrome(String text) {
        return new StringBuilder(text).reverse().toString().equals(text);
    }
}