2014-08-27 103 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

相關問題