我們最近將我們的應用程序與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。有人可以解釋如何解決上述問題嗎?
這個類,你可以張貼一些代碼,也許說明你已經嘗試過什麼,什麼是行不通的。 – 2014-08-28 09:48:20