Skip to content

Commit 20d6c0e

Browse files
committed
Map target to runner, not test class; intercept class runner sub-types
1 parent e8838f4 commit 20d6c0e

File tree

2 files changed

+15
-22
lines changed

2 files changed

+15
-22
lines changed

src/main/java/com/nordstrom/automation/junit/LifecycleHooks.java

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public static ClassFileTransformer installTransformer(Instrumentation instrument
8686
builder.method(named("createTestClass")).intercept(MethodDelegation.to(CreateTestClass.class))
8787
.method(named("run")).intercept(MethodDelegation.to(Run.class))
8888
.implement(Hooked.class))
89-
.type(is(blockJUnit4ClassRunner))
89+
.type(isSubTypeOf(blockJUnit4ClassRunner))
9090
.transform((builder, type, classLoader, module) ->
9191
builder.method(named("createTest")).intercept(MethodDelegation.to(CreateTest.class))
9292
.method(named("runChild")).intercept(MethodDelegation.to(RunChild.class))
@@ -196,7 +196,7 @@ static Object getParentOf(Object child) {
196196
public static class CreateTest {
197197

198198
private static final ServiceLoader<TestObjectWatcher> objectWatcherLoader;
199-
private static final Map<Object, TestClass> TARGET_TO_TESTCLASS = new ConcurrentHashMap<>();
199+
private static final Map<Object, Object> TARGET_TO_RUNNER = new ConcurrentHashMap<>();
200200

201201
static {
202202
objectWatcherLoader = ServiceLoader.load(TestObjectWatcher.class);
@@ -214,41 +214,37 @@ public static class CreateTest {
214214
public static Object intercept(@This final Object runner,
215215
@SuperCall final Callable<?> proxy) throws Exception {
216216
Object testObj = callProxy(proxy);
217-
TARGET_TO_TESTCLASS.put(testObj, getTestClassOf(runner));
217+
TARGET_TO_RUNNER.put(testObj, runner);
218218
applyTimeout(testObj);
219219

220220
synchronized(objectWatcherLoader) {
221221
for (TestObjectWatcher watcher : objectWatcherLoader) {
222-
watcher.testObjectCreated(testObj, TARGET_TO_TESTCLASS.get(testObj));
222+
watcher.testObjectCreated(testObj, runner);
223223
}
224224
}
225225

226226
return testObj;
227227
}
228228

229229
/**
230-
* Get the test class object that wraps the specified instance.
230+
* Get the class runner associated with the specified instance.
231231
*
232232
* @param target instance of JUnit test class
233-
* @return {@link TestClass} associated with specified instance
233+
* @return {@link org.junit.runners.BlockJUnit4ClassRunner BlockJUnit4ClassRunner} for specified instance
234234
*/
235-
static TestClass getTestClassFor(Object target) {
236-
TestClass testClass = TARGET_TO_TESTCLASS.get(target);
237-
if (testClass != null) {
238-
return testClass;
239-
}
240-
throw new IllegalArgumentException("No associated test class was found for specified instance");
235+
static Object getRunnerFor(Object target) {
236+
return TARGET_TO_RUNNER.get(target);
241237
}
242238
}
243239

244240
/**
245-
* Get the test class object that wraps the specified instance.
241+
* Get the class runner associated with the specified instance.
246242
*
247243
* @param target instance of JUnit test class
248-
* @return {@link TestClass} associated with specified instance object
244+
* @return {@link org.junit.runners.BlockJUnit4ClassRunner BlockJUnit4ClassRunner} for specified instance
249245
*/
250-
public static TestClass getTestClassFor(Object target) {
251-
return CreateTest.getTestClassFor(target);
246+
public static Object getRunnerFor(Object target) {
247+
return CreateTest.getRunnerFor(target);
252248
}
253249

254250
/**
@@ -310,8 +306,7 @@ public static boolean hasConfiguration(TestClass testClass) {
310306
* @return {@link Description} object for the indicated child
311307
*/
312308
public static Description describeChild(Object target, Object child) {
313-
TestClass testClass = getTestClassFor(target);
314-
Object runner = getRunnerFor(testClass);
309+
Object runner = getRunnerFor(target);
315310
return invoke(runner, "describeChild", child);
316311
}
317312

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package com.nordstrom.automation.junit;
22

3-
import org.junit.runners.model.TestClass;
4-
53
/**
64
* This interface defines the methods implemented by JUnit test object watchers. These watchers are registered via a
75
* ServiceLoader provider configuration file.
@@ -12,8 +10,8 @@ public interface TestObjectWatcher {
1210
* Invoked when a test class instance gets created.
1311
*
1412
* @param testObj test class instance that was just created
15-
* @param testClass {@link TestClass} object that owns this test class instance
13+
* @param runner JUnit runner for this test class instance
1614
*/
17-
void testObjectCreated(Object testObj, TestClass testClass);
15+
void testObjectCreated(Object testObj, Object runner);
1816

1917
}

0 commit comments

Comments
 (0)