[英]Spring ServletRegistrationBean not loaded in JUnit Test
I'm trying to test a Rest Service in my Spring Boot Application. 我正在尝试在Spring Boot应用程序中测试Rest服务。 I can do ir successfully when I run or debug the whole application.
当我运行或调试整个应用程序时,我可以成功完成操作。 Spring Bott start shows the following lines, among others:
Spring Bott开始显示以下几行,其中包括:
2015-07-02 17:04:10.654 INFO 3084 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.0.20
2015-07-02 17:04:10.769 INFO 3084 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2015-07-02 17:04:10.769 INFO 3084 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 2449 ms
2015-07-02 17:04:11.219 INFO 3084 --- [ost-startStop-1] o.s.b.c.e.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/]
2015-07-02 17:04:11.224 INFO 3084 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]
2015-07-02 17:04:11.224 INFO 3084 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
However, when I run my JUnit test, everything works fine, request mappings work, etc, except that the servlet is not initialized. 但是,当我运行JUnit测试时,除了未初始化Servlet之外,其他所有东西都可以正常工作,请求映射也可以正常工作。 Indeed, those lines above do not appear.
实际上,上面的那些行没有出现。
That's strange, because I'm running the test using the same Application.class which I use to run the whole application. 这很奇怪,因为我使用与运行整个应用程序相同的Application.class运行测试。 This is Application.class:
这是Application.class:
@ImportResource("classpath:/META-INF/spring/integration/spring-integration-context.xml")
@SpringBootApplication
@WebAppConfiguration
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
And from my JUnit test: 从我的JUnit测试中:
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
public class CatalogRestControllerTests extends AbstractTransactionalJUnit4SpringContextTests {
...
@Test
public void someTest() {...}
}
I've been trying to fix the problem from servlet-context.xml and web.xml configuration files. 我一直在尝试从servlet-context.xml和web.xml配置文件中解决此问题。 But I don't think the problem is there, since it works for Application.class
但我认为问题不存在,因为它适用于Application.class
Any idea? 任何想法? Thank you!
谢谢!
Found it. 找到了。
So it seems I was missing @WebIntegrationTest annotation on my JUnit class. 所以看来我在JUnit类上缺少@WebIntegrationTest批注。 This is what documentation says about @WebIntegrationTest :
这就是有关@WebIntegrationTest的文档所说的内容:
Test class annotation signifying that the tests are "web integration tests" and therefore require full startup in the same way as a production application (listening on normal ports).
测试类注释表示测试是“ Web集成测试”,因此需要以与生产应用程序相同的方式完全启动(在正常端口上侦听)。 Normally used in conjunction with @SpringApplicationConfiguration,
通常与@SpringApplicationConfiguration结合使用,
This annotation can be used as an alternative to @IntegrationTest and @WebAppConfiguration.
此注释可以用作@IntegrationTest和@WebAppConfiguration的替代方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.