nopens
nopens

Reputation: 761

Spring boot override application properties in test

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

Answers (3)

Mehdi Belbal
Mehdi Belbal

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

Murat Kara
Murat Kara

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

Sunil
Sunil

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

Related Questions