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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot 和 Memcached 结合使用可以帮助你在 Java 应用程序缓存数据,提高性能。在 Spring Boot 配置 Memcached,通常会涉及以下几个步骤: 1. 添加依赖:首先,你需要在你的 `pom.xml` 或 `build.gradle` 文件添加 Memcached 的客户端库依赖。对于 Maven,可以使用以下依赖: ```xml <dependency> <groupId>net.spy</groupId> <artifactId>spymemcached</artifactId> <version>2.12.3</version> </dependency> ``` 2. 配置application.yml或application.properties文件:在配置文件(通常是 `application.yml`),添加Memcached的连接配置,如服务器地址、端口等: ```yml spring: cache: type: memcached memcached: servers: 'localhost:11211' # 或者其他 Memcached 服务的实际地址和端口 username: (可选) your_username password: (可选) your_password ``` 3. 使用 CacheTemplate:Spring Boot 提供了 `CacheTemplate` 类,你可以注入这个模板来存储和检索缓存的数据。例如: ```java @Autowired private CacheTemplate cacheTemplate; public void saveToCache(String key, Object value) { cacheTemplate.put(key, value); } public Object getFromCache(String key) { return cacheTemplate.get(key); } ``` 4. 注解支持:如果你想要基于注解的方式使用缓存,可以使用 `@Cacheable` 和 `@CacheEvict` 等。 相关问题-- 1. 在Spring Boot如何启用Memcached缓存? 2. 如何在Spring Boot应用使用@Autowired注入CacheTemplate? 3. 如何在Spring Boot的Controller方法上使用@Cacheable注解来缓存结果?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值