【问题标题】:Spring Boot Application is starting up from a JUnit test but unable to access through URLSpring Boot 应用程序正在从 JUnit 测试启动,但无法通过 URL 访问
【发布时间】:2017-11-18 20:15:37
【问题描述】:

我有一个 JUnit 测试,可以启动我的 Spring Boot 应用程序 (Application.java)。

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

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

如果我运行 JUnit 测试,应用程序已成功启动,但无法通过 url 访问

应用程序日志:

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:\Jars\SpringJDBCMySQL)
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)

当我尝试通过 url 'http://localhost:8080/process' 访问应用程序时,它显示无法访问站点。

【问题讨论】:

  • 您是否在测试中设置断点?因为测试结束后它会关闭你的服务
  • 你检查绑定地址了吗?它可能不是在本地主机上侦听,而是在特定的 IP 地址或主机名上侦听
  • 为什么要通过浏览器访问 Spring Boot 测试?
  • @sitakant 我的错。尚未浏览完整的应用程序日志。 Spring Boot 和 Junit 的新手,因此提出了这个愚蠢的问题。

标签: java spring-boot junit spring-boot-test


【解决方案1】:

你为什么要这么做?这应该是一个单元测试,测试流程应该如下: 启动应用程序 -> 调用您的控制器端点 -> 断言该页面上的特定文本/元素存在 -> 关闭应用程序。

启动应用程序:@RunWith(SpringRunner.class) 为您执行此操作,无需手动启动。

关闭应用程序:在您的测试类结束时,Spring boot 会为您执行此操作(这就是您无法在浏览器中访问您的应用程序的原因)

如需进一步帮助,请在此处查看我的回答:How to test (rest) enpoints

【讨论】:

    【解决方案2】:

    默认情况下,@SpringBootTest 注解中的 webEnvironment 字段设置为 WebEnvironment.MOCK
    因此,Tomcat 不会启动。

    您可以将此字段设置为 WebEnvironment.DEFINED_PORT/RANDOM_PORT。 之后,Tomcat 将运行。

    @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
    public class MyTestClass {
    ...
    ...
    ...
    }
    

    【讨论】:

      猜你喜欢
      • 2017-02-25
      • 2017-02-08
      • 1970-01-01
      • 2018-07-22
      • 1970-01-01
      • 1970-01-01
      • 2015-05-10
      • 2020-08-04
      相关资源
      最近更新 更多