Skip to content

Commit

Permalink
#40: Report skipped and aborted tests correctly
Browse files Browse the repository at this point in the history
------------------------------------------------------------------------
On behalf of the community, the JUnit Lambda Team thanks
msg systems ag (http://www.msg-systems.com) for supporting
the JUnit crowdfunding campaign!
------------------------------------------------------------------------
  • Loading branch information
marcphilipp committed Jan 4, 2016
1 parent 19153fa commit f767d67
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 2 deletions.
Expand Up @@ -10,6 +10,7 @@

package org.junit.gen5.engine;

import static java.util.function.Predicate.isEqual;
import static org.assertj.core.api.Assertions.allOf;
import static org.junit.gen5.commons.util.FunctionUtils.where;
import static org.junit.gen5.engine.ExecutionEvent.*;
Expand Down Expand Up @@ -47,12 +48,20 @@ public static Condition<ExecutionEvent> uniqueIdSubstring(String uniqueIdSubstri
"has descriptor with uniqueId substring '%s'", uniqueIdSubstring);
}

public static Condition<ExecutionEvent> skippedWithReason(String expectedReason) {
return allOf(type(SKIPPED), reason(expectedReason));
}

public static Condition<ExecutionEvent> started() {
return type(STARTED);
}

public static Condition<ExecutionEvent> finishedWithFailure(Condition<TestExecutionResult> causeMessage) {
return finished(allOf(status(FAILED), causeMessage));
public static Condition<ExecutionEvent> abortedWithReason(Condition<TestExecutionResult> resultCondition) {
return finished(allOf(status(ABORTED), resultCondition));
}

public static Condition<ExecutionEvent> finishedWithFailure(Condition<TestExecutionResult> resultCondition) {
return finished(allOf(status(FAILED), resultCondition));
}

public static Condition<ExecutionEvent> finishedSuccessfully() {
Expand All @@ -72,4 +81,9 @@ public static Condition<ExecutionEvent> result(Condition<TestExecutionResult> co
condition);
}

public static Condition<ExecutionEvent> reason(String expectedReason) {
return new Condition<>(byPayload(String.class, isEqual(expectedReason)), "event with reason '%s'",
expectedReason);
}

}
Expand Up @@ -20,6 +20,7 @@
import org.junit.gen5.engine.EngineAwareTestDescriptor;
import org.junit.gen5.engine.ExecutionEventRecordingEngineExecutionListener;
import org.junit.gen5.engine.ExecutionRequest;
import org.junit.gen5.engine.junit4.samples.PlainJUnit4TestCaseWithFourTests;
import org.junit.gen5.engine.junit4.samples.PlainJUnit4TestCaseWithSingleTestWhichFails;
import org.junit.gen5.engine.junit4.samples.PlainJUnit4TestCaseWithTwoTests;

Expand Down Expand Up @@ -61,6 +62,27 @@ void executesPlainJUnit4TestCaseWithTwoTests() {
// @formatter:on
}

@Test
void executesPlainJUnit4TestCaseWithFourTests() {
Class<?> testClass = PlainJUnit4TestCaseWithFourTests.class;

execute(testClass);

// @formatter:off
assertThat(listener.getExecutionEvents())
.hasSize(9)
.has(allOf(container(testClass.getName()), started()), atIndex(0))
.has(allOf(test("abortedTest"), started()), atIndex(1))
.has(allOf(test("abortedTest"), abortedWithReason(causeMessage("this test should be aborted"))), atIndex(2))
.has(allOf(test("failingTest"), started()), atIndex(3))
.has(allOf(test("failingTest"), finishedWithFailure(causeMessage("this test should fail"))), atIndex(4))
.has(allOf(test("ignoredTest"), skippedWithReason("<unknown>")), atIndex(5))
.has(allOf(test("successfulTest"), started()), atIndex(6))
.has(allOf(test("successfulTest"), finishedSuccessfully()), atIndex(7))
.has(allOf(container(testClass.getName()), finishedSuccessfully()), atIndex(8));
// @formatter:on
}

private void execute(Class<?> testClass) {
JUnit4TestEngine engine = new JUnit4TestEngine();
EngineAwareTestDescriptor engineTestDescriptor = engine.discoverTests(build(forClass(testClass)));
Expand Down
@@ -0,0 +1,45 @@
/*
* Copyright 2015-2016 the original author or authors.
*
* All rights reserved. This program and the accompanying materials are
* made available under the terms of the Eclipse Public License v1.0 which
* accompanies this distribution and is available at
*
* http://www.eclipse.org/legal/epl-v10.html
*/

package org.junit.gen5.engine.junit4.samples;

import static org.junit.Assert.*;
import static org.junit.Assume.assumeFalse;
import static org.junit.runners.MethodSorters.NAME_ASCENDING;

import org.junit.FixMethodOrder;
import org.junit.Ignore;
import org.junit.Test;

@FixMethodOrder(NAME_ASCENDING)
public class PlainJUnit4TestCaseWithFourTests {

@Test
public void abortedTest() {
assumeFalse("this test should be aborted", true);
}

@Test
public void failingTest() {
fail("this test should fail");
}

@Test
@Ignore
public void ignoredTest() {
fail("this should never be called");
}

@Test
public void successfulTest() {
assertEquals(3, 1 + 2);
}

}
Expand Up @@ -51,11 +51,22 @@ public void testRunStarted(Description description) throws Exception {
fireExecutionStarted(runnerTestDescriptor);
}

@Override
public void testIgnored(Description description) throws Exception {
listener.executionSkipped(lookupDescriptor(description), "<unknown>");
}

@Override
public void testStarted(Description description) throws Exception {
fireExecutionStarted(lookupDescriptor(description));
}

@Override
public void testAssumptionFailure(Failure failure) {
TestDescriptor testDescriptor = lookupDescriptor(failure.getDescription());
executionResults.put(testDescriptor, aborted(failure.getException()));
}

@Override
public void testFailure(Failure failure) throws Exception {
TestDescriptor testDescriptor = lookupDescriptor(failure.getDescription());
Expand Down

0 comments on commit f767d67

Please sign in to comment.