1

According to: https://jira.spring.io/browse/SPR-4103 SpringJUnit4ClassRunner won't always called DisposableBean.destroy after tests (*facepalm*!) -- due to implementation problems with JUnit.

Is this still true?

I'm creating tests in scala like so:

@RunWith(classOf[SpringJUnit4ClassRunner])
@WebAppConfiguration
@ContextConfiguration(classes = Array(classOf[Service1Config]))
class Service1Test {

  @Test
  def test1(): Unit = {
  }

}

@RunWith(classOf[SpringJUnit4ClassRunner])
@WebAppConfiguration
@ContextConfiguration(classes = Array(classOf[Service2Config]))
class Service2Test {

  @Test
  def test2(): Unit = {
  }

}

and I'm finding that the destroy method of beans in Service1Config aren't destroyed when Service2Test is executed.

I've found many articles recommending adding a @After to shut down the context explicitly. This sounds like an error waiting to happen (since if you forget to add an @After cleanup in one test, the next test class will fail and you will have no idea why).

If this still can't be done with SpringJUnit4ClassRunner/JUnit, is there a test framework that will automatically call the context cleanup after each test?

elixenide
  • 44,308
  • 16
  • 74
  • 100
user48956
  • 14,850
  • 19
  • 93
  • 154

1 Answers1

2

Try adding to the class

@DirtiesContext(classMode=ClassMode.AFTER_CLASS)
class Service1Test {

That will do the trick.

pabloa98
  • 630
  • 7
  • 16