0

Currently for tests I'm using TestExecutionListener and it works just perfect

public class ExecutionManager extends AbstractTestExecutionListener {

    @Override
    public void beforeTestClass(TestContext testContext) throws Exception {
        System.out.println("beforeClass");
    }

    @Override
    public void afterTestClass(TestContext testContext) throws Exception {
        System.out.println("afterClass");
    }
}

Test classes:

@RunWith(SpringJUnit4ClassRunner.class)
@TestExecutionListeners(ExecutionManager.class)
public final class TC_001 {

    @Test
    public void test() {
        System.out.println("Test_001");
    }
}

@RunWith(SpringJUnit4ClassRunner.class)
@TestExecutionListeners(ExecutionManager.class)
public final class TC_002 {

    @Test
    public void test() {
        System.out.println("Test_002");
    }
}

When I include those tests in test suite, beforeTestClass(TestContext testContext) and afterTestClass(TestContext testContext) methods are executed for each test class, what is quite logical:

@RunWith(Suite.class)
@Suite.SuiteClasses({
        TC_001.class,
        TC_002.class
})
public final class TS_001 {
}

Is there anything like SuiteExecutionListener (TestExecutionListener for suites)?

Basically I need non-static @BeforeClass and @AfterClass for suite

OR

In ExecutionListener I need to find out what class has been launched: case or suite. For this purpose I can:

  • analyze StackTrace and get calling class
  • use Reflection.getCallerClass(int i) (which is deprecated)
  • pass caller class to ExecutionManager (By the way, how can I do that? Is it possible to put Object into TestContext like in Android Bundle?)

But I don't really like those solutions. SuiteExecutionListener is much more preferable

Thank you

Anton
  • 449
  • 2
  • 7
  • 20

1 Answers1

1

No, there is unfortunately no such thing as a SuiteExecutionListener in the Spring TestContext Framework (TCF).

The TCF does not integrate with JUnit 4 at the suite level.

If you want to store something in the TestContext, that's not a problem. TestContext implements org.springframework.core.AttributeAccessor, so you can store attributes in the TestContext. Note, however, that the lifecycle of a given TestContext is tied to a test class.

Sam Brannen
  • 29,611
  • 5
  • 104
  • 136
  • So basically, if TestContext is tied to a test class, I cannot use it to manage test suite flow from TestExecutionListener because it is going to be different TestContext for each test case. Am I correct? – Anton Jun 24 '16 at 10:21
  • In addition, as far as I can see for now, I cannot access TestContext from test suite, but ApplicationContext is available. As it is available, I can put some flag bean there and read it from my ExecutionManager. Theoretically it will allow me to manage test suite flow as ApplicationContext may be cached. It doesn't look good, by I don't see other options considering the additional fact, that I cannot use reflection in ExecutionManager: test suite and test classes are simply absent in stacktrace. Could you give me a clue if there is anything more handy for my purpose? Thank you – Anton Jun 24 '16 at 10:21
  • To answer your first question: yes, that's correct. Spring has nothing to do with the JUnit 4 `Suite` runner. So there is no support for `TestExecutionListener`s at the suite level. – Sam Brannen Jun 24 '16 at 12:27
  • If you want to _manage test suite flow_ (as you call it), you would basically have to extend `org.junit.runners.Suite` and add your custom behavior. – Sam Brannen Jun 24 '16 at 12:28
  • To communicate between your custom `Suite` implementation and the test classes it runs, you might consider using a `ThreadLocal`. That might be more elegant than reflection or storing some bean in a _potentially_ shared `ApplicationContext`. – Sam Brannen Jun 24 '16 at 12:30