I’m having a problem running cucumber test with Spring Boot 2.
I have two steps definitions and in both clases I try to spy an object with mockito trying to capture the argument passed to one method of this class.
The point is that since Cucumber only allows one Spring Application Context Configuration I’ve created an abstract class to configure it and I’ve extended one of the steps definition with this class.
@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()); } }
And everything works fine except that sendEventService is only recognized as a spy bean in the class that extends the BaseTestIntegration class the other one throws this exception:
org.mockito.exceptions.misusing.NotAMockException: Argument passed to verify() is of type SendEventSencondServiceImpl and is not a mock!
Advertisement
Answer
It’s currently not possible to use @MockBean
or @SpyBean
because cucumber-spring
turns step definitions into beans and does not use the TestContextManager
. There is an issue to Support @MockBean in cucumber-spring #1470. It’s up for grabs if anybody wants to take it.