升级到camel-test-spring-junit5后,所有Autowired和MockBean注入都不起作用

问题描述 投票:0回答:1

我正在将旧应用程序从camel 2.22.0升级到camel 3.14.10(是的,我迟到了,我知道)。

这包括从camel-test-spring更新到camel-test-spring-junit5。

对注释和其他一些内容进行一些更改后,我再次进行了编译构建。但测试全部失败,原因是我的 @Autowired 和 @MockBean 属性实际上都不包含任何内容。它们全部未初始化。

这是测试现在和之前的样子的简短示例。

现在(注射不起作用):

@CamelSpringBootTest
@ActiveProfiles("test")
@TestPropertySource(properties = ["ftp.enabled=true"])
@SpringBootTest(webEnvironment = RANDOM_PORT)
@DirtiesContext
class ImageGatewayApplicationTest {

    @MockBean
    private lateinit var authenticationService: AuthenticationService
    @MockBean
    private lateinit var panoramaService: PanoramaService
    @MockBean
    private lateinit var bmetryService: BmetryEventService
    @Autowired
    private lateinit var template: ProducerTemplate
    @Autowired
    private lateinit var restTemplate: TestRestTemplate

    @EndpointInject(uri = "mock:new-image")
    private lateinit var newImageMock: MockEndpoint

    @Test
    fun `upload with ftp`() {
        val resource = ClassPathResource(TEST_PNG)
        val tempFile = FileHelper.createTempFile(resource)
        template.sendBodyAndHeader(TEST_FTP, tempFile, FILE_NAME, TEST_PNG) // <- template not initialised
        // verify mock
        verify<AuthenticationService>(authenticationService).checkCredentials(TEST_USER, TEST_PW)
        // clean up
        tempFile.delete()
    }

以下是更新前的注释。除了注释没有改变(注入工作得很好):

@RunWith(CamelSpringRunner::class)
@ActiveProfiles("test")
@TestPropertySource(properties = ["ftp.enabled=true"])
@SpringBootTest(webEnvironment = RANDOM_PORT)
@DirtiesContext
class ImageGatewayApplicationTest { }

我得到的确切错误是这样的:

kotlin.UninitializedPropertyAccessException: lateinit property restTemplate has not been initialized

请注意,它并不是抱怨找不到 beans 或无法实例化它们或任何人们可能期望的东西。就好像注释被简单地忽略了一样。当我暂停代码并查看该类时,它们都没有初始化。没有注入任何东西。

我半信半疑我缺少了some注释,但我找不到它。我尝试添加@EnableAutoConfiguration,@ContextConfiguration和@UseAdviceWith(因为实际上在一些测试中使用了),但都没有结果。

其他数据:事实证明它在 JUnit4 上运行

在 sidgates 评论的提示下,我查看了完整的堆栈跟踪。我什至不知道还有更多内容,Intellij 出于某种原因认为默认情况下适合隐藏它。这揭示了一些相当令人惊讶的事情,并且可能解释了我的困境:整个事情仍然在 JUnit4 上运行!

kotlin.UninitializedPropertyAccessException: lateinit property restTemplate has not been initialized

    at webcam.yellow.gateway.ImageGatewayApplicationTest.upload with http(ImageGatewayApplicationTest.kt:82)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:78)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:567)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
    at org.junit.runners.BlockJUnit4ClassRunner$1.evaluate(BlockJUnit4ClassRunner.java:100)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:366)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:103)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:63)
    at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329)
    at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293)
    at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:413)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
    at org.junit.vintage.engine.execution.RunnerExecutor.execute(RunnerExecutor.java:42)
    at org.junit.vintage.engine.VintageTestEngine.executeAllChildren(VintageTestEngine.java:80)
    at org.junit.vintage.engine.VintageTestEngine.execute(VintageTestEngine.java:72)
    at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:107)
    at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:88)
    at org.junit.platform.launcher.core.EngineExecutionOrchestrator.lambda$execute$0(EngineExecutionOrchestrator.java:54)
    at org.junit.platform.launcher.core.EngineExecutionOrchestrator.withInterceptedStreams(EngineExecutionOrchestrator.java:67)
    at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:52)
    at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:114)
    at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:86)
    at org.junit.platform.launcher.core.DefaultLauncherSession$DelegatingLauncher.execute(DefaultLauncherSession.java:86)
    at org.junit.platform.launcher.core.SessionPerRequestLauncher.execute(SessionPerRequestLauncher.java:53)
    at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:57)
    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:232)
    at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:55)

这是朝着正确方向迈出的一步,但是尽我所能,我无法让它与 Junit5 一起运行。老实说,我以为它已经支持了,因为 spring-boot 版本(2.7.18)确实支持它(spring-boot 2.2+)。

事实证明,旧的 kotlin-test 依赖项强制 junit4 运行。一旦我用更新的 kotest-junit5-runner 依赖项替换它,测试最终就会运行。不过,他们还没有检查过,骆驼似乎还有很多问题,但看起来这是另一种动物......

spring-boot kotlin apache-camel junit5
1个回答
0
投票

事实证明,虽然我假设由于 spring-boot 版本,测试在 JUnit5 上运行,但实际上它们仍然在 junit-vintage 上运行。

造成这种情况的是对 kotlintest 的另一个过时的依赖。一旦我用 kotest-runner-junit5-jvm 替换它,问题就消失了。

骆驼测试仍然存在很多问题,但这是另一个问题。

© www.soinside.com 2019 - 2024. All rights reserved.