简体   繁体   中英

Errors when try to use TestEntityManager in my functional test

I am trying to write some functional testings with some data pre-inserted by TestEntityManager. Below is my code

@RunWith(SpringRunner.class)
@SpringBootTest(
        classes = ApplicationFunctionalTests.class,
        webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@DataJpaTest
public abstract class BaseFunctionalTest {

    @Autowired
    protected TestEntityManager testEntityManager;

    .....

}

However, when I try to run the testing code, I got the following error:

Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/data/repository/config/BootstrapMode
    at java.lang.Class.getDeclaredMethods0(Native Method)
    at java.lang.Class.privateGetDeclaredMethods(Class.java:2729)
    at java.lang.Class.getDeclaredMethods(Class.java:2003)
    at sun.reflect.annotation.AnnotationType$1.run(AnnotationType.java:112)
    at sun.reflect.annotation.AnnotationType$1.run(AnnotationType.java:109)
    at java.security.AccessController.doPrivileged(Native Method)
    at sun.reflect.annotation.AnnotationType.<init>(AnnotationType.java:109)
    at sun.reflect.annotation.AnnotationType.getInstance(AnnotationType.java:85)
    at sun.reflect.annotation.AnnotationParser.parseAnnotation2(AnnotationParser.java:266)
    at sun.reflect.annotation.AnnotationParser.parseAnnotations2(AnnotationParser.java:120)
    at sun.reflect.annotation.AnnotationParser.parseAnnotations(AnnotationParser.java:72)
    at java.lang.Class.createAnnotationData(Class.java:3549)
    at java.lang.Class.annotationData(Class.java:3538)
    at java.lang.Class.createAnnotationData(Class.java:3554)
    at java.lang.Class.annotationData(Class.java:3538)
    at java.lang.Class.getAnnotation(Class.java:3443)
    at com.intellij.junit4.JUnit4TestRunnerUtil.buildRequest(JUnit4TestRunnerUtil.java:131)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:47)
    at com.intellij.rt.junit.IdeaTestRunner$Repeater$1.execute(IdeaTestRunner.java:38)
    at com.intellij.rt.execution.junit.TestsRepeater.repeat(TestsRepeater.java:11)
    at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:35)
    at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:235)
    at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:54)
Caused by: java.lang.ClassNotFoundException: org.springframework.data.repository.config.BootstrapMode
    at java.net.URLClassLoader.findClass(URLClassLoader.java:387)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:418)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:352)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
    ... 23 more

My pom.xml is as below:

<dependencies>
    <!-- test dependencies -->
    <dependency>
        <groupId>com.github.tomakehurst</groupId>
        <artifactId>wiremock-jre8</artifactId>
        <version>2.27.2</version>
    </dependency>
    <dependency>
        <groupId>io.rest-assured</groupId>
        <artifactId>rest-assured</artifactId>
        <version>4.5.1</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.assertj</groupId>
        <artifactId>assertj-core</artifactId>
        <version>3.19.0</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.mockito</groupId>
        <artifactId>mockito-junit-jupiter</artifactId>
        <version>3.8.0</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <version>2.7.0</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.testcontainers</groupId>
        <artifactId>junit-jupiter</artifactId>
        <version>1.17.3</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webflux</artifactId>
        <version>5.3.20</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>io.projectreactor.netty</groupId>
        <artifactId>reactor-netty-http</artifactId>
        <version>1.0.19</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>io.projectreactor.netty</groupId>
        <artifactId>reactor-netty-http</artifactId>
        <version>1.0.19</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>jakarta.validation</groupId>
        <artifactId>jakarta.validation-api</artifactId>
        <version>2.0.2</version>
        <scope>test</scope>
    </dependency>
</dependencies>

<properties>
    <java.version>1.8</java.version>
</properties>

Please help. Thanks.

You shouldn't combine @SpringBootTest and @DataJpaTest , it's either or. @SpringBootTest creates the whole application context (with adjustments for testing) whereas @DataJpaTest only creates the JPA layer of the Spring application and executes every test by default in a transaction. Check the documentation provided with the links for more details.

Moreover, if you specify classes = ApplicationFunctionalTests.class in the @SpringBootTest annotation, you limit the application context basically to only this class which is most likely not enough for a working application context.

I suggest you study the Spring documentation on Testing Spring Boot Applications and Auto-configured Data JPA Tests .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM