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

JUnit 4 to 5 recipe for JUnit4 Parameterized Tests #81

Closed
pway99 opened this issue Mar 18, 2021 · 1 comment · Fixed by #89
Closed

JUnit 4 to 5 recipe for JUnit4 Parameterized Tests #81

pway99 opened this issue Mar 18, 2021 · 1 comment · Fixed by #89
Assignees
Labels
enhancement New feature or request

Comments

@pway99
Copy link
Contributor

pway99 commented Mar 18, 2021

Discovered while testing Boot2Junit4to5Migration on spring-batch

create a new recipe for converting JUnit4 Parameterized tests

spring-batch parameterized AlmostStatefulChunkRetryTests

@pway99 pway99 added the enhancement New feature or request label Mar 18, 2021
@pway99 pway99 self-assigned this Mar 22, 2021
@pway99
Copy link
Contributor Author

pway99 commented Mar 23, 2021

Recipe for @ParameterizedTest with @MethodSource

1. Remove `@RunWith(Parameterized.class)`
2. Replace `@Test` with `@ParameterizedTest` having arguments from `@Parameters` method
3. Add `@MethodSource(...)` with argument equal to `@Parameters` method name to each `@ParameterizedTest`
4. Remove @Parameters annotation
5. Change constructor to an initialization method having a void return type.
6. For each `@ParameterizedTest` Insert statement to Invoke initialization method with test parameters
7. Remove imports
	org.junit.Test;
	org.junit.runner.RunWith;
	org.junit.runners.Parameterized;
	org.junit.runners.Parameterized.Parameters;
8. Add imports
	org.junit.jupiter.params.ParameterizedTest;
	org.junit.jupiter.params.provider.MethodSource;


Before:

@RunWith(Parameterized.class)
public class VetTests {

    private String firstName;
    private String lastName;
    private Integer id;

    public VetTests(String firstName, String lastName, Integer id) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.id = id;
    }

    @Test
    public void testSerialization() {
        Vet vet = new Vet();
        vet.setFirstName(firstName);
        vet.setLastName(lastName);
        vet.setId(id);
        Vet other = (Vet) SerializationUtils
                .deserialize(SerializationUtils.serialize(vet));
        assertEquals(vet.getFirstName(), other.getFirstName());
        assertEquals(vet.getLastName(), other.getLastName());
        assertEquals(vet.getId(), other.getId());
    }

    @Parameters(name="{index}: {0} {1} - {2}")
    public static List<Object[]> parameters() {
        return Arrays.asList(new Object[] {
            new Object[] { "Otis "TheDog", 124 },
            new Object[] { "Garfield", "TheBoss", 126 });
    }

}


After:

public class VetTests {

    private String firstName;
    private String lastName;
    private Integer id;

    public void initVetTests(String firstName, String lastName, Integer id) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.id = id;
    }

    @ParameterizedTest(name = "{index}: {0} {1} {2}")
    @MethodSource("parameters")
    public void testSerialization(String firstName, String lastName, Integer id) {
        initVetTests(firstName, lastName, id);
        Vet vet = new Vet();
        vet.setFirstName(firstName);
        vet.setLastName(lastName);
        vet.setId(id);
        Vet other = (Vet) SerializationUtils
                .deserialize(SerializationUtils.serialize(vet));
        assertEquals(vet.getFirstName(), other.getFirstName());
        assertEquals(vet.getLastName(), other.getLastName());
        assertEquals(vet.getId(), other.getId());
    }

    public static List<Object[]> parameters() {
        return Arrays.asList(new Object[] {
            new Object[] { "Otis "TheDog", 124 },
            new Object[] { "Garfield", "TheBoss", 126 });
    }
}

Reference info

pway99 added a commit that referenced this issue Mar 24, 2021
…Junit 5 Jupiter ParameterizedTest equivalent. Fixes #81
jkschneider pushed a commit that referenced this issue Mar 24, 2021
…Junit 5 Jupiter ParameterizedTest equivalent. Fixes #81 (#89)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
1 participant