1

For optimizing purpose I need to customize TestExecutionListeners invoking logic

In my case I have one ApplicationContext and two types of tests:

  1. Ones which use WebDriver (let’s call it ObservableTest)
  2. Ones which use RestTemplate and JdbcTemplate (let’s call it ApiTest)

Each type uses its own TestExecutionListener:

  1. ObservableTest - ObservableTestListener
  2. ApiTest - ApiTestListener

Both ObservableTestListener and ApiTestListener extend TestListener where prepareTestInstance() is defined

ObservableTestListener implements beforeTestClass() and afterTestClass() methods as well as ApiTestListener does

I need combine test types above in one JUnit TestSuite in the next way:

  1. For each test prepareTestInstance() is invoked
  2. As soon as first ObservableTest is about to be instantiated, beforeTestClass() of ObservableTestListener is executed
  3. The same with ApiTest
  4. afterTestClass() of ObservableTestListener is invoked when last ObservableTest is finished in current Suite
  5. The same with ApiTest

Things got even more complicated as each test may be run in one suite and in different ApplicationContexts (due to different profiles usage)

I would be very grateful for any hint and digging direction to implement such logic properly

I have two ideas so far:

  1. Implementing custom Runner (I'm not confident it is even possible)
  2. Notify TestContextManager somehow that particular method (beforeTestClass() or afterTestClass()) should or should not be invoked. I have a feeling that @BootstrapWith custom SpringClassRule should help me in that

Thanks!

Anton
  • 449
  • 2
  • 7
  • 20
  • Why isn't it possible to use `@TestExecutionListeners` on the 2 different test types to register the appropriate listeners? – M. Deinum Dec 06 '16 at 09:55
  • @M.Deinum this is exactly what I am doing, but when I put those test in suite, TestExecutionListeners invoke its methods with each test of this suite. I'm trying to avoid that – Anton Dec 06 '16 at 09:59
  • Spring's SuiteExecutionListener may work. http://stackoverflow.com/questions/37985717/is-there-anything-like-spring-testexecutionlistener-for-testsuite – Theo Briscoe Feb 05 '17 at 08:58

1 Answers1

1

The TestExecutionListener API in the Spring TestContext Framework does not have any lifecycle callbacks at the suite level.

Thus, out of the box, there is no way to instruct Spring to invoke methods on TELs before or after a suite. Consequently, any solution you come up with to support your requirements will have to be completely custom.

I don't foresee how a custom SpringClassRule would be that useful: you will still need some reliable mechanism for determining when a test suite begins and ends. That is actually the biggest challenge since suite support in JUnit 4 is via a specific Runner. So, if you run all of your tests via a JUnit 4 Suite, you could potentially build in your own lifecycle callbacks in a custom subclass of Suite.

Regards,

Sam (author of the Spring TestContext Framework)

Sam Brannen
  • 29,611
  • 5
  • 104
  • 136