【问题标题】:Getting spring application variables into Integration Tests将 spring 应用程序变量导入集成测试
【发布时间】:2021-02-09 21:20:08
【问题描述】:

我已经设置了用于测试我的 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
}

【问题讨论】:

    标签: spring spring-boot integration rest-assured properties-file


    【解决方案1】:

    我相信您正在寻找的是SpringBootTest 注释。在您的测试类顶部添加以下注释。

    @RunWith(SpringRunner.class)
    @SpringBootTest
    

    如果您需要,您可以将其他属性传递到 Spring 环境中,方法是将它们包含在 SpringBootTest 注释中,如

    @SpringBootTest(properties = "com.mycompany.myclass.someproperty=some_value")
    

    如果您尝试加载的属性位于配置文件特定的属性文件中,您可以使用激活该配置文件

    @ActiveProfiles(value = "my_profile")
    

    【讨论】:

    • 我将@RunWith(SpringRunner.class) @SpringBootTest 用于另一个项目并且有效,但是当用于该项目时,我得到:Failed to introspect Class [org.example.forex.service.CurrencyService] 和其他弹簧管理bean 的相同错误消息
    • 这是一个单独的问题,听起来好像类路径上缺少依赖项,但仅凭此评论无法确定。
    • 我在原帖中添加了 build.gradle 文件
    【解决方案2】:

    好的。感谢@lane.maxwell 提供的线索,我发现了缺少的依赖项。

    这些确实像@lane.maxwell 所说的那样工作:

    @RunWith(SpringRunner.class)
    @SpringBootTest
    

    基本上,我的应用程序使用数据库,我需要将数据库依赖项添加到 build.gradle 以使集成测试工作:

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

    【讨论】:

      猜你喜欢
      • 2010-11-21
      • 2017-12-21
      • 1970-01-01
      • 1970-01-01
      • 2017-01-17
      • 1970-01-01
      • 2015-02-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多