![](/img/trans.png)
[英]java.lang.NoClassDefFoundError: javax/servlet/ServletContext
[英]Does not find the java.lang.NoClassDefFoundError class: javax / servlet / SessionCookieConfig
我在項目中有依賴項
compile("org.springframework.boot:spring-boot-starter-data-jpa") {
exclude group: "org.apache.tomcat", module: "tomcat-jdbc"
exclude group: "org.hibernate", module: "hibernate-entitymanager"
}
compile("org.springframework.boot:spring-boot-starter-security")
compile("org.springframework.boot:spring-boot-starter-mail")
compile("org.springframework.boot:spring-boot-configuration-processor")
compile("org.eclipse.persistence:org.eclipse.persistence.jpa")
compile("org.eclipse.persistence:org.eclipse.persistence.jpa.modelgen.processor")
compile("com.google.api-client:google-api-client")
compile("com.google.oauth-client:google-oauth-client-jetty")
compile("com.google.apis:google-api-services-drive")
// dependencies from the inherited module (compile(project("..."))
api("com.fasterxml.jackson.core:jackson-databind")
api("org.hibernate.validator:hibernate-validator")
api("commons-validator:commons-validator")
api("org.apache.commons:commons-lang3")
implementation("com.google.guava:guava")
我想做集成測試所以我添加了依賴項
testCompile("com.github.springtestdbunit:spring-test-dbunit:1.3.0")
testCompile("org.dbunit:dbunit:2.5.4")
我創建了基本的配置類
/**
* Spring configuration class for integration tests.
*/
@Configuration
@EnableAutoConfiguration
@ComponentScan
public class PopcornCoreTestApplication {}
和一個抽象類
/**
* Base class to save on configuration.
*/
@RunWith(SpringRunner.class)
@SpringBootTest(classes = PopcornCoreTestApplication.class, webEnvironment = SpringBootTest.WebEnvironment.MOCK)
@TestExecutionListeners(
{
DependencyInjectionTestExecutionListener.class,
DirtiesContextTestExecutionListener.class,
TransactionalTestExecutionListener.class,
TransactionDbUnitTestExecutionListener.class
}
)
public abstract class DBUnitTestBase {
@Autowired
private UserRepository userRepository;
/**
* Clean out the db after every test.
*/
@After
public void cleanup() {
this.userRepository.deleteAll();
}
}
和一些示例測試來檢查它是否有效
/**
* Integration tests for UserPersistenceServiceImpl.
*/
public class UserPersistenceServiceImplIntegrationTests extends DBUnitTestBase {
@Autowired
private UserPersistenceService userPersistenceService;
/**
* Setup.
*/
@Test
public void setup() {
Assert.assertThat(this.userRepository.count(), Matchers.is(0L));
}
}
它不工作。 我正在測試開始彈出
lip 04, 2018 6:30:10 PM org.springframework.boot.test.context.SpringBootTestContextBootstrapper buildDefaultMergedContextConfiguration
INFO: Neither @ContextConfiguration nor @ContextHierarchy found for test class [com.jonki.popcorn.core.jpa.service.UserPersistenceServiceImplIntegrationTests], using SpringBootContextLoader
lip 04, 2018 6:30:10 PM org.springframework.test.context.support.AbstractContextLoader generateDefaultLocations
INFO: Could not detect default resource locations for test class [com.jonki.popcorn.core.jpa.service.UserPersistenceServiceImplIntegrationTests]: no resource found for suffixes {-context.xml, Context.groovy}.
lip 04, 2018 6:30:11 PM org.springframework.boot.test.context.SpringBootTestContextBootstrapper getTestExecutionListeners
INFO: Using TestExecutionListeners: [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@769e7ee8, org.springframework.test.context.support.DirtiesContextTestExecutionListener@5276e6b0, org.springframework.test.context.transaction.TransactionalTestExecutionListener@71b1176b, com.github.springtestdbunit.TransactionDbUnitTestExecutionListener@6193932a]
java.lang.NoClassDefFoundError: javax/servlet/SessionCookieConfig
...
lip 04, 2018 6:30:12 PM org.springframework.test.context.TestContextManager prepareTestInstance
SEVERE: Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@769e7ee8] to prepare test instance [com.jonki.popcorn.core.jpa.service.UserPersistenceServiceImplIntegrationTests@402bba4f]
java.lang.NoClassDefFoundError: javax/servlet/SessionCookieConfig
在 pastebin 所有錯誤https://pastebin.com/8kC4Mkm6
我試圖添加一個依賴項
javax.servlet-api
但它沒有幫助,同樣的錯誤仍然存在。
如何處理?
SessionCookieConfig類出現在 3.0 版的 servlet-api 中。
要解決您的問題,只需將此依賴項添加到您的build.gradle文件中
testCompile("javax.servlet:javax.servlet-api:3.1.0")
對於所有 mvn 用戶,請將以下依賴項添加到 pom.xml
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>test</scope>
</dependency>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.