gpt4 book ai didi

在 Spring Integration 中模拟 MessageHandler 时出现 java.lang.NoSuchMethodError

转载 作者:行者123 更新时间:2023-11-29 08:26:25 32 4
gpt4 key购买 nike

我有一个简单的 Spring Integration 配置可以工作

@Configuration
public class FTPIntegration {


@Bean
public MessageChannel outputIntegrationChannel(ErrorHandler errorHandler) {
return MessageChannels
.publishSubscribe()
.errorHandler(errorHandler)
.get();
}

@Bean
public IntegrationFlow ftpIntegration(ErrorHandler errorHandler, MessageHandler messageHandler) {
return IntegrationFlows
.from(outputIntegrationChannel(errorHandler))
.transform(Transformers.toJson())
.handle(messageHandler)
.get();
}
}

我想使用 Mockito 准备一些集成测试来模拟、监视 MessageHandler 和 ErrorHandler。所以这就是为什么我想在测试配置中替换它们。

@Profile("test")
@EnableIntegration
@Configuration
@Import(value = FTPIntegration.class)
public class TestConf {

@Bean
@Primary
public ErrorHandler errorHandler() {
return mock(ErrorHandler.class);
}

@Bean
@Primary
public MessageHandler messageHandler() {
return mock(MessageHandler.class);
}
}

并在我的测试课中使用它

@ActiveProfiles("test")
@RunWith(SpringRunner.class)
@SpringBootTest(classes = TestConf.class)
public class FTPIntegrationTest {

@Autowired
@Qualifier("outputIntegrationChannel")
private MessageChannel integrationChannel;
}

但是在初始化时我得到了一个错误

java.lang.NoSuchMethodError: org.mockito.MockingDetails.getMockCreationSettings()Lorg/mockito/mock/MockCreationSettings;

at org.springframework.boot.test.mock.mockito.MockReset.get(MockReset.java:107)
at org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener.resetMocks(ResetMocksTestExecutionListener.java:84)
at org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener.resetMocks(ResetMocksTestExecutionListener.java:70)
at org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener.beforeTestMethod(ResetMocksTestExecutionListener.java:57)
at org.springframework.test.context.TestContextManager.beforeTestMethod(TestContextManager.java:291)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:251)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
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)

是我的配置问题还是无法在Spring Integration中使用Mockito的组件?

当我使用一些简单的方法引用时,比如返回日志::错误或者日志::信息MessageErrorMessageHandler正常工作

我也试过用 MockIntegration.mockMessageHandler();但结果是相同的(异常(exception)),因为它在下面使用了 Mockito 的模拟。

最佳答案

Mockito 是 1.x 版本,与 5.x 版本的 Spring Integration 不兼容。将 Mockito 升级到 2.x (2.22.0) 解决了这个问题并与 Spring Integration (5.0.8.RELEASE) 一起工作。

关于在 Spring Integration 中模拟 MessageHandler 时出现 java.lang.NoSuchMethodError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52535222/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com