Permalink
Browse files

Preparing for Cucumber Plugin

  • Loading branch information...
1 parent 8f6f4d0 commit d5983b7e39b04c7808ff2bd66e8f7c5135055776 @vasiliygagin vasiliygagin committed Feb 7, 2014
@@ -47,6 +47,7 @@ public JUnitRunner(Class<?> testClass) throws InitializationError {
computeClassStatement(errors);
validateTestRules(errors);
+ // TODO: would be nice to discover tests before computeClassStatement
discoverTests(errors);
configureTestSuite(errors);
@@ -113,7 +114,10 @@ private void processPlugins() throws InitializationError {
private void computeClassStatement(List<Throwable> errors) {
- Description description = getDescription();
+ /*
+ * children are not registered yet, calling getDescription() will cache incomplete description
+ */
+ Description description = suite.describeSelf();
Statement statement = suite.constructInvokeStatement(this);
@@ -18,7 +18,7 @@ public JUnitSuite(Class<?> testClass) {
this.testClass = testClass;
}
- void addTask(JUnitTask task) {
+ protected void addTask(JUnitTask task) {
children.add(task);
}
@@ -27,9 +27,9 @@ public boolean isEmpty() {
}
@Override
- public Description describe() {
+ public Description createDescription() {
- Description description = Description.createSuiteDescription(testClass.getName(), testClass.getAnnotations());
+ Description description = describeSelf();
for (JUnitTask test : children) {
if (test.isFilteredOut()) {
continue;
@@ -39,6 +39,10 @@ public Description describe() {
return description;
}
+ protected Description describeSelf() {
+ return Description.createSuiteDescription(testClass.getName(), testClass.getAnnotations());
+ }
+
protected void invoke(JUnitRunner jUnitRunner) throws Throwable {
boolean hasFailures = false;
@@ -52,13 +56,14 @@ protected void invoke(JUnitRunner jUnitRunner) throws Throwable {
} else {
Statement statement = task.constructInvokeStatement(jUnitRunner);
jUnitRunner.listenerContainer.taskStarted(task);
+ Throwable failure = null;
try {
statement.evaluate();
- jUnitRunner.listenerContainer.taskFinished(task);
} catch (Throwable exc) {
- jUnitRunner.listenerContainer.taskFailed(task, exc);
+ failure = exc;
hasFailures = true;
}
+ jUnitRunner.listenerContainer.taskFinished(task, failure);
}
}
@@ -10,6 +10,7 @@
private boolean ignored;
private boolean filteredOut;
+ protected Description description;
protected JUnitTask() {
}
@@ -30,7 +31,14 @@ protected void filterOut() {
filteredOut = true;
}
- public abstract Description describe();
+ public final Description describe() {
+ if (description == null) {
+ description = createDescription();
+ }
+ return description;
+ }
+
+ public abstract Description createDescription();
protected abstract Statement constructInvokeStatement(JUnitRunner jUnitRunner);
@@ -21,24 +21,25 @@ public void testIgnored(JUnitTask test) {
}
@Override
- public void testStarted(JUnitTask test) {
- runNotifier.fireTestStarted(test.describe());
- }
-
- @Override
- public void testFinished(JUnitTask test) {
- runNotifier.fireTestFinished(test.describe());
+ public void taskStarted(JUnitTask task) {
+ if (task instanceof JUnitTest) {
+ runNotifier.fireTestStarted(task.describe());
+ }
}
@Override
- public void testFailed(JUnitTask test, Throwable e) {
- Description description = test.describe();
- Failure failure = new Failure(description, e);
- if (e instanceof AssumptionViolatedException) {
- runNotifier.fireTestAssumptionFailed(failure);
- } else {
- runNotifier.fireTestFailure(failure);
+ public void taskFinished(JUnitTask task, Throwable failure) {
+ if (task instanceof JUnitTest) {
+ if (failure != null) {
+ Description description = task.describe();
+ if (failure instanceof AssumptionViolatedException) {
+ runNotifier.fireTestAssumptionFailed(new Failure(description, failure));
+ } else {
+ runNotifier.fireTestFailure(new Failure(description, failure));
+ }
+ }
+
+ runNotifier.fireTestFinished(task.describe());
}
- runNotifier.fireTestFinished(description);
}
}
@@ -11,7 +11,6 @@
public class JUnitJavaMethodTest extends JUnitTest {
private final Method method;
- private Description description;
public JUnitJavaMethodTest(Class<?> testClass, Method method) {
this.method = method;
@@ -20,8 +19,8 @@ public JUnitJavaMethodTest(Class<?> testClass, Method method) {
}
@Override
- public Description describe() {
- return description;
+ public Description createDescription() {
+ throw new UnsupportedOperationException();
}
public Method getMethod() {
@@ -15,30 +15,18 @@ public void testClassFinished(Description description) {
}
@Override
- public void testSuiteStarted(JUnitTask test) {
+ public void taskStarted(JUnitTask task) {
}
@Override
- public void testSuiteFinished(JUnitTask test) {
+ public void taskFinished(JUnitTask task, Throwable failure) {
}
@Override
public void testIgnored(JUnitTask test) {
}
@Override
- public void testStarted(JUnitTask test) {
- }
-
- @Override
- public void testFailed(JUnitTask test, Throwable e) {
- }
-
- @Override
- public void testFinished(JUnitTask test) {
- }
-
- @Override
public void testCoreStarted(JUnitTask test, Object testObject) {
}
@@ -10,20 +10,14 @@
void testClassFinished(Description description);
- void testSuiteStarted(JUnitTask test);
+ void taskStarted(JUnitTask task);
- void testSuiteFinished(JUnitTask test);
+ void taskFinished(JUnitTask task, Throwable failure);
void testCoreStarted(JUnitTask test, Object testObject);
void testCoreFinished(JUnitTask test, Object testObject, Throwable error);
- void testStarted(JUnitTask test);
-
- void testFailed(JUnitTask test, Throwable e);
-
- void testFinished(JUnitTask test);
-
void testIgnored(JUnitTask test);
}
@@ -5,9 +5,7 @@
import org.junit.runner.Description;
-import com.github.junitrunner.JUnitSuite;
import com.github.junitrunner.JUnitTask;
-import com.github.junitrunner.JUnitTest;
public class RunnerListenerContainer {// implements RunnerListener {
@@ -36,54 +34,14 @@ public void taskIgnored(JUnitTask test) {
}
public void taskStarted(JUnitTask task) {
- if (task instanceof JUnitSuite) {
- testSuiteStarted(task);
- } else {
- testStarted(task);
- }
- }
-
- public void taskFailed(JUnitTask task, Throwable exception) {
- if (task instanceof JUnitTest) {
- testFailed(task, exception);
- }
- }
-
- public void taskFinished(JUnitTask task) {
- if (task instanceof JUnitSuite) {
- testSuiteFinished(task);
- } else {
- testFinished(task);
- }
- }
-
- private void testSuiteStarted(JUnitTask test) {
- for (RunnerListener listener : listeners) {
- listener.testSuiteStarted(test);
- }
- }
-
- private void testSuiteFinished(JUnitTask test) {
- for (RunnerListener listener : listeners) {
- listener.testSuiteFinished(test);
- }
- }
-
- private void testStarted(JUnitTask test) {
- for (RunnerListener listener : listeners) {
- listener.testStarted(test);
- }
- }
-
- private void testFailed(JUnitTask test, Throwable e) {
for (RunnerListener listener : listeners) {
- listener.testFailed(test, e);
+ listener.taskStarted(task);
}
}
- private void testFinished(JUnitTask test) {
+ public void taskFinished(JUnitTask task, Throwable failure) {
for (RunnerListener listener : listeners) {
- listener.testFinished(test);
+ listener.taskFinished(task, failure);
}
}

0 comments on commit d5983b7

Please sign in to comment.