0

我有一个 JUnit 测试来启动我的 spring boot appcliation (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 ' 访问应用程序时,它说无法访问站点。

在此处输入图像描述

4

2 回答 2

3

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

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

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

如需进一步帮助,请在此处查看我的答案:如何测试(休息)点

于 2017-06-16T08:17:14.733 回答
0

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

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

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
public class MyTestClass {
...
...
...
}
于 2021-09-08T07:52:29.627 回答