jeudi 6 février 2020

Micronaut Test mocked bean failing to inject values

I'm trying to test a class that depends on a bean to work, which I'd like to mock. This bean requires a string value, which is received from my application.yml file using @ConfigurationProperties and that is likely the problem since other beans mocked in the same test class work just fine. Running the application normally also works just fine, so the error seems to be related to the @MockBean in some way.

I have this configuration class which gets the value from the application.yml:

@Data
@ConfigurationProperties("some_api")
public class SomeApiDaoConfig {
    private String url;
}

Also, the value is set in the integrationTest application.yml file:

some_api:
  url: http://localhost:8082

And also this factory, which creates the bean:

@Factory
public class SomeApiDaoFactory {

  @Singleton
  public SomeApiDao someApiDao(SomeApiDaoConfig someApiDaoConfig) {
    return new SomeApiDao(someApiDaoConfig.getUrl());
  }
}

The test class is basically:

@MicronautTest(packages = {"<<path to someApiDao>>"})
public class ServiceTest {
  @Inject private BlockingStub blockingStub;
  @Inject private AnotherDao anotherDao;
  @Inject private SomeApiDao someApiDao;

@BeforeEach
  void setUp() {
    MockitoAnnotations.initMocks(this);
  }

... (tests)

@MockBean(AnotherDao.class)
  AnotherDao anotherDao() {
    return mock(AnotherDao.class);
  }

@MockBean(SomeApiDao.class)
  SomeApiDao someApiDao() {
    return mock(SomeApiDao.class);
  }

When I run the tests, however, this error pops up while it tries to initialize the SomeApiDao bean:

Failed to inject value for parameter [url] of class: <path to test>.$ServiceTest$SomeApiDao3Definition$Intercepted

Message: No bean of type [java.lang.String] exists. Make sure the bean is not disabled by bean requirements (enable trace logging for 'io.micronaut.context.condition' to check) and if the bean is enabled then ensure the class is declared a bean and annotation processing is enabled (for Java and Kotlin the 'micronaut-inject-java' dependency should be configured as an annotation processor).
Path Taken: new GrpcEmbeddedServer(ApplicationContext applicationContext,ApplicationConfiguration applicationConfiguration,GrpcServerConfiguration grpcServerConfiguration,[ServerBuilder serverBuilder],ApplicationEventPublisher eventPublisher,ComputeInstanceMetadataResolver computeInstanceMetadataResolver,List metadataContributors) --> ServerBuilder.serverBuilder(GrpcServerConfiguration configuration,[List serviceList],List interceptors,List serverTransportFilters) --> new Service([SomeApiDao someApiDao]) --> new $ServiceTest$SomeApiDao3Definition$Intercepted([String url],BeanContext beanContext,Qualifier qualifier,Interceptor[] interceptors)
io.micronaut.context.exceptions.DependencyInjectionException: Failed to inject value for parameter [url] of class: <path to test>.$ServiceTest$SomeApiDao3Definition$Intercepted

Aucun commentaire:

Enregistrer un commentaire