下面列出了org.junit.runners.Suite#org.junit.runner.Runner 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public AbstractPinpointPluginTestSuite(Class<?> testClass) throws InitializationError, ArtifactResolutionException, DependencyResolutionException {
super(testClass, Collections.<Runner> emptyList());
PinpointAgent agent = testClass.getAnnotation(PinpointAgent.class);
this.agentJar = resolveAgentPath(agent);
PinpointConfig config = testClass.getAnnotation(PinpointConfig.class);
this.configFile = config == null ? null : resolveConfigFileLocation(config.value());
PinpointProfile profile = testClass.getAnnotation(PinpointProfile.class);
this.profile = resolveProfile(profile);
JvmArgument jvmArgument = testClass.getAnnotation(JvmArgument.class);
this.jvmArguments = getJvmArguments(jvmArgument);
JvmVersion jvmVersion = testClass.getAnnotation(JvmVersion.class);
this.jvmVersions = jvmVersion == null ? new int[] { NO_JVM_VERSION } : jvmVersion.value();
ImportPlugin importPlugin = testClass.getAnnotation(ImportPlugin.class);
this.importPluginIds = getImportPlugin(importPlugin);
this.requiredLibraries = getClassPathList(REQUIRED_CLASS_PATHS);
this.mavenDependencyLibraries = getClassPathList(MAVEN_DEPENDENCY_CLASS_PATHS);
this.testClassLocation = resolveTestClassLocation(testClass);
this.debug = isDebugMode();
}
public List<Runner> runners()
throws InitializationError, ParsingException, SAXException, IOException, IllegalArgumentException, SecurityException, IllegalAccessException, InvocationTargetException, NoSuchMethodException, InstantiationException {
AutomationEngine engine = AutomationEngine.getInstance();
ArrayList<Runner> tests = new ArrayList<Runner>();
engine.buildModel(new File("testlibrary"), new File("testscripts"));
TestAgenda testAgenda = engine.getTestAgenda();
for (TestScript testScript : testAgenda.getTestScripts()) {
TestCaseRunner theRunner = new TestCaseRunner(ParallelScriptRunner.class, new ParallelScriptRunner(testScript));
tests.add(theRunner);
ConcurrentScheduler.registerTestName(testScript.getName());
}
return tests;
}
@Override
public List<Class<?>> apply(final Class<?> a) {
try {
final Runner runner = AdaptedJUnitTestUnit.createRunner(a);
final List<Description> allChildren = new ArrayList<>();
flattenChildren(allChildren, runner.getDescription());
final List<Description> suites = FCollection.filter(allChildren,
Prelude.or(isSuiteMethodRunner(runner), isSuite()));
final Set<Class<?>> classes = suites.stream().flatMap(descriptionToTestClass()).collect(Collectors.toSet());
classes.remove(a);
return new ArrayList<>(classes);
} catch (final RuntimeException ex) {
// some runners (looking at you spock) can throw a runtime exception
// when the getDescription method is called.
return Collections.emptyList();
}
}
@Override
public Runner runnerForClass(Class<?> testClass) throws Throwable {
if (androidRunnerParams.isIgnoreSuiteMethods()) {
return null;
}
try {
if (hasSuiteMethod(testClass)) {
Test t = SuiteMethod.testFromSuiteMethod(testClass);
if (!(t instanceof TestSuite)) {
// this should not be possible
throw new IllegalArgumentException(
testClass.getName() + "#suite() did not return a TestSuite");
}
return new JUnit38ClassRunner(new AndroidTestSuite((TestSuite) t, androidRunnerParams));
}
} catch (Throwable e) {
// log error message including stack trace before throwing to help with debugging.
Log.e(LOG_TAG, "Error constructing runner", e);
throw e;
}
return null;
}
@Test
public void successfullyCreateAndroidRunner() throws Exception {
final Runner mockedRunner = mock(Runner.class);
AndroidAnnotatedBuilder ab =
new AndroidAnnotatedBuilder(mockRunnerBuilder, mockAndroidRunnerParams) {
@Override
public Runner buildAndroidRunner(Class<? extends Runner> runnerClass, Class<?> testClass)
throws Exception {
assertEquals(runnerClass, AndroidJUnit4.class);
assertEquals(testClass, RunWithAndroidJUnit4Class.class);
return mockedRunner;
}
};
// attempt to create a runner for a class annotated with @RunWith(AndroidJUnit4.class)
Runner runner = ab.runnerForClass(RunWithAndroidJUnit4Class.class);
assertEquals(0, runner.testCount());
}
private List<Runner> createRunnersForParameters(
Iterable<Object> allParameters, String namePattern,
ParametersRunnerFactory runnerFactory) throws Exception {
try {
List<TestWithParameters> tests = createTestsForParameters(
allParameters, namePattern);
List<Runner> runners = new ArrayList<Runner>();
for (TestWithParameters test : tests) {
runners.add(runnerFactory
.createRunnerForTestWithParameters(test));
}
return runners;
} catch (ClassCastException e) {
throw parametersMethodReturnedWrongType();
}
}
private void filterIfRequired(final ResultCollector rc, final Runner runner) {
if (this.filter.isPresent()) {
if (!(runner instanceof Filterable)) {
LOG.warning("Not able to filter " + runner.getDescription()
+ ". Mutation may have prevented JUnit from constructing test");
return;
}
final Filterable f = (Filterable) runner;
try {
f.filter(this.filter.get());
} catch (final NoTestsRemainException e1) {
rc.notifySkipped(this.getDescription());
return;
}
}
}
@Override
public List<TestUnit> findTestUnits(final Class<?> clazz) {
final Runner runner = AdaptedJUnitTestUnit.createRunner(clazz);
if (isExcluded(runner) || isNotARunnableTest(runner, clazz.getName()) || !isIncluded(clazz)) {
return Collections.emptyList();
}
if (Filterable.class.isAssignableFrom(runner.getClass())
&& !shouldTreatAsOneUnit(clazz, runner)) {
final List<TestUnit> filteredUnits = splitIntoFilteredUnits(runner.getDescription());
return filterUnitsByMethod(filteredUnits);
} else {
return Collections.<TestUnit> singletonList(new AdaptedJUnitTestUnit(
clazz, Optional.<Filter> empty()));
}
}
/**
* Confirms that each given name has a {@linkplain ReifiedParamRunner} associated with it, and returns the
* name -> runner map
* @param runner the parameterized runner
* @param names the expected names
* @return a map of names to reified runners
*/
private static Map<String,ReifiedParamRunner> testParameterizations(NamedParameterizedRunner runner, String... names)
{
List<Runner> children = runner.getChildren();
assertEquals("children.size()", names.length, children.size());
Set<String> expectedNames = new HashSet<>(names.length, 1.0f);
for (String name : names) {
assertTrue("unexpected error, duplicate name: " + name, expectedNames.add(name));
}
Map<String,ReifiedParamRunner> foundRunners = new HashMap<>();
for (Runner child : children)
{
ReifiedParamRunner reified = (ReifiedParamRunner)child;
String paramToString = reified.paramToString();
assertNull("duplicate name: " + paramToString, foundRunners.put(paramToString, reified));
}
for (String expected : expectedNames)
{
assertTrue("didn't find expected param: " + expected, foundRunners.containsKey(expected));
}
return foundRunners;
}
public static Runner createRunner(final Class<?> clazz) {
final RunnerBuilder builder = createRunnerBuilder();
try {
return builder.runnerForClass(clazz);
} catch (final Throwable ex) {
LOG.log(Level.SEVERE, "Error while creating runner for " + clazz, ex);
throw translateCheckedException(ex);
}
}
private boolean shouldTreatAsOneUnit(final Class<?> clazz, final Runner runner) {
final Set<Method> methods = Reflection.allMethods(clazz);
return runnerCannotBeSplit(runner)
|| hasAnnotation(methods, BeforeClass.class)
|| hasAnnotation(methods, AfterClass.class)
|| hasClassRuleAnnotations(clazz, methods);
}
@Test
public void testNoRunWith() throws Exception {
AndroidAnnotatedBuilder ab =
new AndroidAnnotatedBuilder(mockRunnerBuilder, mockAndroidRunnerParams) {
@Override
public Runner buildAndroidRunner(Class<? extends Runner> runnerClass, Class<?> testClass)
throws Exception {
Assert.fail(
"Should not attempt to build Android Runner no @RunWith " + "annotation is used");
return null;
}
};
// attempt to create a runner for a class with no @RunWith annotation
ab.runnerForClass(NoRunWithClass.class);
}
/**
* Only called reflectively. Do not use programmatically.
*/
public SpringJUnit4ParameterizedClassRunner(Class<?> klass) throws Throwable {
super(klass, Collections.<Runner>emptyList());
List<Object[]> parametersList = getParametersList(getTestClass());
for (int i = 0; i < parametersList.size(); i++)
runners.add(new TestClassRunnerForParameters(getTestClass().getJavaClass(), parametersList, i));
}
private boolean shouldRun(Filter filter, Runner each) {
if (filter.shouldRun(describeChild(each))) {
return true;
}
if (each instanceof PinpointPluginTestRunner) {
return ((PinpointPluginTestRunner) each).isAvaiable(filter);
}
return false;
}
private CtCodeSnippetStatement createTestSnippet(CtClass element, Class<? extends Runner> runner) {
CtCodeSnippetStatement e = getFactory().Core()
.createCodeSnippetStatement();
String val = " new "+runner.getCanonicalName()+"("
+ element.getQualifiedName()
+ ".class).run(new org.junit.runner.notification.RunNotifier() {\n"
+ " @Override\n"
+ " public void fireTestFailure(org.junit.runner.notification.Failure failure) {\n"
+ " if (failure.getException() instanceof RuntimeException) throw (RuntimeException)failure.getException(); \n"
+ " if (failure.getException() instanceof Error) throw (Error)failure.getException(); \n"
+ " throw new RuntimeException(failure.getException());\n"
+ " }\n" + " })";
e.setValue(val);
return e;
}
public void testMemoizesRunner() {
MemoizingRequest memoizingRequest = new MemoizingRequest(mockRequestDelegate);
Runner firstRunner = memoizingRequest.getRunner();
Runner secondRunner = memoizingRequest.getRunner();
assertThat(secondRunner).isSameInstanceAs(firstRunner);
verify(mockRequestDelegate).getRunner();
verifyNoMoreInteractions(mockRequestDelegate);
}
@Override
public Runner runnerForClass(Class<?> testClass) throws Throwable {
// Try the custom RunnerBuilder instances first.
for (RunnerBuilder customRunnerBuilder : customRunnerBuilders) {
Runner runner = customRunnerBuilder.safeRunnerForClass(testClass);
if (runner != null) {
return runner;
}
}
return super.runnerForClass(testClass);
}
@Override
public final synchronized Runner getRunner() {
if (runnerDelegate == null) {
runnerDelegate = createRunner(requestDelegate);
}
return runnerDelegate;
}
private void initExecutions() {
if (executions.isEmpty()) {
try {
Runner descriptionProvider = createRunnerFor(Arrays.asList(target), Collections.<Filter>emptyList());
templateDescription = descriptionProvider.getDescription();
} catch (InitializationError initializationError) {
throw UncheckedException.throwAsUncheckedException(initializationError);
}
createExecutions();
for (Execution execution : executions) {
execution.init(target, templateDescription);
}
}
}
public Live(Class<?> klass) throws InitializationError {
super(klass, Collections.<Runner>emptyList());
graph = testGraph(klass);
if (graph.manager() != null && graph.manager().checkConnection()) {
runners = createRunners(klass);
} else {
runners = Collections.emptyList();
}
}
private void runTestClass(String testClassName) throws ClassNotFoundException {
final Class<?> testClass = Class.forName(testClassName, false, applicationClassLoader);
Request request = Request.aClass(testClass);
if (options.hasCategoryConfiguration()) {
Transformer<Class<?>, String> transformer = new Transformer<Class<?>, String>() {
public Class<?> transform(final String original) {
try {
return applicationClassLoader.loadClass(original);
} catch (ClassNotFoundException e) {
throw new InvalidUserDataException(String.format("Can't load category class [%s].", original), e);
}
}
};
request = request.filterWith(new CategoryFilter(
CollectionUtils.collect(options.getIncludeCategories(), transformer),
CollectionUtils.collect(options.getExcludeCategories(), transformer)
));
}
if (!options.getIncludedTests().isEmpty()) {
request = request.filterWith(new MethodNameFilter(options.getIncludedTests()));
}
Runner runner = request.getRunner();
//In case of no matching methods junit will return a ErrorReportingRunner for org.junit.runner.manipulation.Filter.class.
//Will be fixed with adding class filters
if (!org.junit.runner.manipulation.Filter.class.getName().equals(runner.getDescription().getDisplayName())) {
RunNotifier notifier = new RunNotifier();
notifier.addListener(listener);
runner.run(notifier);
}
}
public Runner runnerForClass(Class<?> testClass) throws Throwable {
try {
return new BlockJUnit4ClassRunner(testClass);
} catch (Throwable t) {
//failed to instantiate BlockJUnitRunner. try deprecated JUnitRunner (for JUnit < 4.5)
try {
Class<Runner> runnerClass = (Class<Runner>) Thread.currentThread().getContextClassLoader().loadClass("org.junit.internal.runners.JUnit4ClassRunner");
final Constructor<Runner> constructor = runnerClass.getConstructor(Class.class);
return constructor.newInstance(testClass);
} catch (Throwable e) {
LoggerFactory.getLogger(getClass()).warn("Unable to load JUnit4 runner to calculate Ignored test cases", e);
}
}
return null;
}
private void runEnabledTests(RunNotifier nested) {
if (enabledTests.isEmpty()) {
return;
}
Runner runner;
try {
runner = createExecutionRunner();
} catch (Throwable t) {
runner = new CannotExecuteRunner(getDisplayName(), target, t);
}
try {
if (!disabledTests.isEmpty()) {
((Filterable) runner).filter(new Filter() {
@Override
public boolean shouldRun(Description description) {
return !disabledTests.contains(description);
}
@Override
public String describe() {
return "disabled tests";
}
});
}
} catch (NoTestsRemainException e) {
return;
}
runner.run(nested);
}
private void runTestClass(String testClassName) throws ClassNotFoundException {
final Class<?> testClass = Class.forName(testClassName, true, applicationClassLoader);
Request request = Request.aClass(testClass);
if (options.hasCategoryConfiguration()) {
Transformer<Class<?>, String> transformer = new Transformer<Class<?>, String>() {
public Class<?> transform(final String original) {
try {
return applicationClassLoader.loadClass(original);
} catch (ClassNotFoundException e) {
throw new InvalidUserDataException(String.format("Can't load category class [%s].", original), e);
}
}
};
request = request.filterWith(new CategoryFilter(
CollectionUtils.collect(options.getIncludeCategories(), transformer),
CollectionUtils.collect(options.getExcludeCategories(), transformer)
));
}
if (!options.getIncludedTests().isEmpty()) {
request = request.filterWith(new MethodNameFilter(options.getIncludedTests()));
}
Runner runner = request.getRunner();
//In case of no matching methods junit will return a ErrorReportingRunner for org.junit.runner.manipulation.Filter.class.
//Will be fixed with adding class filters
if (!org.junit.runner.manipulation.Filter.class.getName().equals(runner.getDescription().getDisplayName())) {
RunNotifier notifier = new RunNotifier();
notifier.addListener(listener);
runner.run(notifier);
}
}
List<Description> getAllDescriptions(Description description, String className) {
final AllExceptIgnoredTestRunnerBuilder allExceptIgnoredTestRunnerBuilder = new AllExceptIgnoredTestRunnerBuilder();
try {
final Class<?> testClass = description.getClass().getClassLoader().loadClass(className);
Runner runner = allExceptIgnoredTestRunnerBuilder.runnerForClass(testClass);
if (runner == null) {
//fall back to default runner
runner = Request.aClass(testClass).getRunner();
}
final Description runnerDescription = runner.getDescription();
return runnerDescription.getChildren();
} catch (Throwable throwable) {
throw new TestSuiteExecutionException(String.format("Unable to process Ignored class %s.", className), throwable);
}
}
public Runner runnerForClass(Class<?> testClass) throws Throwable {
try {
return new BlockJUnit4ClassRunner(testClass);
} catch (Throwable t) {
//failed to instantiate BlockJUnitRunner. try deprecated JUnitRunner (for JUnit < 4.5)
try {
Class<Runner> runnerClass = (Class<Runner>) Thread.currentThread().getContextClassLoader().loadClass("org.junit.internal.runners.JUnit4ClassRunner");
final Constructor<Runner> constructor = runnerClass.getConstructor(Class.class);
return constructor.newInstance(testClass);
} catch (Throwable e) {
LoggerFactory.getLogger(getClass()).warn("Unable to load JUnit4 runner to calculate Ignored test cases", e);
}
}
return null;
}
@Override
protected List<Runner> getChildren() {
for (Runner runner : super.getChildren()) {
BlockJUnit4ClassRunner classRunner = (BlockJUnit4ClassRunner) runner;
classRunner.setScheduler(scheduler);
}
return super.getChildren();
}
@Test
public void builderHandlesJUnit3Tests() throws Throwable {
Runner selectedRunner = androidLogOnlyBuilder.runnerForClass(JUnit3FailingTestCase.class);
assertThat(selectedRunner, notNullValue());
assertThat(selectedRunner.getClass(), typeCompatibleWith(JUnit38ClassRunner.class));
runWithRunner(selectedRunner, 1, 0);
}
public Runner runnerForClass(Class<?> testClass) throws Throwable {
try {
return new BlockJUnit4ClassRunner(testClass);
} catch (Throwable t) {
//failed to instantiate BlockJUnitRunner. try deprecated JUnitRunner (for JUnit < 4.5)
try {
Class<Runner> runnerClass = (Class<Runner>) Thread.currentThread().getContextClassLoader().loadClass("org.junit.internal.runners.JUnit4ClassRunner");
final Constructor<Runner> constructor = runnerClass.getConstructor(Class.class);
return constructor.newInstance(testClass);
} catch (Throwable e) {
LoggerFactory.getLogger(getClass()).warn("Unable to load JUnit4 runner to calculate Ignored test cases", e);
}
}
return null;
}
/**
* Runs the guided fuzzing loop for a resolved class.
*
* <p>The test class must be annotated with <tt>@RunWith(JQF.class)</tt>
* and the test method must be annotated with <tt>@Fuzz</tt>.</p>
*
* <p>Once this method is invoked, the guided fuzzing loop runs continuously
* until the guidance instance decides to stop by returning <tt>false</tt>
* for {@link Guidance#hasInput()}. Until the fuzzing stops, this method
* cannot be invoked again (i.e. at most one guided fuzzing can be running
* at any time in a single JVM instance).</p>
*
* @param testClass the test class containing the test method
* @param testMethod the test method to execute in the fuzzing loop
* @param guidance the fuzzing guidance
* @param out an output stream to log Junit messages
* @throws IllegalStateException if a guided fuzzing run is currently executing
* @return the Junit-style test result
*/
public synchronized static Result run(Class<?> testClass, String testMethod,
Guidance guidance, PrintStream out) throws IllegalStateException {
// Ensure that the class uses the right test runner
RunWith annotation = testClass.getAnnotation(RunWith.class);
if (annotation == null || !annotation.value().equals(JQF.class)) {
throw new IllegalArgumentException(testClass.getName() + " is not annotated with @RunWith(JQF.class)");
}
// Set the static guided instance
setGuidance(guidance);
// Register callback
SingleSnoop.setCallbackGenerator(guidance::generateCallBack);
// Create a JUnit Request
Request testRequest = Request.method(testClass, testMethod);
// Instantiate a runner (may return an error)
Runner testRunner = testRequest.getRunner();
// Start tracing for the test method
SingleSnoop.startSnooping(testClass.getName() + "#" + testMethod);
// Run the test and make sure to de-register the guidance before returning
try {
JUnitCore junit = new JUnitCore();
if (out != null) {
junit.addListener(new TextListener(out));
}
return junit.run(testRunner);
} finally {
unsetGuidance();
}
}