junit-parameterized-test-case

Library for parameterizing individual test methods in JUnit

License

License

Categories

Categories

JUnit Unit Testing
GroupId

GroupId

com.andrewjamesjohnson
ArtifactId

ArtifactId

junit-parameterized-test-case
Last Version

Last Version

1.0.1
Release Date

Release Date

Type

Type

jar
Description

Description

junit-parameterized-test-case
Library for parameterizing individual test methods in JUnit
Project URL

Project URL

https://github.com/ajsquared/junit-parameterized-test-case
Source Code Management

Source Code Management

https://github.com/ajsquared/junit-parameterized-test-case/

Download junit-parameterized-test-case

How to add to project

<!-- https://jarcasting.com/artifacts/com.andrewjamesjohnson/junit-parameterized-test-case/ -->
<dependency>
    <groupId>com.andrewjamesjohnson</groupId>
    <artifactId>junit-parameterized-test-case</artifactId>
    <version>1.0.1</version>
</dependency>
// https://jarcasting.com/artifacts/com.andrewjamesjohnson/junit-parameterized-test-case/
implementation 'com.andrewjamesjohnson:junit-parameterized-test-case:1.0.1'
// https://jarcasting.com/artifacts/com.andrewjamesjohnson/junit-parameterized-test-case/
implementation ("com.andrewjamesjohnson:junit-parameterized-test-case:1.0.1")
'com.andrewjamesjohnson:junit-parameterized-test-case:jar:1.0.1'
<dependency org="com.andrewjamesjohnson" name="junit-parameterized-test-case" rev="1.0.1">
  <artifact name="junit-parameterized-test-case" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.andrewjamesjohnson', module='junit-parameterized-test-case', version='1.0.1')
)
libraryDependencies += "com.andrewjamesjohnson" % "junit-parameterized-test-case" % "1.0.1"
[com.andrewjamesjohnson/junit-parameterized-test-case "1.0.1"]

Dependencies

compile (1)

Group / Artifact Type Version
junit : junit jar 4.12

Project Modules

There are no modules declared in this project.

junit-parameterized-test-case Build Status

junit-parameterized-test-case is a library for JUnit 4 that provides greater flexibility in writing parameterized tests.

Unlike the standard org.junit.runners.Parameterized test runner, which parameterizes the whole test class, junit-parameterized-test-case adds a @ParameterizedTest annotatation to support parameterizing specific test methods. This allows for different parameters to be used for different test cases within a single test class.

Usage

Here is a complete example of a test using junit-parameterized-test-case:

@RunWith(ParameterizedTestCaseRunner.class)
public class ParameterizedTestCaseExampleTest {
    public static class ExampleParameterGenerator implements ParameterGenerator<Tuple2<String, String>> {

        @Override
        public Collection<Tuple2<String, String>> generate() {
            return Arrays.asList(
                    Tuple2.of("a", "b"),
                    Tuple2.of("c", "d")
            );
        }
    }

    @Test
    public void basicTest() {
        assertTrue(true);
    }

    @ParameterizedTest(generator = ExampleParameterGenerator.class)
    public void parameterizedTest(Tuple2<String, String> param) {
        assertTrue(param.description().startsWith(param._1));
    }
}

This test class will thus have three test cases: basicTest, parameterizedTest - a,b, and parameterizedTest - c,d.

The key pieces are as follows:

  1. Annotate the class with @RunWith(ParameterizedTestCaseRunner.class)
  2. Annotate test method to parameterize with @ParameterizedTest. This annotation takes a generator parameter, which is a class implementing the ParameterGenerator interface. This annotation also supports the expected and timeout arguments like the standard @Test.
  3. All standard JUnit functionality (@Before/@After, @BeforeClass/@AfterClass, @Rule, etc.) are supported, as are regular test methods annotated with @Test. The behavior of these features is the same as in standard JUnit.

Parameter Generation

Generators for parameters must implement the ParameterGenerator interface. This interface has one method, generate, which produces a Collection of objects that implement the Parameter interface.

A Parameter can be an arbitrary object, but it must implement the description method which is used to name the test cases. For convenience Tuple1 through Tuple26 classes are provided to use as tuple parameters.

Versions

Version
1.0.1
1.0.0