Skip to content

Commit 87571b7

Browse files
author
Brian Muramatsu
committed
Try Alternate Constructor for Single Method Tests
Some tests do not have a no argument constructor. If they don't have one, then try a constructor with a String argument. A lot of CTS tests from open source projects have different practices and may not have a no arg constructor. Change-Id: I87c490c22347a2f4b03c3125308be0d2259f9208
1 parent 5eeee5e commit 87571b7

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed

test-runner/src/android/test/AndroidTestRunner.java

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import junit.framework.TestSuite;
2929
import junit.runner.BaseTestRunner;
3030

31+
import java.lang.reflect.Constructor;
3132
import java.lang.reflect.InvocationTargetException;
3233
import java.util.List;
3334

@@ -91,15 +92,35 @@ private Class<? extends Test> loadTestClass(String testClassName) {
9192

9293
private TestCase buildSingleTestMethod(Class testClass, String testMethodName) {
9394
try {
94-
TestCase testCase = (TestCase) testClass.newInstance();
95+
Constructor c = testClass.getConstructor();
96+
return newSingleTestMethod(testClass, testMethodName, c);
97+
} catch (NoSuchMethodException e) {
98+
}
99+
100+
try {
101+
Constructor c = testClass.getConstructor(String.class);
102+
return newSingleTestMethod(testClass, testMethodName, c, testMethodName);
103+
} catch (NoSuchMethodException e) {
104+
}
105+
106+
return null;
107+
}
108+
109+
private TestCase newSingleTestMethod(Class testClass, String testMethodName,
110+
Constructor constructor, Object... args) {
111+
try {
112+
TestCase testCase = (TestCase) constructor.newInstance(args);
95113
testCase.setName(testMethodName);
96114
return testCase;
97115
} catch (IllegalAccessException e) {
98116
runFailed("Could not access test class. Class: " + testClass.getName());
99117
} catch (InstantiationException e) {
100118
runFailed("Could not instantiate test class. Class: " + testClass.getName());
119+
} catch (IllegalArgumentException e) {
120+
runFailed("Illegal argument passed to constructor. Class: " + testClass.getName());
121+
} catch (InvocationTargetException e) {
122+
runFailed("Constructor thew an exception. Class: " + testClass.getName());
101123
}
102-
103124
return null;
104125
}
105126

0 commit comments

Comments
 (0)