将Spring应用程序变量纳入集成测试

问题描述

我已经设置了用于测试API的集成测试,并且希望我的测试从application.yml中获取设置。问题是我的测试类从不从属性文件中读取 $ {base-urls.test-url} 的值。我尝试使用@ ContextConfiguration,@ TestPropertySource,@ PropertySource和@RunWith(SpringRunner.class)失败。

如何获得集成测试以读取属性文件?

我的项目目录如下:

|____src
| |____test
| | |____resources
| | |____java
| | | |____org
| | | | |____example
| | | | | |____forex
| | | | | | |____controller
| | | | | | | |____CurrencyControllerTest.java
| | | | | | |____service
| | | | | | | |____CurrencyServiceTest.java
| |____integrationTest
| | |____resources
| | | |____application.yml
| | |____java
| | | |____org
| | | | |____example
| | | | | |____forex
| | | | | | |____integration
| | | | | | | |____ApiTests.java
| | | | | | |____domain
| | | | | | | |____CurrencyHelper.java
| |____main
| | |____resources
| | | |____application.yml
| | |____java
| | | |____org
| | | | |____example
| | | | | |____Application.java
| | | | | |____forex
| | | | | | |____repository
| | | | | | | |____CurrencyRepository.java
| | | | | | |____controller
| | | | | | | |____CurrencyController.java
| | | | | | |____service
| | | | | | | |____CurrencyService.java
| | | | | | |____domain
| | | | | | | |____Currency.java

我有一个应用程序资源文件src / integrationTest / resources / application.yml,并且还尝试将其放在与ApiTests类相同的程序包中。该文件包含以下内容:

base-urls:
  test-url: ${TEST_URL:http://localhost:8080}

设置是我想要一个正在运行的应用程序,然后使用@Value批注中的URL运行集成测试。我的测试课程使用Rest Assured,看起来像这样:

public class ApiTests {

    @Value("${base-urls.test-url}")
    private String baseUrl;

    private Currency dummy;

    @Before
    public void setup() {
        RestAssured.baseURI = baseUrl;
        dummy = CurrencyHelper.dummy();
    }

我的build.gradle文件如下:

plugins {
    id 'java'
    id 'application'
    id 'io.spring.dependency-management' version '1.0.7.RELEASE'
    id 'org.springframework.boot' version '2.3.4.RELEASE'
}

apply plugin: 'java'
apply plugin: 'application'
apply plugin: 'io.spring.dependency-management'
apply plugin: 'org.springframework.boot'

group 'org.example.forex'
version '1.0-SNAPSHOT'
sourceCompatibility = '1.8'
mainClassName = 'org.example.Application'

sourceSets {
    integrationTest {
        resources {
            srcDir 'src/integrationTest/resources'
        }
        compileClasspath += sourceSets.main.output + configurations.testRuntime
        runtimeClasspath += output + compileClasspath
    }
}

def versions = [
        postgresql         : '42.2.2'
]

repositories {
    mavenCentral()
}

dependencies {
    implementation "org.springframework.boot:spring-boot-starter"
    implementation "org.springframework.boot:spring-boot-starter-web"
    implementation'org.springframework.boot:spring-boot-starter-data-jpa'
    implementation "org.postgresql:postgresql:${versions.postgresql}"

    testImplementation "org.springframework.boot:spring-boot-starter-test"
    testImplementation group: 'junit',name: 'junit',version: '4.12'

    integrationTestImplementation "org.springframework.boot:spring-boot-starter-test"
    integrationTestImplementation "org.springframework:spring-test"
    integrationTestImplementation 'io.rest-assured:rest-assured:3.3.0'
    integrationTestImplementation "com.fasterxml.jackson.core:jackson-databind:2.11.0"
    integrationTestImplementation 'junit:junit:4.12'
}

task integrationTest(type: Test) {
    description = 'Runs integration tests.'
    group = 'verification'

    testClassesDirs = sourceSets.integrationTest.output.classesDirs
    classpath = sourceSets.integrationTest.runtimeClasspath
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)