Search code examples
jboss-arquillianopen-libertymicroprofileliberty-maven-plugin

How to use MicroProfile ConfigProperty injection from a microprofile-config.properties file in Open Liberty test using ShrinkWrap + Arquillian?


Problem

I added a microprofile-config.properties file to the Liberty "Testing microservices with the Arquillian managed container" guide sample, but my microprofile-config.properties isn't picked up by my test.

Symptom

> Exception : io.smallrye.config.inject.ConfigException: SRCFG02000:
> Failed to Inject @ConfigProperty for key serviceName into
> io.openliberty.guides.system.AppConfig.serviceName since the config
> property could not be found in any config source at
> io.smallrye.config.inject.ConfigExtension.validate(ConfigExtension.java:183)
> at
> io.openliberty.microprofile.config.internal.extension.OLSmallRyeConfigExtension.validate(OLSmallRyeConfigExtension.java:65)
> at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
> ...

Starting point

microprofile-config.properties

Path: src/main/resources/META-INF/microprofile-config.properties

serviceName=myService

Bean to inject into: AppConfig.java

@ApplicationScoped
public class AppConfig {

  @Inject @ConfigProperty(name="serviceName")
  private String serviceName;

   ...
}

Liberty server config (server.xml)

    <featureManager>
        <feature>restfulWS-3.0</feature>
        <feature>jsonb-2.0</feature>
        <feature>jsonp-2.0</feature>
        <feature>cdi-3.0</feature>
        <feature>mpConfig-3.0</feature>
        <!--Enable the following features to run tests with Arquillian managed container-->
        <feature>localConnector-1.0</feature>
        <feature>servlet-5.0</feature>
    </featureManager>

Solution

  • You need to specifically package the microprofile-config.properties file in the ShrinkWrap package like:

        .addAsManifestResource(new File("src/main/resources/META-INF", "microprofile-config.properties"))
    

    More completely in the context of this sample it would look like:

            WebArchive archive = ShrinkWrap.create(WebArchive.class, WARNAME)
                .addAsManifestResource(new File("src/main/resources/META-INF", "microprofile-config.properties"))
                    .addPackages(true, "io.openliberty.guides.system");
    

    Explanation:

    Since the sample uses ShrinkWrap to package the application test deployment, the microprofile-config.properties must be programmatically added to the ShrinkWrap deployment. It doesn't become part of the package by virtue of being in src/main/resources (like it becomes part of a standard Maven WAR package built by the maven-war-plugin).