2016-07-03 33 views
1

我想了解爲什麼我的SpringBoot應用程序沒有從集成測試開始。我完全困惑,我不知道它是否必須對我的依賴做些什麼,以及如何解決它。無法運行Spring集成測試,因爲容器無法啓動

這是我在運行測試:

@RunWith(SpringJUnit4ClassRunner.class) 
@SpringApplicationConfiguration(classes = Application.class) 
@WebAppConfiguration 
@IntegrationTest("server.port:0") 
public class TicTacToeAcceptaceTest { 
//my tests are here... 
} 

堆棧跟蹤說:

java.lang.IllegalStateException: Failed to load ApplicationContext 

    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:124) 
    at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:83) 
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:117) 
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:83) 
    at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:228) 
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:230) 
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:289) 
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) 
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:291) 
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:249) 
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:89) 
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) 
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) 
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) 
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) 
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) 
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61) 
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70) 
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363) 
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:193) 
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137) 
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:119) 
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:42) 
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:234) 
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:74) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:498) 
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144) 
Caused by: org.springframework.context.ApplicationContextException: Unable to start embedded container; nested exception is java.lang.NoSuchMethodError: javax.servlet.ServletContext.addServlet(Ljava/lang/String;Ljavax/servlet/Servlet;)Ljavax/servlet/ServletRegistration$Dynamic; 
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:133) 
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:532) 
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118) 
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:766) 
    at org.springframework.boot.SpringApplication.createAndRefreshContext(SpringApplication.java:361) 
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:307) 
    at org.springframework.boot.test.SpringApplicationContextLoader.loadContext(SpringApplicationContextLoader.java:98) 
    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:98) 
    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:116) 
    ... 29 more 
Caused by: java.lang.NoSuchMethodError: javax.servlet.ServletContext.addServlet(Ljava/lang/String;Ljavax/servlet/Servlet;)Ljavax/servlet/ServletRegistration$Dynamic; 
    at org.springframework.boot.context.embedded.ServletRegistrationBean.onStartup(ServletRegistrationBean.java:190) 
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.selfInitialize(EmbeddedWebApplicationContext.java:225) 
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.access$000(EmbeddedWebApplicationContext.java:85) 
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext$1.onStartup(EmbeddedWebApplicationContext.java:209) 
    at org.springframework.boot.context.embedded.jetty.ServletContextInitializerConfiguration$Initializer.callInitializers(ServletContextInitializerConfiguration.java:83) 
    at org.springframework.boot.context.embedded.jetty.ServletContextInitializerConfiguration$Initializer.doStart(ServletContextInitializerConfiguration.java:72) 
    at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68) 
    at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:132) 
    at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:106) 
    at org.eclipse.jetty.server.handler.AbstractHandler.doStart(AbstractHandler.java:61) 
    at org.eclipse.jetty.server.handler.ScopedHandler.doStart(ScopedHandler.java:120) 
    at org.eclipse.jetty.server.handler.ContextHandler.startContext(ContextHandler.java:784) 
    at org.eclipse.jetty.servlet.ServletContextHandler.startContext(ServletContextHandler.java:294) 
    at org.eclipse.jetty.webapp.WebAppContext.startWebapp(WebAppContext.java:1349) 
    at org.eclipse.jetty.webapp.WebAppContext.startContext(WebAppContext.java:1342) 
    at org.eclipse.jetty.server.handler.ContextHandler.doStart(ContextHandler.java:741) 
    at org.eclipse.jetty.webapp.WebAppContext.doStart(WebAppContext.java:505) 
    at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68) 
    at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:132) 
    at org.eclipse.jetty.server.Server.start(Server.java:387) 
    at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:114) 
    at org.eclipse.jetty.server.handler.AbstractHandler.doStart(AbstractHandler.java:61) 
    at org.eclipse.jetty.server.Server.doStart(Server.java:354) 
    at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68) 
    at org.springframework.boot.context.embedded.jetty.JettyEmbeddedServletContainer.initialize(JettyEmbeddedServletContainer.java:85) 
    at org.springframework.boot.context.embedded.jetty.JettyEmbeddedServletContainer.<init>(JettyEmbeddedServletContainer.java:74) 
    at org.springframework.boot.context.embedded.jetty.JettyEmbeddedServletContainerFactory.getJettyEmbeddedServletContainer(JettyEmbeddedServletContainerFactory.java:472) 
    at org.springframework.boot.context.embedded.jetty.JettyEmbeddedServletContainerFactory.getEmbeddedServletContainer(JettyEmbeddedServletContainerFactory.java:163) 
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.createEmbeddedServletContainer(EmbeddedWebApplicationContext.java:160) 
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:130) 
    ... 37 more 

我認爲我也許有一個傳遞依賴,但我不知道如何找到它並修復它。這是我的gradle.build:

buildscript { 
    repositories { 
     mavenCentral() 
    } 
    dependencies { 
     classpath group: 'org.springframework.boot', name: 'spring-boot-gradle-plugin', version: '1.3.5.RELEASE' 
    } 
} 

apply plugin: 'java' 
apply plugin: 'idea' 
apply plugin: 'spring-boot' 
apply plugin: 'application' 

version = '1.0.0-SNAPSHOT' 
sourceCompatibility = 1.8 
targetCompatibility = 1.8 

//applicationDefaultJvmArgs = ["-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=5005"] 

repositories { 
     mavenLocal() 
     mavenCentral() 
     maven { 
      url "http://repo.bodar.com" 
     } 
} 

ext { 
    commonsLangVersion = '3.4' 
    slf4jVersion = '1.7.19' 
    unirestVersion = '1.4.8' 
    jsonioVersion = '4.4.0' 
    mockitoVersion = '1.10.19' 
    assertjVersion = '3.3.0' 
    jsonassertVersion = '1.3.0' 
    junitVersion = '4.12' 
    yatspecVersion = '217' 
    seleniumJavaVersion = '2.48.2' 
    seleniumVersion = '2.48.2' 
} 

dependencies { 
    compile("org.springframework.boot:spring-boot-starter-web") { 
     exclude module: "spring-boot-starter-tomcat" 
    } 
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-actuator' 
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-jetty' 
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-logging' 

    compile group: 'org.springframework', name: 'spring-context-support' 

    compile group: 'org.apache.commons', name: 'commons-lang3', version: commonsLangVersion 
    compile group: 'org.slf4j', name: 'slf4j-api', version: slf4jVersion 
    compile group: 'com.mashape.unirest', name: 'unirest-java', version: unirestVersion 
    compile group: 'com.cedarsoftware', name: 'json-io', version: jsonioVersion 

    testCompile group: 'org.springframework', name: 'spring-test' 
    testCompile group: 'junit', name: 'junit', version: junitVersion 
    testCompile group: 'org.mockito', name: 'mockito-core', version: mockitoVersion 
    testCompile group: 'org.assertj', name:'assertj-core', version: assertjVersion 
    testCompile group: 'org.skyscreamer', name:'jsonassert', version: jsonassertVersion 
    testCompile group: 'com.googlecode.yatspec', name:'yatspec', version: yatspecVersion 
    testCompile group: 'org.seleniumhq.selenium', name:'selenium-java', version: seleniumVersion 
} 

task wrapper(type: Wrapper) { 
    gradleVersion = "2.11" 
} 
+0

你應該模擬servlet對象。 –

+0

這是什麼意思,爲什麼我必須這樣做,我該怎麼做? – sfrj

+0

做些事情,然後復出並告訴我們問題。此外,如果您有其他問題,可以將其發佈到單獨的頁面上。 –

回答

0

看起來像傳遞性依賴性問題。嘗試添加

configurations { 
     provided.all*.exclude group: 'javax.servlet' 
} 

這應該停止使用不同方法簽名的庫的所有舊版本。

0

今天我有類似的問題。我的設置有各種測試類,其中一些包括@WebConfiguration@IntegrationTest,一些僅包含@IntegrationTest。所有測試都加載了相同的上下文,除了一些不需要完整的服務器啓動。 我不知道爲什麼當我將@WebConfiguration添加到其他不需要它的測試時,測試運行良好。 (雖然有點慢,因爲我不得不每次啓動服務器)。

相關問題