geco17
geco17

Reputation: 5294

junit integration test spring property not resolved

I have a simple intergration test that looks like this

@RunWith(SpringRunner.class)
@SpringBootTest(classes = MyIntTestConfig.class, properties = {some.property.key=value})
public class MyIntTest {
    @Autowired
    public MyComponent myComponent;

    // ...

}

@Configuration
public class MyIntTestConfig {
    @MockBean
    private MyComponent myComponent;
    @Bean
    public myComponent() {
        // ...
        return myComponent;
    }
}

In the application.yml there is

some:
  property:
    key: ${PLACEHOLDER}

When I run this test with mvn clean test I get the error

Could not resolve placeholder 'PLACEHOLDER' in value "${PLACEHOLDER}"

Many other answers suggest adding an application-test.yml and calling it a day but I want to do it directly on the test class as the parameter can change from one test class to another and I don't really want lots of different .yml test configuration files.

Has anyone else come across this problem?

EDIT

Deadpool's answer fixed the issue as it sets the actual placeholder, so in a case where a bunch of different property keys could have values derived from the same placeholder, his answer is the way to go. Setting each some.property.key=PLACEHOLDER works too, until you forget one, which is what I did.

Upvotes: 1

Views: 201

Answers (1)

Ryuzaki L
Ryuzaki L

Reputation: 40058

Once you have placeholder for properties in application.yml

some:
 property:
  key: ${PLACEHOLDER}

You should use that placeholder to replace the value

@RunWith(SpringRunner.class)
@SpringBootTest(classes = MyIntTestConfig.class, properties = {PLACEHOLDER=value})
 public class MyIntTest {

    @Autowired
    public MyComponent myComponent;

      // ...

 }

Upvotes: 1

Related Questions