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

java - Override a single property for all tests - Stack Overflow

programmeradmin6浏览0评论

I have a set of properties in src/main/resources/application.properties:

app.firstprop=2
app.secondprop=3
app.thirdprop=5
app.fourthprop=7

And I want to override app.secondprop for all my tests, while keeping the remaining properties unchanged. One option is:

@SpringBootTest(properties=["app.secondprop=99"])

but that means I have to repeat this property value in every single test file.

Another option is to use src/test/resources/application.properties to override my properties file. Unfortunately, I have to specify all the unchanged properties as well as the overridden property:

app.firstprop=2
app.secondprop=99
app.thirdprop=5
app.fourthprop=7

If I don't specify the unchanged properties, they won't be defined during the tests. This again is unwanted repetition.

How do I override a single property for all tests and keep to the DRY principle?

I have a set of properties in src/main/resources/application.properties:

app.firstprop=2
app.secondprop=3
app.thirdprop=5
app.fourthprop=7

And I want to override app.secondprop for all my tests, while keeping the remaining properties unchanged. One option is:

@SpringBootTest(properties=["app.secondprop=99"])

but that means I have to repeat this property value in every single test file.

Another option is to use src/test/resources/application.properties to override my properties file. Unfortunately, I have to specify all the unchanged properties as well as the overridden property:

app.firstprop=2
app.secondprop=99
app.thirdprop=5
app.fourthprop=7

If I don't specify the unchanged properties, they won't be defined during the tests. This again is unwanted repetition.

How do I override a single property for all tests and keep to the DRY principle?

Share Improve this question asked 9 hours ago k314159k314159 11.2k1 gold badge25 silver badges63 bronze badges 1
  • 1 Try renaming src/test/resources/application.properties to src/test/resources/application-test.properties, which has only the one overridden property value. And in each test class, add @ActiveProfiles("test"). Assuming maven and to avoid the @ActiveProfile in each test class, create a test maven profile that is active by default (this might have other implications though). – Andrew S Commented 8 hours ago
Add a comment  | 

2 Answers 2

Reset to default 1

Have application-test.properties file with only the property you want to override, and set active profile to test.

Spring boot automatically merges properties from application-test.properties and application.properties files. Property mentioned in application-test.properties overrides the property mentioned in application.properties.

See this post about setting active profile: Setting the default active profile in Spring-boot

If you want a specific property value in all your tests, then follow Andrew S's suggestion:

  • override test props in application-test.properties (no need to specify all props)
  • activate profile test via @ActiveProfiles("test")

If you want a specific property value in some tests, then use test class inheritance:

  • create a parent test class, annotate it with @SpringBootTest(properties=["app.secondprop=99"])
  • extend or implement this parent test class in test classes where you want to have the value.
    Also you are still can override this prop in child test classes.
@SpringBootTest(properties = {"app.secondprop=99"})
interface ParentTest {
}

class ChildTest implements ParentTest {

    @Value("${app.secondprop}")
    private String prop2nd;

    @Test
    void test() {
        assertEquals("99", prop2nd);
    }
}

// here you implement ParentTest, but overrides the prop's value
@SpringBootTest(properties = {"app.secondprop=199"})
class ChildTestWithOwnValue implements ParentTest {

    @Value("${app.secondprop}")
    private String prop2nd;

    @Test
    void test() {
        assertEquals("199", prop2nd);
    }
}
发布评论

评论列表(0)

  1. 暂无评论