简体   繁体   English

Micronaut 测试模拟 bean 未能注入值

[英]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.我正在尝试测试一个依赖 bean 工作的类,我想模拟它。 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.这个 bean 需要一个字符串值,它是使用@ConfigurationProperties从我的application.yml文件接收的,这可能是问题所在,因为在同一个测试类中模拟的其他 bean 工作得很好。 Running the application normally also works just fine, so the error seems to be related to the @MockBean in some way.正常运行应用程序也可以正常工作,因此该错误似乎与@MockBean以某种方式有关。

I have this configuration class which gets the value from the application.yml :我有这个配置类,它从application.yml获取值:

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

Also, the value is set in the integrationTest application.yml file:此外,该值是在 integrationTest application.yml文件中设置的:

some_api:
  url: http://localhost:8082

And also this factory, which creates the bean:还有这个创建 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:但是,当我运行测试时,会在尝试初始化 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
@Data
@ConfigurationProperties("some_api")
public class SomeApiDaoConfig {
    private String url;
}

I'll assume @Data is the Lombok annotation and thus it's creating a constructor argument for the url .我假设@Data是 Lombok 注释,因此它为url创建了一个构造函数参数。 Micronaut did not support injecting configuration keys into constructor arguments until 1.3 and it requires the constructor be annotated with ConfigurationInject Micronaut 直到 1.3 才支持将配置键注入构造函数参数,它要求构造函数使用ConfigurationInject进行注释

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM