我在运行测试的黄瓜和春天启动2个问题。
我有两个步骤,定义和这两类我试图窥探的对象与尝试的Mockito捕捉传递给这个类的一个方法的参数。
问题的关键是,由于黄瓜只允许一个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类另一个抛出该异常的类间谍豆:
org.mockito.exceptions.misusing.NotAMockException:
Argument passed to verify() is of type SendEventSencondServiceImpl and is not a mock!
这是目前不可能的,因为@MockBean
原来的步骤定义为豆类和不使用@SpyBean
使用cucumber-spring
或TestContextManager
。还有就是Support @MockBean in cucumber-spring #1470的问题。这是待价而沽,如果有人想抓住它。