Reputation: 761
If I have a appplication.properties
like:
url=someUrl
user=userOne
password=ABCD
But if I want to be able to set the password when testing to something else, lets say:
password=someTest
How do I do that?
I need to do this in one test
@Test
void checkSomething{
//change/override password before calling someMethod only for this test
someMethod();
}
Upvotes: 0
Views: 1589
Reputation: 553
create another application.properties under src/test/resources thats all you need, if you want to get properties to use in one method you can do i without involving spring :
InputStream input = Main.class.getResourceAsStream(yourproperties-path);
Properties prop = System.getProperties();
prop.load(input);
Upvotes: 1
Reputation: 831
There are multiple ways.
1st way: Spring Profile
aplication.yaml:
spring.profiles.active=dev
---
spring.profile=dev
url=someUrl
user=userOne
password=ABCD
---
spring.profile=test
password=someTest
Test class:
@RunWith(SpringRunner.class)
@SpringBootTest
@ActiveProfiles("test")
public class MyTestClass {...
2nd way: SpringBootTest Properties
@RunWith(SpringRunner.class)
@SpringBootTest(properties = { "password=someTest" })
public class MyTestClass {...
Upvotes: 1
Reputation: 374
You can create a testing profile file something like application-testing.properties
and specify the overridden properties there.
Now while running the application you can specify use profile using
-Dspring.active.profiles=testing
Upvotes: 1