java - Micronaut:在 Spock 中模拟工厂创建的 Bean

标签 java spring mocking spock micronaut

我需要执行从 Micronaut 到 Spring 应用程序的远程调用。为了创建必要的 bean,我创建了一个工厂:

@Factory
public class RemotingConfig {

  @Bean
  @Singleton
  public OfferLeadService offerLeadService(@Value("${offer.server.remoting.base.url}") 
                                                 String offerRemotingBaseUrl) {
    HttpInvokerProxyFactoryBean invoker = new HttpInvokerProxyFactoryBean();
    invoker.setHttpInvokerRequestExecutor(new SimpleHttpInvokerRequestExecutor());
    invoker.setServiceUrl(offerRemotingBaseUrl + OfferLeadService.URI);
    invoker.setServiceInterface(OfferLeadService.class);
    invoker.afterPropertiesSet();
    return (OfferLeadService) invoker.getObject();
  }

  @Bean
  @Singleton
  public APIKeyService apiKeyService(@Value("${offer.server.remoting.base.url}") 
                                           String offerRemotingBaseUrl) {
    HttpInvokerProxyFactoryBean invoker = new HttpInvokerProxyFactoryBean();
    invoker.setHttpInvokerRequestExecutor(new SimpleHttpInvokerRequestExecutor());
    invoker.setServiceUrl(offerRemotingBaseUrl + APIKeyService.URI);
    invoker.setServiceInterface(APIKeyService.class);
    invoker.afterPropertiesSet();
    return (APIKeyService) invoker.getObject();
  }
}

在我的 Spock 集成测试中,我需要模拟这些 bean,我根据 Micronaut 文档进行了尝试:https://docs.micronaut.io/latest/guide/index.html#replaces

这导致了这样的测试:

@MicronautTest
class StackoverflowSpecification extends Specification {

  @Inject
  AuthorizedClient authorizedClient

  @Inject
  UnauthorizedClient unauthorizedClient

  @Inject
  OfferLeadService offerLeadService

  @Inject
  APIKeyService apiKeyService

  @Factory
  @Replaces(factory = RemotingConfig.class)
  static class RemotingConfigTest extends Specification {

    @Singleton
    OfferLeadService offerLeadService() {
      return Mock(OfferLeadService)
    }

    @Singleton
    APIKeyService apiKeyService() {
      return Mock(APIKeyService)
    }
  }

  void "authenticated sessions request returns 200 ok"() {

    when:
    HttpResponse response = authorizedClient.getSession("AA-BB-CC")

    then:
    response.status == OK

    and: 'setup mock calls'
    1 * apiKeyService.find(_, _) >> buildApiKeyVO()
    1 * offerLeadService.containsHipHavingPostalCode(_, _) >> true
    0 * _
  }

  void "authenticated sessions request with wrong passphrase returns 403 forbidden"() {

    when:
    unauthorizedClient.getSessionWithWrongPassphrase("AA-BB-CC")

    then:
    HttpClientResponseException ex = thrown(HttpClientResponseException)

    then:
    ex.status == FORBIDDEN

    and: 'setup mock calls'
    1 * apiKeyService.find(_, _) >> buildApiKeyVO()
    1 * offerLeadService.containsHipHavingPostalCode(_, _) >> false
    0 * _
  }

  private static APIKeyVO buildApiKeyVO() {
    APIKeyVO key = new APIKeyVO()
    key.setId(1L)
    key.setValue("123")
    key.setEnabled(true)
    key.setRoles(List.of("ROLE_STANDARD"))
    key.setValidUntil(Instant.now().plus(100, ChronoUnit.DAYS))
    key.setDescription("CBC App")
    key.setAccountId("CBC")
    return key
  }
}

此解决方案效果不佳。如果两个测试单独运行,则两个测试通过,但是同时运行它们会导致第二个测试失败(这里的顺序是相关的,所以如果第二个测试排在最前面,它就会通过)。

同时运行测试和调试时,我看到一旦在第一个测试中按预期调用了两个模拟,对模拟的所有后续调用都会导致 nullfalse 分别尽管指定了其他内容。

如何在集成测试中模拟通过 RemotingConfig 指定的两个 bean?

最佳答案

您没有正确使用 @Replaces 注释。 factory 成员并不打算单独使用,而是进一步限定被替换的类型。

  @Factory
  static class RemotingConfigTest extends Specification {

    @Singleton
    @Replaces(bean = OfferLeadService.class, factory = RemotingConfig.class)
    OfferLeadService offerLeadService() {
      return Mock(OfferLeadService)
    }

    @Singleton
    @Replaces(bean = APIKeyService.class, factory = RemotingConfig.class)
    APIKeyService apiKeyService() {
      return Mock(APIKeyService)
    }
  }

编辑:以上内容仍然适用,但是您希望模拟在测试执行之间被重置。上面的代码不会发生这种情况。您需要使用 @MockBean 注释,它是 micronaut-test 的一部分。

@MockBean(OfferLeadService.class)
OfferLeadService offerLeadService() {
    return Mock(OfferLeadService)
}

@MockBean(APIKeyService.class)
APIKeyService apiKeyService() {
    return Mock(APIKeyService)
}

关于java - Micronaut:在 Spock 中模拟工厂创建的 Bean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55634575/

相关文章:

java - 如何在 Espresso 测试中测试 word style "ITALIC"

java - Java如何在Jenkins中使用环境变量?

unit-testing - 如何正确模拟和单元测试

java - Maven 停止下载依赖项并显示请求目标的有效证书路径

java - Hibernate 错误的日期时间值

java - 如何在 Kotlin Spring 项目中使用 Java 主类?

java - 如何在反序列化时重新附加单例 Spring bean

testing - 用于测试目的的动态控制的 swagger/openapi 模拟服务器

angularjs - 找不到名称 'provide'

java - Spring Boot 应用程序开始使用所有 CPU