Skip to content

Commit 3b81711

Browse files
authored
Update README.md
1 parent 9e68b50 commit 3b81711

File tree

1 file changed

+78
-0
lines changed

1 file changed

+78
-0
lines changed

README.md

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -594,6 +594,84 @@ public class ArtifactCollectorJUnitParams implements ArtifactParams {
594594
}
595595
```
596596

597+
The following example demonstrates how to publish invocation parameters with the [TestParameterInjector](https://github.com/google/TestParameterInjector) runner. The implementation is more complex than the `Parameterized` example above, due to the strategy used by the runner to inject parameters into the test methods.
598+
599+
The `TestParameterInjector` runner supports a variety of methods to inject parameters. Test parameters can be provided by test class fields, test method arguments, or both. When test method arguments are used, these are populated from a `parameters` list in a `testInfo` object that's been added to the **JUnit** framework method. Unlike the `Parameterized` runner, where all test methods in the class run with the same set of values, the `TestParameterInjector` runner allows each test method in the class to run with its own unique set of values. This variability must be accounted for in the implementation of the `getParameters()` method.
600+
601+
The example below queries the method for its parameter types, then uses these to cast each injected value to its corresponding type.
602+
603+
###### Publishing Invocation Parameters - `TestParameterInjector` Runner
604+
```java
605+
package com.nordstrom.example;
606+
607+
import static org.junit.Assert.assertEquals;
608+
609+
import java.util.Map;
610+
import org.junit.Test;
611+
import org.junit.runner.RunWith;
612+
import org.junit.internal.runners.model.ReflectiveCallable;
613+
import org.junit.runners.model.FrameworkMethod;
614+
import com.google.testing.junit.testparameterinjector.TestParameterInjector;
615+
import com.google.testing.junit.testparameterinjector.TestParameters;
616+
import com.nordstrom.automation.junit.ArtifactParams;
617+
import com.nordstrom.automation.junit.AtomIdentity;
618+
import com.google.common.base.Optional;
619+
620+
@RunWith(TestParameterInjector.class)
621+
public class ArtifactCollectorParamInjector implements ArtifactParams {
622+
623+
@Rule
624+
public final AtomIdentity identity = new AtomIdentity(this);
625+
626+
@Override
627+
public AtomIdentity getAtomIdentity() {
628+
return identity;
629+
}
630+
631+
@Override
632+
public Description getDescription() {
633+
return identity.getDescription();
634+
}
635+
636+
@Override
637+
public Optional<Map<String, Object>> getParameters() {
638+
AtomicTest atomicTest = LifecycleHooks.getAtomicTestOf(this);
639+
FrameworkMethod method = atomicTest.getIdentity();
640+
641+
// get test method parameters
642+
Class<?>[] paramTypes = method.getMethod().getParameterTypes();
643+
644+
try {
645+
Object testInfo = LifecycleHooks.getFieldValue(method, "testInfo");
646+
List<Object> params = LifecycleHooks.getFieldValue(testInfo, "parameters");
647+
648+
// allocate named parameters array
649+
Param[] namedParams = new Param[params.size()];
650+
// populate named parameters array
651+
for (int i = 0; i < params.size(); i++) {
652+
Object param = params.get(i);
653+
Map<String, Object> paramValue = LifecycleHooks.getFieldValue(param, "value");
654+
Entry<String, Object> paramEntry = paramValue.entrySet().iterator().next();
655+
namedParams[i] = Param.param(paramEntry.getKey(), paramTypes[i].cast(paramEntry.getValue()));
656+
}
657+
658+
// return params map as Optional
659+
return Param.mapOf(namedParams);
660+
} catch (IllegalAccessException | NoSuchFieldException | SecurityException e) {
661+
return Optional.empty();
662+
}
663+
}
664+
665+
@Test
666+
@TestParameters("{input: 'first test'}")
667+
@TestParameters("{input: 'second test'}")
668+
public void parameterized(String input) {
669+
System.out.println("parameterized: input = [" + input + "]");
670+
assertEquals("first test", input);
671+
}
672+
}
673+
```
674+
597675
#### Artifact capture for parameterized tests
598676

599677
For scenarios that require artifact capture of parameterized tests, the [ArtifactCollector](https://github.com/sbabcoc/JUnit-Foundation/blob/master/src/main/java/com/nordstrom/automation/junit/ArtifactCollector.java) class extends the [AtomIdentity](https://github.com/sbabcoc/JUnit-Foundation/blob/master/src/main/java/com/nordstrom/automation/junit/AtomIdentity.java) test rule. This enables artifact type implementations to access invocation parameters and **Description** object for the current `atomic test`. For more details, see the [Artifact Capture](#artifact-capture) section below.

0 commit comments

Comments
 (0)