我们最近将我们的应用程序与 spring boot集成我们的测试用例基于testng框架. 我们的基础测试类如下所示 @SpringApplicationConfiguration(classes = Application.class) @ActiveProfiles(profiles = "test") @WebAppCo
我们的基础测试类如下所示
@SpringApplicationConfiguration(classes = Application.class) @ActiveProfiles(profiles = "test") @WebAppConfiguration @IntegrationTest public class BaseTestCase extends AbstractTestNGSpringContextTests { }
我们定义了上面的类来设置活动配置文件并加载应用程序上下文.所有集成测试类都扩展了BaseTestCase
我们的一个基本测试用例如下所示
@Test public void testPing() throws Exception{ RestTemplate restTemplate = new RestTemplate(); String response = restTemplate.getForObject( <some url>, String.class); Assert.assertNotNull(response); }
当我们运行上面的测试用例时,我们得到以下异常
FAILED CONFIGURATION: @BeforeClass springTestContextPrepareTestInstance java.lang.IllegalStateException: The WebApplicationContext for test context [DefaultTestContext@11b72c96 testClass = DataResourceTest, testInstance = com.xactly.insights.resource.imp.DataResourceTest@5b630a31, testMethod = [null], testException = [null], mergedContextConfiguration = [WebMergedContextConfiguration@10d034f0 testClass = DataResourceTest, locations = '{}', classes = '{class com.xactly.insights.app.Application}', contextInitializerClasses = '[]', activeProfiles = '{test}', resourceBasePath = 'src/main/webapp', contextLoader = 'org.springframework.boot.test.SpringApplicationContextLoader', parent = [null]]] must be configured with a MockServletContext. at org.springframework.util.Assert.state(Assert.java:385) at org.springframework.test.context.web.ServletTestExecutionListener.setUpRequestContextIfNecessary(ServletTestExecutionListener.java:166) at org.springframework.test.context.web.ServletTestExecutionListener.prepareTestInstance(ServletTestExecutionListener.java:101) at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:331) at org.springframework.test.context.testng.AbstractTestNGSpringContextTests.springTestContextPrepareTestInstance(AbstractTestNGSpringContextTests.java:146) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:84) at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:564) at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:213) at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:138) at org.testng.internal.TestMethodWorker.invokeBeforeClassMethods(TestMethodWorker.java:175) at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:107) at org.testng.TestRunner.privateRun(TestRunner.java:767) at org.testng.TestRunner.run(TestRunner.java:617) at org.testng.SuiteRunner.runTest(SuiteRunner.java:334) at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329) at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291) at org.testng.SuiteRunner.run(SuiteRunner.java:240) at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52) at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86) at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224) at org.testng.TestNG.runSuitesLocally(TestNG.java:1149) at org.testng.TestNG.run(TestNG.java:1057) at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111) at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204) at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)
我们使用spring boot版本1.1.5.RELEASE和testng版本6.1.1.有人可以说明如何解决上述问题吗?
问题是,默认情况下,AbstractTestNGSpringContextTests启用ServletTestExecutionListener.此侦听器为您的测试提供模拟Servlet API支持.在这种情况下,这是不合适的,因为您正在运行Spring Boot集成测试,其中为您启动了嵌入式Tomcat服务器,为您提供了真正的ServletContext.这导致您看到ServletTestExecutionListener声明ServletContext是MockServletContext实例时失败.您可以通过禁用AbstractTestNGSpringContextTests的测试执行侦听器的继承并使用@TestExecutionListeners显式配置它们来解决此问题:
@SpringApplicationConfiguration(classes = Application.class) @WebAppConfiguration @IntegrationTest @TestExecutionListeners(inheritListeners = false, listeners = { DependencyInjectionTestExecutionListener.class, DirtiesContextTestExecutionListener.class }) public class BaseTestCase extends AbstractTestNGSpringContextTests { }