junit-parameterized-test-case

Library for parameterizing individual test methods in JUnit

License

License

GroupId

GroupId

com.andrewjamesjohnson
ArtifactId

ArtifactId

example-application
Last Version

Last Version

1.0.0
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 example-application

How to add to project

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

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.0