Can I make Surefire perform expensive setup/teardown only once?
Asked Answered
B

3

1

I have a series of unit tests that all need to talk to an Apache Zookeeper server. Obviously, if I had a library for mocking server connections (or an easy way to roll my own) that would be optimal, but as far as I can tell none exists at this point, and there's enough moving parts under the hood that I hesitate to try it by hand.

So, our solution so far has been to stand up a dummy server locally, perform the tests, then tear it down at the end. This kind of works, though as far as I can tell there's no easy way to tell Surefire to do something once per entire test run -- at best, I have @BeforeClass and @AfterClass decorations.

The Zookeeper initialization process is turning what should be several-millisecond test runs into several seconds per class, which works out to several minutes for the whole project. If I could set up the tests so that it stands up a server, runs each test, then tears it down, I think I'd see a speedup of at least an order of magnitude.

Is there a simple fix? Am I going about this the wrong way? If there's an existing mock-zookeeper library, or a simple way to roll my own, that's a fine solution too.

Besetting answered 14/9, 2011 at 19:37 Comment(0)
N
4

Sounds like an integration test. Look at 'failsafe' instead of surefire, and do the expensive stuff in the additional lifecycle phases for setup and teardown of integration tests.

Nosing answered 14/9, 2011 at 19:51 Comment(1)
It's definitely not an integration test. Each unit test is actually touching one method without dependencies on any other code. However, the code being tested has an outside dependency (Zookeeper), and interacting with that dependency is slow and expensive, so I'd like to do it less. That said, I'll take a look at failsafe -- maybe I can "fake it" :DBesetting
S
1

If using TestNG is an option to you, you could use @BeforeSuite.

Stimulative answered 15/9, 2011 at 17:36 Comment(0)
S
0

You might want to check the AbstractSingleSpringContextTests class in Spring. This class loads a Spring context for all the subclasses and only reloads it if the method setDirty() is called.

Is not exactly what you want, but I'm sure you can copy the pattern and use it to start a mock zookeeper server.

Singleton answered 14/9, 2011 at 19:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.