JUnit @ParameterizedTest with @ValueSource

JUnit is a popular testing framework in the Java ecosystem that simplifies writing and running tests. The @ParameterizedTest annotation in JUnit is used to run the same test with different inputs. This guide covers the basics of using the @ParameterizedTest annotation with @ValueSource to write parameterized tests in JUnit.

Table of Contents

  1. Introduction
  2. Steps to Create a JUnit Parameterized Test
  3. Real-World Use Case
  4. Conclusion

Introduction

The @ParameterizedTest annotation marks methods that should be executed multiple times with different parameters. The @ValueSource annotation is used to provide a single array of values to the parameterized test.

Steps to Create a JUnit Parameterized Test

Step 1: Add Maven Dependency

To use JUnit in your project, you need to add the JUnit dependency to your pom.xml file. Use the latest version of JUnit 5:

<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter-engine</artifactId>
    <version>5.9.2</version>
    <scope>test</scope>
</dependency>

Step 2: Create the Class to be Tested

Create a Java class with methods that you want to test. For example, a simple MathUtils class:

public class MathUtils {
    public int add(int a, int b) {
        return a + b;
    }

    public int subtract(int a, int b) {
        return a - b;
    }

    public boolean isEven(int number) {
        return number % 2 == 0;
    }
}

Step 3: Create the Parameterized Test Class

Create a test class in the src/test/java directory. Use the @ParameterizedTest annotation and specify the source of the parameters using @ValueSource.

import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class MathUtilsTest {
    private final MathUtils mathUtils = new MathUtils();

    @ParameterizedTest
    @ValueSource(ints = {2, 4, 6, 8, 10})
    void testIsEven(int number) {
        assertTrue(mathUtils.isEven(number), number + " should be even");
    }
}

In this example, the testIsEven method is annotated with @ParameterizedTest and uses @ValueSource to provide a list of integers. The test method will be executed once for each value in the list.

Step 4: Run the Test

You can run the test using your IDE, Maven, or Gradle.

Using an IDE:

Most IDEs, like IntelliJ IDEA and Eclipse, have built-in support for running JUnit tests. Simply right-click on your test class or method and select “Run.”

Using Maven:

If you’re using Maven, you can run your tests with the following command:

mvn test

Using Gradle:

If you’re using Gradle, you can run your tests with the following command:

gradle test

Real-World Use Case

In real-world applications, you may need to test methods with various inputs. For instance, a StringUtils class might need to be tested with different string inputs to ensure correct behavior.

Create the Class to be Tested

Create a Java class that contains business logic. For example, a StringUtils class that checks if a string is uppercase:

public class StringUtils {
    public boolean isUpperCase(String str) {
        if (str == null || str.isEmpty()) {
            return false;
        }
        return str.equals(str.toUpperCase());
    }
}

In this class, the isUpperCase method checks if a given string is uppercase.

Create the Parameterized Test Class

Create a test class for the StringUtils in the src/test/java directory. Use the @ParameterizedTest annotation and @ValueSource to provide different string inputs:

import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class StringUtilsTest {
    private final StringUtils stringUtils = new StringUtils();

    @ParameterizedTest
    @ValueSource(strings = {"HELLO", "WORLD", "JAVA", "JUNIT"})
    void testIsUpperCase(String input) {
        assertTrue(stringUtils.isUpperCase(input), input + " should be uppercase");
    }
}

In this test class, the testIsUpperCase method is annotated with @ParameterizedTest and uses @ValueSource to provide different string inputs. The test method will be executed once for each input string.

Running the Tests

You can run the tests using your IDE, Maven, or Gradle.

Using an IDE:

Most IDEs, like IntelliJ IDEA and Eclipse, have built-in support for running JUnit tests. Simply right-click on your test class or method and select “Run.”

Using Maven:

If you’re using Maven, you can run your tests with the following command:

mvn test

Using Gradle:

If you’re using Gradle, you can run your tests with the following command:

gradle test

Conclusion

The @ParameterizedTest annotation in JUnit makes it easy to run the same test with different inputs. By using @ParameterizedTest with @ValueSource, you can ensure that your methods behave correctly with various inputs without writing multiple test cases. Understanding and using the @ParameterizedTest annotation effectively is crucial for developing robust and maintainable Java applications.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top