下面列出了org.junit.runner.Request#filterWith() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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);
}
}
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);
}
}
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);
}
}
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);
}
}
@Override
public void run() throws Throwable {
Level stdOutLogLevel = Level.INFO;
Level stdErrLogLevel = Level.WARNING;
String unparsedStdOutLogLevel = System.getProperty(STD_OUT_LOG_LEVEL_PROPERTY);
String unparsedStdErrLogLevel = System.getProperty(STD_ERR_LOG_LEVEL_PROPERTY);
if (unparsedStdOutLogLevel != null) {
stdOutLogLevel = Level.parse(unparsedStdOutLogLevel);
}
if (unparsedStdErrLogLevel != null) {
stdErrLogLevel = Level.parse(unparsedStdErrLogLevel);
}
for (String className : testClassNames) {
Class<?> testClass = Class.forName(className);
List<TestResult> results = new ArrayList<>();
RecordingFilter filter = new RecordingFilter();
if (mightBeATestClass(testClass)) {
JUnitCore jUnitCore = new JUnitCore();
Runner suite = new Computer().getSuite(createRunnerBuilder(), new Class<?>[] {testClass});
Request request = Request.runner(suite);
request = request.filterWith(filter);
jUnitCore.addListener(new TestListener(results, stdOutLogLevel, stdErrLogLevel));
jUnitCore.run(request);
}
// Combine the results with the tests we filtered out
List<TestResult> actualResults = combineResults(results, filter.filteredOut);
writeResult(className, actualResults);
}
}