As the title says, I'm interested in the best approaches of testing an application using a Jetty web server, taking in consideration that you don't want to start/stop the test server for each particular test.
As far as I know, these are the solutions:
if you use a build tool like Maven or Gradle, you can do it in the
*.pom
or*.gradle
file.- this has the disadvantage that you have to create the test server in a different way that you normally do it in the application.
Creating a test suite and using the
@BeforeClass
and@AfterClass
annotations to start/stop the server before/after the testSuite
.- disadvantage here is the "ugly" way of specifying the tests to run. You also have to specify that the test which is added to the suite, it shouldn't run outside of the suite(avoid duplicate run of the test). I think Junit is not yet fully equipped for this.
Create and start the server in a static way at the beginning of the tests and use the
ShutDown
mechanism to hook up in the JVM and stop the server automatically when all the tests are completed. This seems to be the best solution because this mechanism is available in Jetty already, but- the disadvantage is that you are not in control of the stopping of the server. It is done actually in a totally different thread, even outside of the building tool(I use Gradle for this)