spring引导集成测试

lkaoscv7  于 2021-07-03  发布在  Java
关注(0)|答案(1)|浏览(355)

我一直在尝试为我编写的一个小型spring boot项目编写集成测试,但由于某些原因,似乎无法让集成测试正常工作。以下是我想做的:

package au.azzmosphere.integration;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = {IntegrationTestConfiguration.class})
public class TestIntegration1 {
    @Test
    public void testDummy() {

    }
}

配置类具有以下功能:

package au.azzmosphere.integration;

import au.azzmosphere.configuration.RobotAppConfig;
import org.springframework.context.annotation.Configuration;

@Configuration
public class IntegrationTestConfiguration extends RobotAppConfig {
}

当我尝试从intellij运行集成测试时,代码总是出现以下错误

14:54:01.537 [main] DEBUG org.springframework.test.context.junit4.SpringJUnit4ClassRunner - SpringJUnit4ClassRunner constructor called with [class au.azzmosphere.integration.TestIntegration1].
14:54:01.542 [main] INFO org.springframework.test.context.TestContext - @ContextConfiguration not found for class [class au.azzmosphere.integration.TestIntegration1].
14:54:01.549 [main] INFO org.springframework.test.context.TestContextManager - @TestExecutionListeners is not present for class [class au.azzmosphere.integration.TestIntegration1]: using defaults.

从gradle返回以下内容

au.azzmosphere.integration.TestIntegration1 > initializationError FAILED
    java.lang.NoClassDefFoundError
        Caused by: java.lang.ClassNotFoundException

完整堆栈跟踪是

java.lang.NoClassDefFoundError: org/springframework/transaction/TransactionDefinition

    at java.lang.Class.getDeclaredConstructors0(Native Method)
    at java.lang.Class.privateGetDeclaredConstructors(Class.java:2671)
    at java.lang.Class.getConstructor0(Class.java:3075)
    at java.lang.Class.getDeclaredConstructor(Class.java:2178)
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:102)
    at org.springframework.test.context.TestContextManager.retrieveTestExecutionListeners(TestContextManager.java:171)
    at org.springframework.test.context.TestContextManager.<init>(TestContextManager.java:108)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTestContextManager(SpringJUnit4ClassRunner.java:107)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.<init>(SpringJUnit4ClassRunner.java:79)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:104)
    at org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:86)
    at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
    at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:26)
    at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
    at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:33)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:49)
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: java.lang.ClassNotFoundException: org.springframework.transaction.TransactionDefinition
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 23 more
xuo3flqw

xuo3flqw1#

问题是我没有定义任何transactiondefinition,因为我没有使用它。默认的springboot注解尝试将其注入。答案是使用@testexecutionlisteners({dependencyinjectiontestexecutionlistener.class}),它覆盖默认值并使事情继续进行。

相关问题