gpt4 book ai didi

java - 我无法通过两步定义在 Cucumber Spring Boot 测试中使用 @Spy 对象

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:06:20 29 4
gpt4 key购买 nike

我在使用 Spring Boot 2 运行 cucumber 测试时遇到问题。

我有两个步骤定义,在这两个类中我都尝试用mockito 试图捕获传递给此类的一个方法的参数。

要点是,由于 Cucumber 只允许一个 Spring 应用程序上下文配置,我创建了一个抽象类来配置它,并且我已经使用此类扩展了其中一个步骤定义。

@ActiveProfiles("INTEGRATION_TEST")
@SpringBootTest
@ContextConfiguration(classes = ServiceApplication.class)
@TestPropertySource(properties =
{"spring.config.location=classpath:application-test.yml"})
public abstract class BaseTestIntegration {}


@Ignore
public class OfferEventsGenerationStep extends BaseTestIntegration {

@Autowired private LoanOfferBatchController loanOfferBatchController;
@SpyBean private SendEventOfferServiceImpl sendEventService;
@Captor private ArgumentCaptor<CreateOfferToUserEvent> createOfferEventCaptor;
@Autowired private GenericWebApplicationContext context;
.........
@Then("^events will be created$")
public void eventsWillBeCreated() throws Throwable {
Mockito.verify(sendEventService, Mockito.times(5)).sendEvent(createOfferEventCaptor.capture());
}
}


@Ignore
public class SecondEventsGenerationStep {

@Autowired private LoanOfferBatchController loanOfferBatchController;
@SpyBean private SendEventSencondServiceImpl sendEventService;
@Captor private ArgumentCaptor<CreateOfferToUserEvent> createOfferEventCaptor;
@Autowired private GenericWebApplicationContext context;
.........
@Then("^events will be created$")
public void eventsWillBeCreated() throws Throwable {
Mockito.verify(sendEventService, Mockito.times(2)).sendEvent(createOfferEventCaptor.capture());
}
}

一切正常,除了 sendEventService 仅在扩展 BaseTestIntegration 类的类中被识别为 spy bean,另一个抛出此异常:

 org.mockito.exceptions.misusing.NotAMockException: 
Argument passed to verify() is of type SendEventSencondServiceImpl and is not a mock!

最佳答案

目前无法使用@MockBean@SpyBean 因为cucumber-spring 将步骤定义转换为bean 并且不使用TestContextManagerSupport @MockBean in cucumber-spring #1470 有问题.如果有人想拿走它,就可以拿走。

关于java - 我无法通过两步定义在 Cucumber Spring Boot 测试中使用 @Spy 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54498933/

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