2014-08-27 146 views
8

我们最近将我们的应用程序与Spring引导集成在一起。我们的测试案例基于testng框架。 我们的基础测试类看起来如下使用TestNG集成测试Spring Boot web应用程序

@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 [[email protected] testClass = DataResourceTest, testInstance = [email protected], testMethod = [null], testException = [null], mergedContextConfiguration = [[email protected] 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) 

我们用春天启动版本1.1.5.RELEASE和testng版本6.1.1。有人可以解释如何解决上述问题吗?

+1

这个类,你可以张贴一些代码,也许说明你已经尝试过什么,什么是行不通的。 – 2014-08-28 09:48:20

回答

17

的问题是,默认情况下,AbstractTestNGSpringContextTests使ServletTestExecutionListener。这位听众提供模拟 Servlet API支持您的测试。在这种情况下,这并不合适,因为您正在运行Spring Boot集成测试,其中启动了嵌入式Tomcat服务器,为您提供真正的ServletContext。这会导致您看到的失败,因为ServletTestExecutionListener声称ServletContextMockServletContext实例。

可以通过禁用AbstractTestNGSpringContextTests的测试执行听众的继承和明确使用@TestExecutionListeners配置,而不是他们解决这个问题:

@SpringApplicationConfiguration(classes = Application.class) 
@WebAppConfiguration 
@IntegrationTest 
@TestExecutionListeners(inheritListeners = false, listeners = { 
     DependencyInjectionTestExecutionListener.class, 
     DirtiesContextTestExecutionListener.class }) 
public class BaseTestCase extends AbstractTestNGSpringContextTests { 

} 
+1

非常感谢。这工作像魔术一样 – 2014-08-28 17:40:35

1

我有同样的问题。令人惊讶的是什么工作对我来说是创造SpringContextLoadingTest类,把所有的注释出现,而是延长AbstractTestNGSpringContextTests

相关问题