https://github.com/ajsquared/junit-parameterized-test-case
Library for parameterizing individual test methods in JUnit
https://github.com/ajsquared/junit-parameterized-test-case
java java8 junit junit4 testing
Last synced: 9 months ago
JSON representation
Library for parameterizing individual test methods in JUnit
- Host: GitHub
- URL: https://github.com/ajsquared/junit-parameterized-test-case
- Owner: ajsquared
- License: mit
- Created: 2018-03-03T16:02:40.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2020-12-05T00:17:29.000Z (over 5 years ago)
- Last Synced: 2025-06-17T03:05:20.251Z (12 months ago)
- Topics: java, java8, junit, junit4, testing
- Language: Java
- Size: 74.2 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# junit-parameterized-test-case [](https://travis-ci.org/ajsquared/junit-parameterized-test-case)
`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`:
```java
@RunWith(ParameterizedTestCaseRunner.class)
public class ParameterizedTestCaseExampleTest {
public static class ExampleParameterGenerator implements ParameterGenerator> {
@Override
public Collection> 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 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.