spring 找不到java.lang.NoClassDefFoundError类:javax / servlet / SessionCookieConfig

jxct1oxe  于 2024-01-05  发布在  Spring
关注(0)|答案(2)|浏览(154)

我在项目中有依赖项

  1. compile("org.springframework.boot:spring-boot-starter-data-jpa") {
  2. exclude group: "org.apache.tomcat", module: "tomcat-jdbc"
  3. exclude group: "org.hibernate", module: "hibernate-entitymanager"
  4. }
  5. compile("org.springframework.boot:spring-boot-starter-security")
  6. compile("org.springframework.boot:spring-boot-starter-mail")
  7. compile("org.springframework.boot:spring-boot-configuration-processor")
  8. compile("org.eclipse.persistence:org.eclipse.persistence.jpa")
  9. compile("org.eclipse.persistence:org.eclipse.persistence.jpa.modelgen.processor")
  10. compile("com.google.api-client:google-api-client")
  11. compile("com.google.oauth-client:google-oauth-client-jetty")
  12. compile("com.google.apis:google-api-services-drive")
  13. // dependencies from the inherited module (compile(project("..."))
  14. api("com.fasterxml.jackson.core:jackson-databind")
  15. api("org.hibernate.validator:hibernate-validator")
  16. api("commons-validator:commons-validator")
  17. api("org.apache.commons:commons-lang3")
  18. implementation("com.google.guava:guava")

字符串
我想做集成测试,所以我添加了依赖项

  1. testCompile("com.github.springtestdbunit:spring-test-dbunit:1.3.0")
  2. testCompile("org.dbunit:dbunit:2.5.4")


我创建了基本配置类

  1. /**
  2. * Spring configuration class for integration tests.
  3. */
  4. @Configuration
  5. @EnableAutoConfiguration
  6. @ComponentScan
  7. public class PopcornCoreTestApplication {}


一个抽象类

  1. /**
  2. * Base class to save on configuration.
  3. */
  4. @RunWith(SpringRunner.class)
  5. @SpringBootTest(classes = PopcornCoreTestApplication.class, webEnvironment = SpringBootTest.WebEnvironment.MOCK)
  6. @TestExecutionListeners(
  7. {
  8. DependencyInjectionTestExecutionListener.class,
  9. DirtiesContextTestExecutionListener.class,
  10. TransactionalTestExecutionListener.class,
  11. TransactionDbUnitTestExecutionListener.class
  12. }
  13. )
  14. public abstract class DBUnitTestBase {
  15. @Autowired
  16. private UserRepository userRepository;
  17. /**
  18. * Clean out the db after every test.
  19. */
  20. @After
  21. public void cleanup() {
  22. this.userRepository.deleteAll();
  23. }
  24. }


和一些示例测试来检查它是否有效

  1. /**
  2. * Integration tests for UserPersistenceServiceImpl.
  3. */
  4. public class UserPersistenceServiceImplIntegrationTests extends DBUnitTestBase {
  5. @Autowired
  6. private UserPersistenceService userPersistenceService;
  7. /**
  8. * Setup.
  9. */
  10. @Test
  11. public void setup() {
  12. Assert.assertThat(this.userRepository.count(), Matchers.is(0L));
  13. }
  14. }


它不工作。我得到了测试开始流行

  1. lip 04, 2018 6:30:10 PM org.springframework.boot.test.context.SpringBootTestContextBootstrapper buildDefaultMergedContextConfiguration
  2. INFO: Neither @ContextConfiguration nor @ContextHierarchy found for test class [com.jonki.popcorn.core.jpa.service.UserPersistenceServiceImplIntegrationTests], using SpringBootContextLoader
  3. lip 04, 2018 6:30:10 PM org.springframework.test.context.support.AbstractContextLoader generateDefaultLocations
  4. 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}.
  5. lip 04, 2018 6:30:11 PM org.springframework.boot.test.context.SpringBootTestContextBootstrapper getTestExecutionListeners
  6. 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]
  7. java.lang.NoClassDefFoundError: javax/servlet/SessionCookieConfig
  8. ...
  9. lip 04, 2018 6:30:12 PM org.springframework.test.context.TestContextManager prepareTestInstance
  10. 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]
  11. java.lang.NoClassDefFoundError: javax/servlet/SessionCookieConfig


pastebin上所有错误https://pastebin.com/8kC4Mkm6
我试图添加一个依赖项

  1. javax.servlet-api


但这并没有帮助,同样的错误仍然存在。
如何应对?

bweufnob

bweufnob1#

对于所有mvn用户,请将以下依赖项添加到pom.xml

  1. <dependency>
  2. <groupId>javax.servlet</groupId>
  3. <artifactId>javax.servlet-api</artifactId>
  4. <version>3.1.0</version>
  5. <scope>test</scope>
  6. </dependency>

字符串

tvmytwxo

tvmytwxo2#

SessionCookieConfig类出现在3.0版本的servlet-api中。

要修复您的问题,只需将此依赖项添加到build.gradle文件

  1. testCompile("javax.servlet:javax.servlet-api:3.1.0")

字符串

相关问题