Skip to content

Commit 535cf61

Browse files
Small improvements and documentation following PR #3085
1 parent 078661e commit 535cf61

File tree

6 files changed

+31
-21
lines changed

6 files changed

+31
-21
lines changed

documentation/src/docs/asciidoc/release-notes/release-notes-5.10.0-M1.adoc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ repository on GitHub.
4242

4343
==== New Features and Improvements
4444

45-
* New `ArgumentsAccessor.getInvocationIndex` method to supply index of a `@ParameterizedTest` invocation.
45+
* New `ArgumentsAccessor.getInvocationIndex()` method that supplies the index of a
46+
`@ParameterizedTest` invocation.
4647

4748

4849
[[release-notes-5.10.0-M1-junit-vintage]]

documentation/src/docs/asciidoc/user-guide/writing-tests.adoc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1769,6 +1769,9 @@ this API, you can access the provided arguments through a single argument passed
17691769
test method. In addition, type conversion is supported as discussed in
17701770
<<writing-tests-parameterized-tests-argument-conversion-implicit>>.
17711771

1772+
Besides, you can retrieve the current test invocation index with
1773+
`ArgumentsAccessor.getInvocationIndex()`.
1774+
17721775
[source,java,indent=0]
17731776
----
17741777
include::{testDir}/example/ParameterizedTestDemo.java[tags=ArgumentsAccessor_example]

junit-jupiter-params/src/main/java/org/junit/jupiter/params/ParameterizedTestMethodContext.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,9 @@ Resolver createResolver(ParameterContext parameterContext) {
213213
}
214214

215215
interface Resolver {
216+
216217
Object resolve(ParameterContext parameterContext, Object[] arguments, int invocationIndex);
218+
217219
}
218220

219221
static class Converter implements Resolver {

junit-jupiter-params/src/main/java/org/junit/jupiter/params/aggregator/DefaultArgumentsAccessor.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,14 @@
3535
@API(status = INTERNAL, since = "5.2")
3636
public class DefaultArgumentsAccessor implements ArgumentsAccessor {
3737

38-
private final Object[] arguments;
3938
private final int invocationIndex;
39+
private final Object[] arguments;
4040

4141
public DefaultArgumentsAccessor(int invocationIndex, Object... arguments) {
42+
Preconditions.condition(invocationIndex >= 1, () -> "invocation index must be >= 1");
4243
Preconditions.notNull(arguments, "Arguments array must not be null");
43-
this.arguments = arguments;
4444
this.invocationIndex = invocationIndex;
45+
this.arguments = arguments;
4546
}
4647

4748
@Override

junit-jupiter-params/src/test/java/org/junit/jupiter/params/aggregator/AggregatorIntegrationTests.java

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,20 @@ void reportsExceptionForErroneousAggregator() {
184184
message("Error aggregating arguments for parameter at index 0: something went horribly wrong"))));
185185
}
186186

187+
@ParameterizedTest
188+
@CsvSource({ //
189+
"first", //
190+
"second" //
191+
})
192+
void argumentsAccessorInvocationIndex(ArgumentsAccessor arguments) {
193+
if ("first".equals(arguments.getString(0))) {
194+
assertEquals(1, arguments.getInvocationIndex());
195+
}
196+
if ("second".equals(arguments.getString(0))) {
197+
assertEquals(2, arguments.getInvocationIndex());
198+
}
199+
}
200+
187201
private void testPersonAggregator(Person person) {
188202
if (person.firstName.equals("Jane")) {
189203
assertEquals("Jane Doe", person.getFullName());
@@ -208,17 +222,6 @@ private EngineExecutionResults execute(DiscoverySelector... selectors) {
208222
return EngineTestKit.execute("junit-jupiter", request().selectors(selectors).build());
209223
}
210224

211-
@ParameterizedTest
212-
@CsvSource({ "first", "second" })
213-
void argumentsAccessorInvocationIndex(ArgumentsAccessor arguments) {
214-
if ("first".equals(arguments.getString(0))) {
215-
assertEquals(1, arguments.getInvocationIndex());
216-
}
217-
if ("second".equals(arguments.getString(0))) {
218-
assertEquals(2, arguments.getInvocationIndex());
219-
}
220-
}
221-
222225
// -------------------------------------------------------------------------
223226

224227
public static class Person {

junit-jupiter-params/src/test/kotlin/org/junit/jupiter/params/aggregator/ArgumentsAccessorKotlinTests.kt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@ class ArgumentsAccessorKotlinTests {
2121

2222
@Test
2323
fun `get() with reified type and index`() {
24-
assertEquals(1, DefaultArgumentsAccessor(0, 1).get<Int>(0))
25-
assertEquals('A', DefaultArgumentsAccessor(0, 'A').get<Char>(0))
24+
assertEquals(1, DefaultArgumentsAccessor(1, 1).get<Int>(0))
25+
assertEquals('A', DefaultArgumentsAccessor(1, 'A').get<Char>(0))
2626
}
2727

2828
@Test
2929
fun `get() with reified type and index for incompatible type`() {
3030
val exception = assertThrows<ArgumentAccessException> {
31-
DefaultArgumentsAccessor(0, Integer.valueOf(1)).get<Char>(0)
31+
DefaultArgumentsAccessor(1, Integer.valueOf(1)).get<Char>(0)
3232
}
3333

3434
assertThat(exception).hasMessage(
@@ -38,13 +38,13 @@ class ArgumentsAccessorKotlinTests {
3838

3939
@Test
4040
fun `get() with index`() {
41-
assertEquals(1, DefaultArgumentsAccessor(0, 1).get(0))
42-
assertEquals('A', DefaultArgumentsAccessor(0, 'A').get(0))
41+
assertEquals(1, DefaultArgumentsAccessor(1, 1).get(0))
42+
assertEquals('A', DefaultArgumentsAccessor(1, 'A').get(0))
4343
}
4444

4545
@Test
4646
fun `get() with index and class reference`() {
47-
assertEquals(1, DefaultArgumentsAccessor(0, 1).get(0, Integer::class.java))
48-
assertEquals('A', DefaultArgumentsAccessor(0, 'A').get(0, Character::class.java))
47+
assertEquals(1, DefaultArgumentsAccessor(1, 1).get(0, Integer::class.java))
48+
assertEquals('A', DefaultArgumentsAccessor(1, 'A').get(0, Character::class.java))
4949
}
5050
}

0 commit comments

Comments
 (0)