加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java-我无法通过两步定义在黄瓜Spring Boot测试中使用@Spy对象

发布时间:2020-12-14 19:25:43 所属栏目:Java 来源:网络整理
导读:我在使用Spring Boot 2运行黄瓜测试时遇到问题. 我有两个步骤的定义,在这两个方面中,我都试图用 Mockito试图捕获传递给此类的一个方法的参数. 关键是,由于Cucumber仅允许进行一个Spring Application Context Configuration,所以我创建了一个抽象类来对其进行

我在使用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有一个问题.如果有人愿意接受,那就可以争取.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读