Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recipe: parametrized tests in JUnit 5 #375

Open
hobovsky opened this issue Feb 7, 2022 · 3 comments
Open

Recipe: parametrized tests in JUnit 5 #375

hobovsky opened this issue Feb 7, 2022 · 3 comments
Labels
kind/recipe New Recipe language/java Articles related to Java

Comments

@hobovsky
Copy link
Contributor

hobovsky commented Feb 7, 2022

import java.util.*;
import java.util.stream.*;
import java.util.function.*;

import org.junit.jupiter.api.*;
import org.junit.jupiter.params.*;
import org.junit.jupiter.params.provider.*;
import static org.junit.jupiter.api.Assertions.*;

// Values source
@ParameterizedTest
@ValueSource(strings = {"", "  "})
void isBlank_ShouldReturnTrueForNullOrBlankStrings(String input) {
    assertTrue(Strings.isBlank(input));
}

// CSV source
@ParameterizedTest
@CsvSource({"test,TEST", "tEst,TEST", "Java,JAVA"})
void toUpperCase_ShouldGenerateTheExpectedUppercaseValue(String input, String expected) {
    String actualValue = input.toUpperCase();
    assertEquals(expected, actualValue);
}

// Generator method
@ParameterizedTest
@MethodSource("provideStringsForIsBlank")
void isBlank_ShouldReturnTrueForNullOrBlankStrings(String input, boolean expected) {
    assertEquals(expected, Strings.isBlank(input));
}

private static Stream<Arguments> provideStringsForIsBlank() {
    return Stream.of(
      Arguments.of(null, true),
      Arguments.of("", true),
      Arguments.of("  ", true),
      Arguments.of("not blank", false)
    );
}
@hobovsky hobovsky added kind/recipe New Recipe language/java Articles related to Java labels Feb 7, 2022
@Madjosz
Copy link
Contributor

Madjosz commented Feb 7, 2022

I would suggest to add also display names:

import org.junit.jupiter.api.DisplayName;

@DisplayName("Tests with JUnit 5")
class SampleTests {

    @ParameterizedTest(name = "input: '{0}' and I expect you to return >>{1}<<")
    @DisplayName("We test some fixed Strings")
    @CsvSource({"test,TEST", "tEst,TEST"})
    void toUpperCase_ShouldGenerateTheExpectedUppercaseValue(String input, String expected) {
        String actualValue = input.toUpperCase();
        assertEquals(expected, actualValue);
    }
}

This will lead to the following output:
image

@Blind4Basics
Copy link
Contributor

Blind4Basics commented Feb 7, 2022

Finally readable parametrized tests in java...? XD

What about the order of the test methods?

@Madjosz
Copy link
Contributor

Madjosz commented Feb 16, 2022

import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.TestMethodOrder;

@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
class SampleTests {
    @Test
    @Order(2)
    void secondTest() { ... }

    @Test
    @Order(1)
    void firstTest() { ... }
}

Other built-in possibilities are

  • MethodOrderer.Alphanumeric (deprecated since JUnit 5.7, but will only be removed in JUnit 6)
  • MethodOrderer.DisplayName (since 5.7, still experimental in latest 5.8.2)
  • MethodOrderer.MethodName (since 5.7, still experimental in latest 5.8.2)
  • MethodOrderer.Random

CW currently uses JUnit 5.4.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/recipe New Recipe language/java Articles related to Java
Projects
None yet
Development

No branches or pull requests

3 participants