Skip to content
Advertisement

Spring Boot Application is starting up from a JUnit test but unable to access through URL

I have a JUnit Test that starts my spring boot appcliation (Application.java).

@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
public class AppclaitionTest {

    @Test
    public void contextLoads(){
        Application.main(new String[]{});
    }
}

If I run the JUnit test, Application is successfully starting up, but not accessible through url

Application Logs:

2017-06-16 12:18:07.918  INFO 207028 --- [           main] com.chandu.test.AppclaitionTest          : Started AppclaitionTest in 1.927 seconds (JVM running for 2.458)

  .   ____          _            __ _ _
 /\ / ___'_ __ _ _(_)_ __  __ _    
( ( )___ | '_ | '_| | '_ / _` |    
 \/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |___, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v1.5.3.RELEASE)

2017-06-16 12:18:08.012  INFO 207028 --- [           main] com.test.app.Application                 : Starting Application on IVL-WS39 with PID 207028 (started by Bhanuchandar.Challa in D:JarsSpringJDBCMySQL)
2017-06-16 12:18:08.012  INFO 207028 --- [           main] com.test.app.Application                 : No active profile set, falling back to default profiles: default
2017-06-16 12:18:08.012  INFO 207028 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@3f1ddac2: startup date [Fri Jun 16 12:18:08 IST 2017]; root of context hierarchy
2017-06-16 12:18:08.402  INFO 207028 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2017-06-16 12:18:08.417  INFO 207028 --- [           main] o.apache.catalina.core.StandardService   : Starting service Tomcat
2017-06-16 12:18:08.417  INFO 207028 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.14
2017-06-16 12:18:08.526  INFO 207028 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2017-06-16 12:18:08.526  INFO 207028 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 514 ms
2017-06-16 12:18:08.636  INFO 207028 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean  : Mapping servlet: 'dispatcherServlet' to [/]
2017-06-16 12:18:08.636  INFO 207028 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'characterEncodingFilter' to: [/*]
2017-06-16 12:18:08.636  INFO 207028 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2017-06-16 12:18:08.636  INFO 207028 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2017-06-16 12:18:08.636  INFO 207028 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'requestContextFilter' to: [/*]
2017-06-16 12:18:08.933  INFO 207028 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@3f1ddac2: startup date [Fri Jun 16 12:18:08 IST 2017]; root of context hierarchy
2017-06-16 12:18:08.933  INFO 207028 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/getRowCount]}" onto public java.lang.Integer com.test.app.controller.TestController.getRowCount(java.lang.String)
2017-06-16 12:18:08.949  INFO 207028 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/process]}" onto public java.lang.String com.test.app.controller.TestController.processRequest()
2017-06-16 12:18:08.949  INFO 207028 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2017-06-16 12:18:08.949  INFO 207028 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2017-06-16 12:18:08.964  INFO 207028 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-06-16 12:18:08.964  INFO 207028 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-06-16 12:18:08.980  INFO 207028 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-06-16 12:18:09.105  INFO 207028 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2017-06-16 12:18:09.151  INFO 207028 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2017-06-16 12:18:09.151  INFO 207028 --- [           main] com.test.app.Application                 : Started Application in 1.186 seconds (JVM running for 3.689)

When I tried to access the application through url ‘http://localhost:8080/process‘, It says Site can’t be reached.

enter image description here

Advertisement

Answer

Why would you do such thing? This should be a unit test and the flow of the test should be the following: Start the application -> Call your controller endpoint -> assert that a specific text/element on that page is present -> Shut down the application.

Start the application : @RunWith(SpringRunner.class) doing this for you, no need to start is manually.

Shut down the application : At the end of your test class Spring boot does this for you (That’s why you cant access your app in the browser)

For further help please see my answer here: How to test (rest) enpoints

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement