title: Micronaut: Mock Factory-created Beans in Spock
body: <p>I need to perform remoting calls from Micronaut to a Spring Application. In order to create the necessary beans I created a Factory:</p>
  
  <pre class="lang-java prettyprint-override"><code>@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();
    }
  }
  </code></pre>
  
  <p>In my Spock integration test I need to mock these beans, which I tried according to the Micronaut docs: <a href="https://docs.micronaut.io/latest/guide/index.html#replaces" rel="nofollow noreferrer">https://docs.micronaut.io/latest/guide/index.html#replaces</a></p>
  
  <p>This resulted in a test like this:</p>
  
  <pre><code>@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(_, _) &gt;&gt; buildApiKeyVO()
      1 * offerLeadService.containsHipHavingPostalCode(_, _) &gt;&gt; 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(_, _) &gt;&gt; buildApiKeyVO()
      1 * offerLeadService.containsHipHavingPostalCode(_, _) &gt;&gt; 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
    }
  }
  </code></pre>
  
  <p>This solution is not working well. The two tests pass if they run in isolation, running both of them however, causes the second test to fail (the order is relevant here, so if the second test were to be on top, it would be the one passing).</p>
  
  <p>When running both tests and debugging, I see that once the two mocks have been invoked as expected in the first test, all subsequent calls to the mocks result in <code>null</code> and <code>false</code> respectively despite specifying something else.</p>
  
  <p>How do I go about mocking the two beans specified via <code>RemotingConfig</code> in the integration test?</p>