sergej
sergej

Reputation: 223

Override spring resource value with classpath in test

I wanna override the value of a resource in a test:

@SpringBootConfiguration
public class MyExampleConfig {

  @Value("classpath:my-example-resource.yml")
  private Resource myExampleResource;

}

Does anybody know how I could override the value "classpath:my-example-resource.yml"in my test?

Upvotes: 0

Views: 1117

Answers (3)

Andrii Vdovychenko
Andrii Vdovychenko

Reputation: 300

You can use create test configuration (ApplicationTest) with test .properties or .yml in test package and then use it in your tests

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = ApplicationTest.class)
public class Test {
    @Test
    public void test() throws Exception {
      //some code
    }
}

Upvotes: 1

razvanspt
razvanspt

Reputation: 51

You can have a separate application.properties file for your test package under resources, and you can define a key value pair i.e : classpath=classpath:my-example-resource.yml in this file and use in the code as follows

 @Value("${classpath}")
 private String classpath;

Upvotes: 1

RichardK
RichardK

Reputation: 3471

If you're running @SpringBootTest you can use this annotations:

@RunWith(SpringRunner.class)
@SpringBootTest(properties = {
        "key=value",
        "key=value"
})

They will override default ones.

Upvotes: 1

Related Questions