我不能在 Cucumber Spring 中用 @Spy 对象分两步启动测试定义
I cannot @Spy Object in Cucumber Spring Boot Test in two Steps Definitions
我在使用 Spring Boot 2 进行黄瓜测试时遇到问题 运行。
我有两个步骤定义,在这两个类中我都尝试用
mockito 试图捕获传递给此 class.
的一种方法的参数
关键在于,由于 Cucumber 只允许一个 Spring 应用程序上下文配置,我创建了一个摘要 class 来配置它,并且我用这个 [= 扩展了其中一个步骤定义21=].
@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 class 的 class 中被识别为间谍 bean 之外一切正常,另一个抛出此异常:
org.mockito.exceptions.misusing.NotAMockException:
Argument passed to verify() is of type SendEventSencondServiceImpl and is not a mock!
目前无法使用 @MockBean
或 @SpyBean
,因为 cucumber-spring
将步骤定义转换为 beans 并且不使用 TestContextManager
。 Support @MockBean in cucumber-spring #1470 有问题。有谁要拿就拿去吧
我正面临这个问题。我使用了下面的解决方案(等待最好的主意):
我用通过 Mockito 创建的间谍 bean 替换了我的 bean :
YourInstanceYourWantSpy spyingInstance= Mockito.spy(YourInstanceYourWantSpy);
然后我使用 ReflectionUtils 替换它调用的实例。
我在使用 Spring Boot 2 进行黄瓜测试时遇到问题 运行。
我有两个步骤定义,在这两个类中我都尝试用 mockito 试图捕获传递给此 class.
的一种方法的参数关键在于,由于 Cucumber 只允许一个 Spring 应用程序上下文配置,我创建了一个摘要 class 来配置它,并且我用这个 [= 扩展了其中一个步骤定义21=].
@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 class 的 class 中被识别为间谍 bean 之外一切正常,另一个抛出此异常:
org.mockito.exceptions.misusing.NotAMockException:
Argument passed to verify() is of type SendEventSencondServiceImpl and is not a mock!
目前无法使用 @MockBean
或 @SpyBean
,因为 cucumber-spring
将步骤定义转换为 beans 并且不使用 TestContextManager
。 Support @MockBean in cucumber-spring #1470 有问题。有谁要拿就拿去吧
我正面临这个问题。我使用了下面的解决方案(等待最好的主意):
我用通过 Mockito 创建的间谍 bean 替换了我的 bean :
YourInstanceYourWantSpy spyingInstance= Mockito.spy(YourInstanceYourWantSpy);
然后我使用 ReflectionUtils 替换它调用的实例。