简体   繁体   中英

test module cannot load its application.yml file

I have a separate module for functional testing of the application module. Its structure is as below

└───functional-test
    ├───pom.xml
    └───src
        ├───test
            ├───java
            |   └─── some java testing class
            ├───resources
                └─── application.yml

Note that there is NO main folder. My pom file is something as below

     <build>
        <testResources>
            <testResource>
                <directory>${project.basedir}/src/test/resources</directory>
                <includes>
                    <include>**/*.yml</include>
                </includes>
            </testResource>
        </testResources>
    </build>

    <dependencies>
        <!-- test dependencies -->
        <dependency>
            <groupId>com.github.tomakehurst</groupId>
            <artifactId>wiremock-jre8</artifactId>
            <version>2.27.2</version>
        </dependency>
        <dependency>
            <groupId>io.rest-assured</groupId>
            <artifactId>rest-assured</artifactId>
            <version>4.5.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.assertj</groupId>
            <artifactId>assertj-core</artifactId>
            <version>3.19.0</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-junit-jupiter</artifactId>
            <version>3.8.0</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <version>2.7.0</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.testcontainers</groupId>
            <artifactId>junit-jupiter</artifactId>
            <version>1.17.3</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webflux</artifactId>
            <version>5.3.20</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>io.projectreactor.netty</groupId>
            <artifactId>reactor-netty-http</artifactId>
            <version>1.0.19</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>io.projectreactor.netty</groupId>
            <artifactId>reactor-netty-http</artifactId>
            <version>1.0.19</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>jakarta.validation</groupId>
            <artifactId>jakarta.validation-api</artifactId>
            <version>2.0.2</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.3.4</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>5.3.4</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <properties>
        <java.version>1.8</java.version>
    </properties>

Now, I put some thing below in the application.yml file as a test

abc:
  def: "123" 

I assume the following code should give me "123". However, it printed null.

@Component
@ConfigurationProperties("abc")
@TestPropertySource("classpath:application.yml")
@Data
public class TestProperties {

    private String def;
}


public class PropertyFuncTest extends BaseFunctionalTest {

    @Autowired
    TestProperties testProperties;

    @Test
    public void testProperty() {

        System.out.println(testProperties.getDef());
        ......
    }

}

@RunWith(SpringRunner.class)
@SpringBootTest
public abstract class BaseFunctionalTest {
    ......
}
   

It looks like the application.yml file is not loaded at all. Any idea?

My bad, the issue is in the pom, which was not shown in the description of the question. I accidentally copied <packaging>pom</packaging> into the pom.xml. After remove it (or change it to jar), the yml file was read successfully.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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