我不能@Spy黄瓜春天开机测试对象分两步定义

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

我在运行测试的黄瓜和春天启动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!
java spring-boot testing cucumber bdd
1个回答
2
投票

这是目前不可能的,因为@MockBean原来的步骤定义为豆类和不使用@SpyBean使用cucumber-springTestContextManager。还有就是Support @MockBean in cucumber-spring #1470的问题。这是待价而沽,如果有人想抓住它。

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