From f0ba79913d5d8d4667a06b72411b48b53199c50e Mon Sep 17 00:00:00 2001 From: Alex Panchenko Date: Thu, 8 Jun 2017 23:05:31 +0700 Subject: [PATCH] Call methodBlock() after fireTestStarted() (#1463) Fixes #1462 --- .../junit/runners/BlockJUnit4ClassRunner.java | 13 +++---- .../classes/BlockJUnit4ClassRunnerTest.java | 37 ++++++++++++++++++- 2 files changed, 42 insertions(+), 8 deletions(-) diff --git a/src/main/java/org/junit/runners/BlockJUnit4ClassRunner.java b/src/main/java/org/junit/runners/BlockJUnit4ClassRunner.java index 4514f47604e7..2a3b6ff7e9fd 100644 --- a/src/main/java/org/junit/runners/BlockJUnit4ClassRunner.java +++ b/src/main/java/org/junit/runners/BlockJUnit4ClassRunner.java @@ -92,13 +92,12 @@ protected void runChild(final FrameworkMethod method, RunNotifier notifier) { if (isIgnored(method)) { notifier.fireTestIgnored(description); } else { - Statement statement; - try { - statement = methodBlock(method); - } - catch (Throwable ex) { - statement = new Fail(ex); - } + Statement statement = new Statement() { + @Override + public void evaluate() throws Throwable { + methodBlock(method).evaluate(); + } + }; runLeaf(statement, description, notifier); } } diff --git a/src/test/java/org/junit/tests/running/classes/BlockJUnit4ClassRunnerTest.java b/src/test/java/org/junit/tests/running/classes/BlockJUnit4ClassRunnerTest.java index 47405f6b1127..9a4e87a57b03 100644 --- a/src/test/java/org/junit/tests/running/classes/BlockJUnit4ClassRunnerTest.java +++ b/src/test/java/org/junit/tests/running/classes/BlockJUnit4ClassRunnerTest.java @@ -5,6 +5,9 @@ import java.util.List; import org.junit.Test; +import org.junit.runner.Description; +import org.junit.runner.JUnitCore; +import org.junit.runner.notification.RunListener; import org.junit.runners.BlockJUnit4ClassRunner; import org.junit.runners.model.InitializationError; @@ -30,4 +33,36 @@ public void detectNonStaticEnclosedClass() throws Exception { causes.get(0).getMessage()); } } -} \ No newline at end of file + + private static String log; + + public static class MethodBlockAfterFireTestStarted { + public MethodBlockAfterFireTestStarted() { + log += " init"; + } + + @Test + public void test() { + log += " test"; + } + } + + @Test + public void methodBlockAfterFireTestStarted() { + log = ""; + JUnitCore junit = new JUnitCore(); + junit.addListener(new RunListener() { + @Override + public void testStarted(Description description) throws Exception { + log += " testStarted(" + description.getMethodName() + ")"; + } + + @Override + public void testFinished(Description description) throws Exception { + log += " testFinished(" + description.getMethodName() + ")"; + } + }); + junit.run(MethodBlockAfterFireTestStarted.class); + assertEquals(" testStarted(test) init test testFinished(test)", log); + } +}