Spring-boot: Consider providing a shortcut for `TestPropertyValues`

Created on 30 May 2017  路  6Comments  路  Source: spring-projects/spring-boot

A lot of code has changed from

EnvironmentTestUtils.addEnvironment(this.context, "foo.bar:baz");

to

TestPropertyValues.of("foo.bar:baz").applyTo(this.context);

I'd like we consider a shortcut method that does the same thing as before, i.e.

TestPropertyValues.addTo(this.context, "foo.bar:baz")

My main concern is that the applyTo part can be easily missed (and it was). If we need some of the advanced features of this class, so be it, but I think 99% of our test suite needs to set a bunch of properties in a provided context.

declined

Most helpful comment

@shakuzen I've often done refactors like that in multiple steps:

1) Create a custom utility:

public class MyEnvironmentTestUtils {

    public static void addEnvironment(this.context, String... pairs) {
        TestPropertyValues.of(pairs).applyTo(this.context);
    }

}

2) Do a search/replace:

EnvironmentTestUtils.addEnvironment(this.context, "foo.bar:baz"); -> MyEnvironmentTestUtils.addEnvironment(this.context, "foo.bar:baz");

3) Use IDE refactoring

Use Refactor -> Inline on MyEnvironmentTestUtils.addEnvironment

All 6 comments

Sounds good to me. #9367 was caused by a missing call to applyTo.

Sounds good. We tried a few different options with that API and that was the best we could come up with.

Now that I've had more experience of using TestPropertyValues I like the API in its current form. I haven't forgotten applyTo and I haven't seen anyone else forget it either. IMO, we should close this one.

Ditto.

Sorry to chime in on a now closed issue. For me, the most difficulty is in replacing now deprecated code without forgetting the applyTo anywhere because it is not a simple find and replace.
I think newly written code is fine with the current API.

@shakuzen I've often done refactors like that in multiple steps:

1) Create a custom utility:

public class MyEnvironmentTestUtils {

    public static void addEnvironment(this.context, String... pairs) {
        TestPropertyValues.of(pairs).applyTo(this.context);
    }

}

2) Do a search/replace:

EnvironmentTestUtils.addEnvironment(this.context, "foo.bar:baz"); -> MyEnvironmentTestUtils.addEnvironment(this.context, "foo.bar:baz");

3) Use IDE refactoring

Use Refactor -> Inline on MyEnvironmentTestUtils.addEnvironment

Was this page helpful?
0 / 5 - 0 ratings