Well, it depends on what you want to test. If you're just unit testing code that has no external dependencies or dependencies that you can mock or stub out (and it would be a good idea to structure your code in such a way that allows this), then you don't need to use WithApplication
. This is probably the best approach.
The first solution you provided doesn't work because applications can only be used once. It's WithApplication
that starts and stops your application, so even if that did work, you wouldn't get any performance benefit.
The second solution you provided doesn't work because when the Helpers.running(app) { }
code block runs, this is only declaring the specs. Specs puts all these in a list, and then you exit the running block and it shuts down the app. Then at some point later, specs runs the tests, and there's no application of course then.
So, if you can't test your code in isolation of the rest of your app, then you need to have a running app, there's nothing you can do about that, it's the reality of integration testing. And you probably want it started and shutdown between each test, otherwise your tests aren't running in isolation of each other.