Reputation: 711
I have a very simple SpringBoot application that exposes a rest endpoint at localhost:8085.
@SpringBootApplication
public class App
{
public static void main(String[] args)
{
SpringApplication.run(App.class, args);
System.out.println("The gOaT");
}
}
@RestController
public class Enpoints {
@RequestMapping("/goat")
public String home() {
return "Goat";
}
}
I'd like to start my application in a junit test. This succeeds in doing so:
public class SomeTest extends TestCase {
@Test
public void testOne() {
String[] args = new String[0];
App.main(args);
assertTrue(true);
}
}
The problem is, once the unit test initializes the application, it immediately shuts it down too (I think this is because the unit test itself terminates):
2018-08-01 21:20:43.422 INFO 4821 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8085 (http) with context path ''
2018-08-01 21:20:43.428 INFO 4821 --- [ main] com.boot.BootTraining.App : Started App in 3.168 seconds (JVM running for 3.803)
The gOaT
2018-08-01 21:20:43.468 INFO 4821 --- [ Thread-3] ConfigServletWebServerApplicationContext : Closing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@56dc1551: startup date [Wed Aug 01 21:20:40 CDT 2018]; root of context hierarchy
2018-08-01 21:20:43.470 INFO 4821 --- [ Thread-3] o.s.j.e.a.AnnotationMBeanExporter : Unregistering JMX-exposed beans on shutdown
How can I run this test, start the application, and then keep the application from closing?
Upvotes: 0
Views: 749
Reputation: 2699
Annotate you test class with:
@RunWith(SpringRunner.class)
@SpringBootTest(classes = App.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
It will load the application into the context and keep your application running.
Upvotes: 2
Reputation: 1225
For testing rest apis, you either need mockmvc or a similar sort of framework. Annotate your class with
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
I do have a sample project that might be helpful for you to get start up: https://github.com/dhananjay12/test-frameworks-tools/tree/master/test-rest-assured
Upvotes: 0