Java Can';t使用GAE运行spring引导单元测试
每次我尝试用Spring Boot项目为我的Google应用程序引擎运行单元测试时,我都会遇到安装错误,似乎找不到解决方案 以下是我当前的错误:Java Can';t使用GAE运行spring引导单元测试,java,unit-testing,google-app-engine,spring-mvc,spring-boot,Java,Unit Testing,Google App Engine,Spring Mvc,Spring Boot,每次我尝试用Spring Boot项目为我的Google应用程序引擎运行单元测试时,我都会遇到安装错误,似乎找不到解决方案 以下是我当前的错误: org.springframework.context.ApplicationContextException: Unable to start embedded container; nested exception is java.lang.NoSuchMethodError: javax.servlet.ServletContext.addSer
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:132)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:474)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:117)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:689)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:321)
at org.springframework.boot.test.SpringApplicationContextLoader.loadContext(SpringApplicationContextLoader.java:103)
at org.springframework.test.context.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:68)
at org.springframework.test.context.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:86)
at org.springframework.test.context.DefaultTestContext.getApplicationContext(DefaultTestContext.java:72)
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:212)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:200)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:259)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:261)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:219)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:83)
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:163)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:78)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:212)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:68)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
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:189)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.selfInitialize(EmbeddedWebApplicationContext.java:221)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.access$000(EmbeddedWebApplicationContext.java:84)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext$1.onStartup(EmbeddedWebApplicationContext.java:205)
at org.springframework.boot.context.embedded.jetty.ServletContextInitializerConfiguration$Initializer.callInitializers(ServletContextInitializerConfiguration.java:96)
at org.springframework.boot.context.embedded.jetty.ServletContextInitializerConfiguration$Initializer.doStart(ServletContextInitializerConfiguration.java:85)
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:83)
at org.springframework.boot.context.embedded.jetty.JettyEmbeddedServletContainer.<init>(JettyEmbeddedServletContainer.java:72)
at org.springframework.boot.context.embedded.jetty.JettyEmbeddedServletContainerFactory.getJettyEmbeddedServletContainer(JettyEmbeddedServletContainerFactory.java:391)
at org.springframework.boot.context.embedded.jetty.JettyEmbeddedServletContainerFactory.getEmbeddedServletContainer(JettyEmbeddedServletContainerFactory.java:135)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.createEmbeddedServletContainer(EmbeddedWebApplicationContext.java:156)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:129)
... 35 common frames omitted
显然,您需要Servlet3.1(归功于)。但是,GAE最多只支持2.5。因此,我修改了pom.xml,根据配置文件在两者之间切换 此外,我无法让测试与jetty(GAE将jetty用于其本地开发服务器)一起工作,因此我也切换到tomcat进行测试 pom.xml更改:
<profiles>
<profile>
<id>build</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
</profile>
<profile>
<id>test</id>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</profile>
</profiles>
建造
真的
javax.servlet
servlet api
2.5
假如
org.springframework.boot
SpringBootStarterWeb
org.springframework.boot
弹簧启动机tomcat
测试
javax.servlet
javax.servlet-api
3.1.0
假如
org.springframework.boot
SpringBootStarterWeb
在这里内联发布POM以及来自mvn dependency:tree
的输出。我敢肯定,如果不是3.1,您至少需要Servlet 3.0。
--- maven-dependency-plugin:2.9:tree (default-cli) @ football-preds ---
com.football_preds:football-preds:war:1.0-SNAPSHOT
+- com.google.appengine:appengine-api-1.0-sdk:jar:1.9.28:compile
+- com.googlecode.objectify:objectify:jar:4.0.1:compile
| \- com.google.guava:guava:jar:14.0.1:compile
+- javax.servlet:servlet-api:jar:2.5:provided
+- org.springframework.boot:spring-boot-legacy:jar:1.0.2.RELEASE:compile
+- org.springframework.boot:spring-boot-starter-web:jar:1.2.7.RELEASE:compile
| +- org.springframework.boot:spring-boot-starter:jar:1.2.7.RELEASE:compile
| | +- org.springframework.boot:spring-boot:jar:1.2.7.RELEASE:compile
| | +- org.springframework.boot:spring-boot-autoconfigure:jar:1.2.7.RELEASE:compile
| | +- org.springframework.boot:spring-boot-starter-logging:jar:1.2.7.RELEASE:compile
| | | +- org.slf4j:jcl-over-slf4j:jar:1.7.12:compile
| | | | \- org.slf4j:slf4j-api:jar:1.7.12:compile
| | | +- org.slf4j:jul-to-slf4j:jar:1.7.12:compile
| | | +- org.slf4j:log4j-over-slf4j:jar:1.7.12:compile
| | | \- ch.qos.logback:logback-classic:jar:1.1.3:compile
| | | \- ch.qos.logback:logback-core:jar:1.1.3:compile
| | \- org.yaml:snakeyaml:jar:1.14:compile
| +- com.fasterxml.jackson.core:jackson-databind:jar:2.4.6:compile
| | +- com.fasterxml.jackson.core:jackson-annotations:jar:2.4.6:compile
| | \- com.fasterxml.jackson.core:jackson-core:jar:2.4.6:compile
| +- org.hibernate:hibernate-validator:jar:5.1.3.Final:compile
| | +- javax.validation:validation-api:jar:1.1.0.Final:compile
| | +- org.jboss.logging:jboss-logging:jar:3.1.3.GA:compile
| | \- com.fasterxml:classmate:jar:1.0.0:compile
| +- org.springframework:spring-core:jar:4.1.8.RELEASE:compile
| +- org.springframework:spring-web:jar:4.1.8.RELEASE:compile
| \- org.springframework:spring-webmvc:jar:4.1.8.RELEASE:compile
+- org.springframework.boot:spring-boot-starter-actuator:jar:1.2.7.RELEASE:compile
| \- org.springframework.boot:spring-boot-actuator:jar:1.2.7.RELEASE:compile
+- org.springframework.boot:spring-boot-starter-security:jar:1.2.7.RELEASE:compile
| +- org.springframework:spring-beans:jar:4.1.8.RELEASE:compile
| +- org.springframework:spring-context:jar:4.1.8.RELEASE:compile
| +- org.springframework:spring-expression:jar:4.1.8.RELEASE:compile
| +- org.springframework.security:spring-security-config:jar:3.2.8.RELEASE:compile
| | +- aopalliance:aopalliance:jar:1.0:compile
| | \- org.springframework.security:spring-security-core:jar:3.2.8.RELEASE:compile
| +- org.springframework.security:spring-security-web:jar:3.2.8.RELEASE:compile
| \- org.springframework:spring-aop:jar:4.1.8.RELEASE:compile
+- org.apache.commons:commons-lang3:jar:3.4:compile
+- commons-validator:commons-validator:jar:1.5.0:compile
| +- commons-beanutils:commons-beanutils:jar:1.9.2:compile
| +- commons-digester:commons-digester:jar:2.1:compile
| +- commons-logging:commons-logging:jar:1.2:compile
| \- commons-collections:commons-collections:jar:3.2.1:compile
+- commons-codec:commons-codec:jar:1.10:compile
+- joda-time:joda-time:jar:2.9.1:compile
+- junit:junit:jar:4.12:compile
| \- org.hamcrest:hamcrest-core:jar:1.3:compile
+- org.springframework.boot:spring-boot-starter-test:jar:1.2.7.RELEASE:test
| +- org.mockito:mockito-core:jar:1.10.19:test
| | \- org.objenesis:objenesis:jar:2.1:test
| +- org.hamcrest:hamcrest-library:jar:1.3:test
| \- org.springframework:spring-test:jar:4.1.8.RELEASE:test
+- com.google.appengine:appengine-api-labs:jar:1.9.28:test
+- com.google.appengine:appengine-testing:jar:1.9.28:test
+- javax.el:javax.el-api:jar:2.2.4:compile
+- org.glassfish.web:javax.el:jar:2.2.4:compile
\- org.springframework.boot:spring-boot-starter-jetty:jar:1.3.1.RELEASE:compile
+- org.eclipse.jetty:jetty-servlets:jar:9.2.13.v20150730:compile
| +- org.eclipse.jetty:jetty-continuation:jar:9.2.13.v20150730:compile
| +- org.eclipse.jetty:jetty-http:jar:9.2.13.v20150730:compile
| +- org.eclipse.jetty:jetty-util:jar:9.2.13.v20150730:compile
| \- org.eclipse.jetty:jetty-io:jar:9.2.13.v20150730:compile
+- org.eclipse.jetty:jetty-webapp:jar:9.2.13.v20150730:compile
| +- org.eclipse.jetty:jetty-xml:jar:9.2.13.v20150730:compile
| \- org.eclipse.jetty:jetty-servlet:jar:9.2.13.v20150730:compile
| \- org.eclipse.jetty:jetty-security:jar:9.2.13.v20150730:compile
| \- org.eclipse.jetty:jetty-server:jar:9.2.13.v20150730:compile
+- org.eclipse.jetty.websocket:websocket-server:jar:9.2.13.v20150730:compile
| +- org.eclipse.jetty.websocket:websocket-common:jar:9.2.13.v20150730:compile
| | \- org.eclipse.jetty.websocket:websocket-api:jar:9.2.13.v20150730:compile
| +- org.eclipse.jetty.websocket:websocket-client:jar:9.2.13.v20150730:compile
| \- org.eclipse.jetty.websocket:websocket-servlet:jar:9.2.13.v20150730:compile
| \- javax.servlet:javax.servlet-api:jar:3.1.0:compile
\- org.eclipse.jetty.websocket:javax-websocket-server-impl:jar:9.2.13.v20150730:compile
+- org.eclipse.jetty:jetty-annotations:jar:9.2.13.v20150730:compile
| +- org.eclipse.jetty:jetty-plus:jar:9.2.13.v20150730:compile
| | \- org.eclipse.jetty:jetty-jndi:jar:9.2.13.v20150730:compile
| +- javax.annotation:javax.annotation-api:jar:1.2:compile
| +- org.ow2.asm:asm:jar:5.0.1:compile
| \- org.ow2.asm:asm-commons:jar:5.0.1:compile
| \- org.ow2.asm:asm-tree:jar:5.0.1:compile
+- org.eclipse.jetty.websocket:javax-websocket-client-impl:jar:9.2.13.v20150730:compile
\- javax.websocket:javax.websocket-api:jar:1.0:compile
<profiles>
<profile>
<id>build</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
</profile>
<profile>
<id>test</id>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</profile>
</profiles>