下面列出了怎么用org.junit.runner.notification.RunNotifier的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
protected void runChild(TestMapping[] childArrayElement, RunNotifier notifier) {
notifier.fireTestStarted(testDescription);
Arrays.stream(childArrayElement).forEach(thisChild -> {
loadProcessor.addTest(thisChild.testClass(), thisChild.testMethod());
});
boolean hasFailed = loadProcessor.processMultiLoad();
if (hasFailed) {
String failureMessage = testClass.getName() + " with load/stress test(s): " + testDescription + " have Failed";
LOGGER.error(failureMessage + ". See target/logs -or- junit granular failure report(csv) -or- fuzzy search and filter report(html) for details");
notifier.fireTestFailure(new Failure(testDescription, new RuntimeException(failureMessage)));
}
notifier.fireTestFinished(testDescription);
}
@Override
protected Statement classBlock(RunNotifier notifier) {
final Statement suite = super.classBlock(notifier);
return new Statement() {
@Override
public void evaluate() throws Throwable {
TestEnvironment testEnvironment = null;
try {
testEnvironment = GlobalTestEnvironment.getOrCreate(InProcessTestEnvironment::new);
suite.evaluate();
} finally {
if (testEnvironment != null) {
testEnvironment.stop();
}
if (driverSupplier instanceof Closeable) {
((Closeable) driverSupplier).close();
}
}
}
};
}
@Override
public void runChild(final FrameworkMethod method, final RunNotifier notifier) {
final Description description = describeChild(method);
if (this.isIgnored(method)) {
notifier.fireTestIgnored(description);
} else {
final EachTestNotifier eachNotifier = new EachTestNotifier(notifier, description);
eachNotifier.fireTestStarted();
boolean ignored = false;
try {
this.methodBlock(method).evaluate();
} catch (AssumptionViolatedException ave) {
eachNotifier.addFailedAssumption(ave);
} catch (Throwable e) {
if (validateForGraphComputer(e)) {
eachNotifier.fireTestIgnored();
logger.info(e.getMessage());
ignored = true;
} else
eachNotifier.addFailure(e);
} finally {
if (!ignored)
eachNotifier.fireTestFinished();
}
}
}
@Override
@SneakyThrows
public void run(RunNotifier notifier) {
System.out.println("running the tests from SmartAssert: " + test);
Object testObject = test.newInstance();
for (Method method : test.getMethods()) {
if (!method.isAnnotationPresent(Test.class)) continue;
notifier.fireTestStarted(description(method));
method.invoke(testObject);
checkResult(failures(test.getName()));
notifier.fireTestFinished(description(method));
}
}
private static void runInternal(final RunNotifier notifier) {
if (openssl && OPENSSL_FAILURE != null) {
throw new RuntimeException(OPENSSL_FAILURE);
}
if (first) {
first = false;
undertow = Undertow.builder()
.setHandler(rootHandler)
.addHttpListener(getHostPort(DEFAULT), getHostAddress(DEFAULT))
.build();
undertow.start();
notifier.addListener(new RunListener() {
@Override
public void testRunFinished(Result result) throws Exception {
super.testRunFinished(result);
undertow.stop();
clientGroup.shutdownGracefully();
}
});
}
}
@Override
protected void runChild(final FrameworkMethod method, final RunNotifier notifier)
{
BrokerSpecific brokerSpecific = method.getAnnotation(BrokerSpecific.class);
if (brokerSpecific == null)
{
brokerSpecific = method.getDeclaringClass().getAnnotation(BrokerSpecific.class);
}
if (brokerSpecific != null && !brokerSpecific.kind().equalsIgnoreCase(_brokerAdmin.getKind()))
{
notifier.fireTestIgnored(describeChild(method));
}
else
{
_brokerAdmin.beforeTestMethod(_testClass, method.getMethod());
try
{
super.runChild(method, notifier);
}
finally
{
_brokerAdmin.afterTestMethod(_testClass, method.getMethod());
}
}
}
/**
* Before to delegate to the {@linkplain BlockJUnit4ClassRunner#runChild default implementation},
* check if a dependency of the given method failed. In such case, the test will be ignored.
*
* @param method the test method to execute.
* @param notifier the object to notify about test results.
*/
@Override
protected void runChild(final FrameworkMethod method, final RunNotifier notifier) {
if (skipAll) {
notifier.fireTestIgnored(describeChild(method));
return;
}
if (methodDependencyFailures != null) {
final DependsOnMethod assumptions = method.getAnnotation(DependsOnMethod.class);
if (assumptions != null) {
for (final String assumption : assumptions.value()) {
if (methodDependencyFailures.contains(assumption)) {
methodDependencyFailures.add(method.getName());
notifier.fireTestIgnored(describeChild(method));
return;
}
}
}
}
super.runChild(method, notifier);
}
/**
* Runs the test corresponding to {@code child}, which can be assumed to be
* an element of the list returned by {@link ParentRunner#getChildren()}.
* Subclasses are responsible for making sure that relevant test events are
* reported through {@code notifier}
*
* @param child
* @param notifier
*/
@Override
protected void runChild(ScenarioSpec child, RunNotifier notifier) {
final Description description = Description.createTestDescription(testClass, child.getScenarioName());
// ----------------------------------------------
// Notify that this single test has been started.
// Supply the scenario/journey name
// ----------------------------------------------
notifier.fireTestStarted(description);
passed = zeroCodeMultiStepsScenarioRunner.runScenario(child, notifier, description);
testRunCompleted = true;
if (passed) {
LOGGER.info(String.format("\nPackageRunner- **FINISHED executing all Steps for [%s] **.\nSteps were:%s",
child.getScenarioName(),
child.getSteps().stream().map(step -> step.getName()).collect(Collectors.toList())));
}
notifier.fireTestFinished(description);
}
private void invokeTest(RunNotifier notifier, TestExecutorType type, FrameworkMethod method) {
if ((type == null) || TestExecutorType.LOCAL.equals(type)) {
runLeaf(methodBlock(method), describeChild(method), notifier);
} else {
EachTestNotifier eachNotifier = new EachTestNotifier(notifier, describeChild(method));
try {
eachNotifier.fireTestStarted();
execute(type, method.getMethod()).get();
} catch (Throwable e) {
log.error("Test " + method + " failed with exception ", e);
eachNotifier.addFailure(unwrap(e));
} finally {
eachNotifier.fireTestFinished();
}
}
}
@Override
public void run(RunNotifier notifier) {
System.out.println("running the tests from MyRunner. " + testClass);
try {
Object testObject = testClass.newInstance();
for (Method method : testClass.getMethods()) {
if (method.isAnnotationPresent(Test.class)) {
notifier.fireTestStarted(Description.EMPTY);
method.invoke(testObject);
notifier.fireTestFinished(Description.EMPTY);
}
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
private Boolean executeRetryWithSteps(RunNotifier notifier,
Description description,
ScenarioExecutionState scenarioExecutionState,
ScenarioSpec scenario, Step thisStep) {
thisStep = extFileProcessor.resolveExtJsonFile(thisStep);
List<Step> thisSteps = extFileProcessor.createFromStepFile(thisStep, thisStep.getId());
if(null == thisSteps || thisSteps.isEmpty()) thisSteps.add(thisStep);
Boolean wasExecSuccess = null;
for(Step step : thisSteps) {
wasExecSuccess = executeRetry(notifier,
description,
scenarioExecutionState,
scenario,
step);
if (wasExecSuccess != null) {
return wasExecSuccess;
}
}
return null;
}
@Override
public void run(RunNotifier notifier) {
RunListener listener = new RunListener() {
WarningsCollector warningsCollector;
@Override
public void testStarted(Description description) throws Exception {
warningsCollector = new WarningsCollector();
}
@Override public void testFailure(Failure failure) throws Exception {
logger.log(warningsCollector.getWarnings());
}
};
notifier.addListener(listener);
runner.run(notifier);
}
@Override
protected void runChild(Spec spec, RunNotifier notifier) {
List<Spec> specs = spec.getSuite().getSpecs();
boolean suiteHasNoSpecs = specs.isEmpty();
boolean isFirstSpec = specs.indexOf(spec) == 0;
boolean isLastSpec = specs.indexOf(spec) == specs.size() -1;
if (suiteHasNoSpecs || isFirstSpec){
runBeforeCallbacks(spec);
}
if (spec.getBlock().isPresent()) {
runBeforeEachCallbacks(spec);
runLeaf(spec, describeChild(spec), notifier);
runAfterEachCallbacks(spec);
} else {
notifier.fireTestIgnored(describeChild(spec));
}
if (suiteHasNoSpecs || isLastSpec){
runAfterCallbacks(spec);
}
}
@Override
protected void runChild(FrameworkMethod method, RunNotifier notifier) {
synchronized (invokeInRender) {
// add for invoking in render phase, where gl context is available
invokeInRender.put(method, notifier);
}
// wait until that test was invoked
waitUntilInvokedInRenderMethod();
}
@Override
protected void runChild(final FrameworkMethod method, RunNotifier notifier) {
Description description = describeChild(method);
if (isIgnored(method)) {
notifier.fireTestIgnored(description);
} else {
runLeaf2(methodBlock(method), description, notifier);
}
}
@Test
public void testClassWithPersistenceContextWithKonfiguredUnitNameSpecified() throws Exception {
// GIVEN
final JCodeModel jCodeModel = new JCodeModel();
final JPackage jp = jCodeModel.rootPackage();
final JDefinedClass jClass = jp._class(JMod.PUBLIC, "ClassUnderTest");
final JFieldVar ruleField = jClass.field(JMod.PUBLIC, JpaUnitRule.class, "rule");
ruleField.annotate(Rule.class);
final JInvocation instance = JExpr._new(jCodeModel.ref(JpaUnitRule.class)).arg(JExpr.direct("getClass()"));
ruleField.init(instance);
final JFieldVar emField = jClass.field(JMod.PRIVATE, EntityManager.class, "em");
final JAnnotationUse jAnnotation = emField.annotate(PersistenceContext.class);
jAnnotation.param("unitName", "test-unit-1");
final JMethod jMethod = jClass.method(JMod.PUBLIC, jCodeModel.VOID, "testMethod");
jMethod.annotate(Test.class);
buildModel(testFolder.getRoot(), jCodeModel);
compileModel(testFolder.getRoot());
final Class<?> cut = loadClass(testFolder.getRoot(), jClass.name());
final BlockJUnit4ClassRunner runner = new BlockJUnit4ClassRunner(cut);
final RunListener listener = mock(RunListener.class);
final RunNotifier notifier = new RunNotifier();
notifier.addListener(listener);
// WHEN
runner.run(notifier);
// THEN
final ArgumentCaptor<Description> descriptionCaptor = ArgumentCaptor.forClass(Description.class);
verify(listener).testStarted(descriptionCaptor.capture());
assertThat(descriptionCaptor.getValue().getClassName(), equalTo("ClassUnderTest"));
assertThat(descriptionCaptor.getValue().getMethodName(), equalTo("testMethod"));
verify(listener).testFinished(descriptionCaptor.capture());
assertThat(descriptionCaptor.getValue().getClassName(), equalTo("ClassUnderTest"));
assertThat(descriptionCaptor.getValue().getMethodName(), equalTo("testMethod"));
}
/**
* Generates a list of tests to run by recursing over the {@link Description} hierarchy and firing
* events to simulate the tests being run successfully.
*
* @param runNotifier the notifier to which the events are sent.
* @param description the description to traverse.
*/
private void generateListOfTests(RunNotifier runNotifier, Description description) {
List<Description> children = description.getChildren();
if (children.isEmpty()) {
runNotifier.fireTestStarted(description);
runNotifier.fireTestFinished(description);
} else {
for (Description child : children) {
generateListOfTests(runNotifier, child);
}
}
}
private RunNotifier runWithNotifier(Class<?> clazz) {
RunNotifier notifier = mock(RunNotifier.class);
try {
new BlockJUnit4ClassRunner(clazz).run(notifier);
} catch (InitializationError initializationError) {
throw new RuntimeException("Cannot initialize test: " + initializationError.getMessage(),
initializationError);
}
return notifier;
}
@Override
protected Statement classBlock(RunNotifier notifier) {
if (SystemProperties.TEST_CODE_EXECUTING_IN_NEW_JVM.evaluate()) {
return QUICK_PERF_SPRING_RUNNER_FOR_SPECIFIC_JVM.classBlock(notifier);
}
return super.classBlock(notifier);
}
private void runChildren(final RunNotifier notifier) {
final RunnerScheduler currentScheduler = scheduler;
try {
for (final FrameworkMethod each : getFilteredChildren()) {
currentScheduler.schedule(new Runnable() {
public void run() {
runChild(each, notifier);
}
});
}
} finally {
currentScheduler.finished();
}
}
@Override
protected Statement classBlock(final RunNotifier notifier) {
Statement statement = childrenInvoker(notifier);
statement = withBeforeParams(statement);
statement = withAfterParams(statement);
return statement;
}
@Override
public void run(RunNotifier notifier) {
try {
super.run(notifier);
} finally {
eventstore.shutdown();
}
}
protected Statement classBlock(final RunNotifier notifier) {
Statement statement = childrenInvoker(notifier);
if (!areAllChildrenIgnored()) {
statement = withBeforeClasses(statement);
statement = withAfterClasses(statement);
statement = withClassRules(statement);
}
return statement;
}
@Override
public void run(RunNotifier runNotifier)
{
CdiContainer container = CdiContainerLoader.getCdiContainer();
if (!containerStarted)
{
container.boot(CdiTestSuiteRunner.getTestContainerConfig());
containerStarted = true;
}
super.run(runNotifier);
}
@Override
public void run(final RunNotifier notifier) {
notifier.fireTestStarted(suiteDescription);
try {
delegate.run(notifier);
}
catch (Throwable e) { // NOSONAR
Failure failure = new Failure(suiteDescription, e);
notifier.fireTestFailure(failure);
captureLogsOnFailure(failure);
}
finally {
notifier.fireTestFinished(suiteDescription);
}
}
@Override
public void run( final RunNotifier notifier )
{
for ( final ScriptTestInstance file : this.testFiles )
{
for ( final ScriptTestMethod method : file.getTestMethods() )
{
final Description desc = Description.createTestDescription( file.getName(), method.getName() );
notifier.fireTestStarted( desc );
try
{
runBefore( file );
method.runTest( this.testInstance );
runAfter( file );
}
catch ( final Throwable e )
{
notifier.fireTestFailure( new Failure( desc, e ) );
}
finally
{
notifier.fireTestFinished( desc );
}
}
file.dispose();
}
}
/**
* Run the tests in the supplied {@code testClass}, using the specified
* {@link Runner}, and assert the expectations of the test execution.
*
* <p>If the specified {@code runnerClass} is {@code null}, the tests
* will be run with the runner that the test class is configured with
* (i.e., via {@link RunWith @RunWith}) or the default JUnit runner.
*
* @param runnerClass the explicit runner class to use or {@code null}
* if the default JUnit runner should be used
* @param testClass the test class to run with JUnit
* @param expectedStartedCount the expected number of tests that started
* @param expectedFailedCount the expected number of tests that failed
* @param expectedFinishedCount the expected number of tests that finished
* @param expectedIgnoredCount the expected number of tests that were ignored
* @param expectedAssumptionFailedCount the expected number of tests that
* resulted in a failed assumption
*/
public static void runTestsAndAssertCounters(Class<? extends Runner> runnerClass, Class<?> testClass,
int expectedStartedCount, int expectedFailedCount, int expectedFinishedCount, int expectedIgnoredCount,
int expectedAssumptionFailedCount) throws Exception {
TrackingRunListener listener = new TrackingRunListener();
if (runnerClass != null) {
Constructor<?> constructor = runnerClass.getConstructor(Class.class);
Runner runner = (Runner) BeanUtils.instantiateClass(constructor, testClass);
RunNotifier notifier = new RunNotifier();
notifier.addListener(listener);
runner.run(notifier);
}
else {
JUnitCore junit = new JUnitCore();
junit.addListener(listener);
junit.run(testClass);
}
// @formatter:off
assertAll(
() -> assertEquals(expectedStartedCount, listener.getTestStartedCount(), "tests started for [" + testClass + "]"),
() -> assertEquals(expectedFailedCount, listener.getTestFailureCount(), "tests failed for [" + testClass + "]"),
() -> assertEquals(expectedFinishedCount, listener.getTestFinishedCount(), "tests finished for [" + testClass + "]"),
() -> assertEquals(expectedIgnoredCount, listener.getTestIgnoredCount(), "tests ignored for [" + testClass + "]"),
() -> assertEquals(expectedAssumptionFailedCount, listener.getTestAssumptionFailureCount(), "failed assumptions for [" + testClass + "]")
);
// @formatter:on
}
@Override
protected Statement childrenInvoker(RunNotifier notifier) {
Statement statement = super.childrenInvoker(notifier);
statement = withParameterizedBeforeClasses(statement);
statement = withParameterizedAfterClasses(statement);
statement = withParameterizedClassRules(statement);
return statement;
}
public void run(RunNotifier notifier) {
for (JmesPathComplianceTest.ComplianceTest<T> complianceTest : getAllTests()) {
Description testDescription = createDescription(complianceTest);
notifier.fireTestStarted(testDescription);
try {
complianceTest.run();
} catch (AssertionError ae) {
notifier.fireTestFailure(new Failure(testDescription, ae));
} catch (Exception e) {
notifier.fireTestFailure(new Failure(testDescription, e));
} finally {
notifier.fireTestFinished(testDescription);
}
}
}
protected void reportResults(final RunNotifier notifier, final SpecificationReport spec, final Description test) {
switch (spec.getResult()) {
case SUCCESS:
notifier.fireTestFinished(test);
return;
case FAILURE:
notifier.fireTestFailure(new Failure(test, spec.getCause()));
return;
case ERROR:
default:
throw new SpecificationError(spec.getMessage(), spec.getCause());
}
}