0

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();
}
5
  • Did you create application.properties file in src/test/resources ? Commented Jul 23, 2020 at 10:36
  • @Habil Yes I did. Commented Jul 23, 2020 at 10:42
  • 1
    Ok, try this: @SpringBootTest(properties = { "key=value" }) Commented Jul 23, 2020 at 10:48
  • @Habil Any way to apply only for one test method instead of the whole test class? Commented Jul 23, 2020 at 10:51
  • Add application-testing.properties in resource folder inside test and access it. Commented Jul 23, 2020 at 11:10

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

Comments

1

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 {...

3 Comments

Any way to apply only for one test method instead of the whole test class?
@nopens i updated my response to load properties for one method
I don't see no changes. May be you forgot to edit your answer :-) ?
1

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.