最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

quarkus - Is there a way to use @ConfigMapping in a test without @QuarkusTest? - Stack Overflow

programmeradmin1浏览0评论

I have an interface MyConfig that is using @ConfigMapping(prefix = "myconfig") and getting info from src/main/resources/application.properties.

I have an unit test for a component where I need to pass the MyConfig on its constructor. I prefer not mocking the config interface since I have many values (properties) that needs to be used.

So, I have tried to do what I found in the smallrye-config docs:

    final SmallRyeConfig sconfig = 
          ConfigProvider.getConfig().unwrap(SmallRyeConfig.class);
    final MyConfig config = sconfig.getConfigMapping(MyConfig.class);

But I'm getting a error:

java.util.NoSuchElementException: SRCFG00027: Could not find a mapping for com.natixis.matrisk.api.management.releases.config.ReleaseConfig
at io.smallrye.config.SmallRyeConfig.getConfigMapping(SmallRyeConfig.java:683)
at io.smallrye.config.SmallRyeConfig.getConfigMapping(SmallRyeConfig.java:673)

Please, what am I missing ?

I have an interface MyConfig that is using @ConfigMapping(prefix = "myconfig") and getting info from src/main/resources/application.properties.

I have an unit test for a component where I need to pass the MyConfig on its constructor. I prefer not mocking the config interface since I have many values (properties) that needs to be used.

So, I have tried to do what I found in the smallrye-config docs:

    final SmallRyeConfig sconfig = 
          ConfigProvider.getConfig().unwrap(SmallRyeConfig.class);
    final MyConfig config = sconfig.getConfigMapping(MyConfig.class);

But I'm getting a error:

java.util.NoSuchElementException: SRCFG00027: Could not find a mapping for com.natixis.matrisk.api.management.releases.config.ReleaseConfig
at io.smallrye.config.SmallRyeConfig.getConfigMapping(SmallRyeConfig.java:683)
at io.smallrye.config.SmallRyeConfig.getConfigMapping(SmallRyeConfig.java:673)

Please, what am I missing ?

Share Improve this question asked Mar 6 at 11:58 CristianoCristiano 1,8351 gold badge18 silver badges27 bronze badges 1
  • 1 Quarkus has support for component testing with (auto) mocking support, try this instead: quarkus.io/guides/testing-components – Serkan Commented Mar 10 at 17:31
Add a comment  | 

1 Answer 1

Reset to default 2

SmallRye Config has no auto discovery mechanism for mappings, when running standalone. You need to register the required mappings for the test manually. You can use a SmallRyeConfigBuilderCustomizer and register the mapping in the build directly via SmallRyeConfigBuilder#withMapping. Please check:

https://smallrye.io/smallrye-config/3.11.4/config/mappings/#registration

https://smallrye.io/smallrye-config/3.11.4/config/customizer/

发布评论

评论列表(0)

  1. 暂无评论