Nie znajduje klasy java.lang.NoClassDefFoundError: javax/servlet/SessionCookieConfig

0

Mam w projekcie zależności

	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")

       // zależności z modułu dziedziczonego (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")

chciałem zrobić testy integracyjne więc dodałem jeszcze zależności

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

Stworzyłem podstawową klasę konfiguracyjną

/**
 * Spring configuration class for integration tests.
 */
@Configuration 
@EnableAutoConfiguration 
@ComponentScan
public class PopcornCoreTestApplication {}

i klasę abstrakcyjną

/**
 * 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();
    }
}

i jakiś przykładowy test, aby sprawdzić czy działa

/**
 * 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));
    }
}

i nie działa. Pop uruchomienu testu otrzymuję

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

na pastebin cały błąd https://pastebin.com/8kC4Mkm6

Próbowałem dodać zależność (najnowsza wersja)

javax.servlet-api

ale nic to nie pomogło i dalej jest ten sam błąd.

Jak sobie z tym poradzić?

1

Ten servlet api to raczej było to, tylko trzeba odpowiedniej wersji.

// https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api
provided group: 'javax.servlet', name: 'javax.servlet-api', version: '3.1.0'
0

Dodałem najnowszą zależność

compile group: 'javax.servlet', name: 'javax.servlet-api', version: '4.0.1'

i co prawda z brakiem tej klasy pomogło, bo poprzednim razem musiałem zaciągać przestarzałą zależność.

Jednak teraz mam inny problem. Uruchamiam test, najpierw wyrzuca

lip 07, 2018 10:03:39 AM 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 07, 2018 10:03:39 AM 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 07, 2018 10:03:40 AM org.springframework.boot.test.context.SpringBootTestContextBootstrapper getTestExecutionListeners
INFO: Using TestExecutionListeners: [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@71b1176b, org.springframework.test.context.support.DirtiesContextTestExecutionListener@6193932a, org.springframework.test.context.transaction.TransactionalTestExecutionListener@647fd8ce, com.github.springtestdbunit.TransactionDbUnitTestExecutionListener@159f197]

potem ładuje część aplikacji Spring Boot

:: Spring Boot ::        (v2.0.1.RELEASE)

2018-07-07 10:03:46.184  INFO 3576 --- [           main] erPersistenceServiceImplIntegrationTests : Starting UserPersistenceServiceImplIntegrationTests on Jonki-PC with PID 3576 (started by Jonki in C:\Users\Jonki\Documents\GitHub\popcorn\popcorn-core)
2018-07-07 10:03:46.189  INFO 3576 --- [           main] erPersistenceServiceImplIntegrationTests : The following profiles are active: db-h2
2018-07-07 10:03:47.814  INFO 3576 --- [           main] o.s.w.c.s.GenericWebApplicationContext   : Refreshing org.springframework.web.context.support.GenericWebApplicationContext@6cb107fd: startup date [Sat Jul 07 10:03:47 CEST 2018]; root of context hierarchy
2018-07-07 10:03:59.246  INFO 3576 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
...

i wywala

java.lang.IllegalStateException: Failed to load ApplicationContext

https://pastebin.com/w5A7S8VF

Na SO https://stackoverflow.com/a/36608574/10033266 znalazłem, że jeśli aplikacja ma konfigurację na adnotacjach tak jak moja, to wystarczy dodać @SpringBootTest , ale ja mam przecież dodaną tą adnotację nad klasą DBUnitTestBase .

1

Twoim problemem jest że nie wstrzykuje ci org.springframework.mail.javamail.JavaMailSender beana. Nie widzi implementacji do niego.

Tak na szybko to daję 3 propozycje do przetestowania:

  1. Zobacz https://stackoverflow.com/questions/23246912/could-not-autowire-org-springframework-mail-javamail-javamailsender

  2. Czy twoja klasa com\jonki\popcorn\core\service\impl\MailServiceImpl jest widziana jako bean springowy? W spring boocie jest tak, że robi automatyczny package-scan ale tylko dla podpakietów od klasy głównej (w której jest main), więc musiałbyś dołożyć @ComponentScan w głównej klase, look https://dzone.com/articles/spring-spring-boot-and-component-scan

  3. Dodaj dependencję to którejś z wersji tego: https://mvnrepository.com/artifact/org.springframework/spring-context-support

0

Dzięki. Ten problem rozwiązany. Nie chodziło o żadną z twoich 3 propozycji, ale nakierowałeś mnie o co może chodzić. Tylko moduł web ma w resources plik application.yml z danymi logowania do poczty itd.

  mail:
      host: ...
      port: ...
      username: ...
      password: ...
      properties.mail.smtp:
        auth: true
        starttls.enable: true

a teraz testuje moduł core, który nie ma takiego pliku, więc musiałem go stworzyć i przekopiować te dane. Dzięki.

Zostają jeszcze te komunikaty, które pojawiają się na początku uruchomienia testu Springa

lip 07, 2018 10:55:02 AM 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 07, 2018 10:55:03 AM 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 07, 2018 10:55:04 AM org.springframework.boot.test.context.SpringBootTestContextBootstrapper getTestExecutionListeners
INFO: Using TestExecutionListeners: [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@71b1176b, org.springframework.test.context.support.DirtiesContextTestExecutionListener@6193932a, org.springframework.test.context.transaction.TransactionalTestExecutionListener@647fd8ce, com.github.springtestdbunit.TransactionDbUnitTestExecutionListener@159f197]

o co z nimi chodzi?

0

To tylko INFO, olej :)

1 użytkowników online, w tym zalogowanych: 0, gości: 1