我在使用Spring Boot 2运行黄瓜测试时遇到问题.
我有两个步骤的定义,在这两个方面中,我都试图用
Mockito试图捕获传递给此类的一个方法的参数.
关键是,由于Cucumber仅允许进行一个Spring Application Context Configuration,所以我创建了一个抽象类来对其进行配置,并为此类扩展了步骤定义之一.
@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类的类中被识别为间谍bean之外,另一个抛出此异常:
org.mockito.exceptions.misusing.NotAMockException:
Argument passed to verify() is of type SendEventSencondServiceImpl and is not a mock!
最佳答案
当前无法使用@MockBean或@SpyBean,因为Cucumber-spring将步骤定义转换为Bean,并且不使用TestContextManager. Support @MockBean in cucumber-spring #1470有一个问题.如果有人愿意接受,那就可以争取.