24

Consider the following example:

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
    properties = {
        "some.property=valueA"
    })
public class ServiceTest {
    @Test
    public void testA() { ... }

    @Test
    public void testB() { ... }

    @Test
    public void testC() { ... }
}

I'm using SpringBootTest annotation's properties attribute to set some.property property's value for all tests in this test suite. Now I'd like to set another value of this property for one of this tests (let's say testC) without affecting the others. How can I achieve this? I've read the "Testing" chapter of Spring Boot docs, but I haven't found anything that'd match my use case.

korolar
  • 1,220
  • 1
  • 12
  • 19
  • If you need to configure just a few properties, you can use the new @DynamicPropertySource annotation. https://stackoverflow.com/a/60941845/8650621 – Felipe Desiderati Mar 31 '20 at 01:01

3 Answers3

16

Your properties are evaluated by Spring during the Spring context loading.
So you cannot change them after the container has started.

As workaround, you could split the methods in multiple classes that so would create their own Spring context. But beware as it may be a bad idea as tests execution should be fast.

A better way could be having a setter in the class under test that injects the some.property value and using this method in the test to change programmatically the value.

private String someProperty;

@Value("${some.property}")
public void setSomeProperty(String someProperty) {
    this.someProperty = someProperty;
}
davidxxx
  • 115,998
  • 20
  • 192
  • 199
10

Update

Possible at least with Spring 5.2.5 and Spring Boot 2.2.6

@DynamicPropertySource
static void dynamicProperties(DynamicPropertyRegistry registry) {
    registry.add("some.property", () -> "valueA");
}
timguy
  • 1,633
  • 1
  • 18
  • 32
2

Just another solution in case you are using @ConfigurationProperties:

@Test
void do_stuff(@Autowired MyProperties properties){
  properties.setSomething(...);
  ...
}
Sam
  • 1,704
  • 17
  • 30
  • I think that this would be changed for all other tests in this class as well if this method runs first. – Wim Deblauwe Feb 16 '21 at 07:36
  • I guess so but we could also autowire the properties into the setup method and set up the defaults there – Sam Feb 17 '21 at 02:47