-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce support for injecting the current invocation index
This commit introduces a new @InvocationIndex annotation for injecting the current invocation index into @test, @beforeeach, and @AfterEach methods. Furthermore, support has been added for injecting the repetition count for @RepeatedTest methods using @InvocationIndex. Issues: #14, #214
- Loading branch information
Showing
6 changed files
with
143 additions
and
14 deletions.
There are no files selected for viewing
44 changes: 44 additions & 0 deletions
44
junit-jupiter-api/src/main/java/org/junit/jupiter/api/InvocationIndex.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* Copyright 2015-2017 the original author or authors. | ||
* | ||
* All rights reserved. This program and the accompanying materials are | ||
* made available under the terms of the Eclipse Public License v1.0 which | ||
* accompanies this distribution and is available at | ||
* | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
*/ | ||
|
||
package org.junit.jupiter.api; | ||
|
||
import static org.junit.platform.commons.meta.API.Usage.Experimental; | ||
|
||
import java.lang.annotation.Documented; | ||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
import org.junit.platform.commons.meta.API; | ||
|
||
/** | ||
* {@code @InvocationIndex} is used to inject the current <em>invocation index</em> | ||
* into {@code @Test}, {@code @BeforeEach}, and {@code @AfterEach} methods. | ||
* | ||
* <p>An invocation index is a counter associated with a given | ||
* {@link org.junit.jupiter.api.extension.TestTemplateInvocationContext | ||
* TestTemplateInvocationContext}, such as the current repetition in a | ||
* {@link RepeatedTest @RepeatedTest}. | ||
* | ||
* <p>If a method parameter is of type {@code int} or {@code Integer} and | ||
* annotated with {@code @InvocationIndex}, JUnit will supply the current | ||
* <em>invocation index</em> as the value for the parameter. | ||
* | ||
* @since 5.0 | ||
* @see RepeatedTest | ||
*/ | ||
@Target(ElementType.PARAMETER) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Documented | ||
@API(Experimental) | ||
public @interface InvocationIndex { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
...ne/src/main/java/org/junit/jupiter/engine/extension/InvocationIndexParameterResolver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* Copyright 2015-2017 the original author or authors. | ||
* | ||
* All rights reserved. This program and the accompanying materials are | ||
* made available under the terms of the Eclipse Public License v1.0 which | ||
* accompanies this distribution and is available at | ||
* | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
*/ | ||
|
||
package org.junit.jupiter.engine.extension; | ||
|
||
import static org.junit.platform.commons.util.AnnotationUtils.isAnnotated; | ||
|
||
import org.junit.jupiter.api.InvocationIndex; | ||
import org.junit.jupiter.api.extension.ExtensionContext; | ||
import org.junit.jupiter.api.extension.ParameterContext; | ||
import org.junit.jupiter.api.extension.ParameterResolver; | ||
|
||
/** | ||
* {@link ParameterResolver} that supports resolution of the current | ||
* <em>invocation index</em> for parameters annotated with {@code @InvocationIndex}. | ||
* | ||
* @since 5.0 | ||
*/ | ||
class InvocationIndexParameterResolver implements ParameterResolver { | ||
|
||
private final Integer invocationIndex; | ||
|
||
InvocationIndexParameterResolver(int invocationIndex) { | ||
this.invocationIndex = invocationIndex; | ||
} | ||
|
||
@Override | ||
public boolean supports(ParameterContext parameterContext, ExtensionContext extensionContext) { | ||
return isAnnotated(parameterContext.getParameter(), InvocationIndex.class); | ||
} | ||
|
||
@Override | ||
public Integer resolve(ParameterContext parameterContext, ExtensionContext extensionContext) { | ||
return this.invocationIndex; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters