How to read variable from application.properties from method that is testing
I have class:
@Service
public class A {
@Value("${a.b.c}")
private String abc;
public void foo() {
sout(abc);
}
}
I Have test class:
@SpringBootTest
@SpringBootConfiguration
@RunWith(SpringRunner.class)
@TestPropertySource(locations = "classpath:application.yml")
public class TestA {
@Value("${a.b.c}")
private String abc;
@InjectMocks
private A a;
@Test
public void testFoo() {
this.a.foo();
}
}
When I debugging the test method testFoo()
,
I see that variable abc
is read from the application.yml
file.
But,
inside the foo()
method,
I see that the variable abc
is null.
How can I set variable abc
such that it is available in method foo()
when I trying to test this method?
java spring unit-testing
add a comment |
I have class:
@Service
public class A {
@Value("${a.b.c}")
private String abc;
public void foo() {
sout(abc);
}
}
I Have test class:
@SpringBootTest
@SpringBootConfiguration
@RunWith(SpringRunner.class)
@TestPropertySource(locations = "classpath:application.yml")
public class TestA {
@Value("${a.b.c}")
private String abc;
@InjectMocks
private A a;
@Test
public void testFoo() {
this.a.foo();
}
}
When I debugging the test method testFoo()
,
I see that variable abc
is read from the application.yml
file.
But,
inside the foo()
method,
I see that the variable abc
is null.
How can I set variable abc
such that it is available in method foo()
when I trying to test this method?
java spring unit-testing
Like that:@SpringBootTest({"a.b.c=myValue"})
– piotr szybicki
Nov 27 '18 at 18:59
I tried. Still null
– Anton Kolosok
Nov 27 '18 at 19:09
@AntonKolosok try to replace@InjectMocks
annotation in the classTestA
to the@Autowired
– Mr. Skip
Nov 27 '18 at 19:13
Possible duplicate of How do I mock an autowired @Value field in Spring with Mockito?
– jordiburgos
Nov 27 '18 at 19:14
A better solution is to use constructor injection, which will usually let you avoid involving Spring at all and simply donew A(myTestAbc)
.
– chrylis
Nov 27 '18 at 19:32
add a comment |
I have class:
@Service
public class A {
@Value("${a.b.c}")
private String abc;
public void foo() {
sout(abc);
}
}
I Have test class:
@SpringBootTest
@SpringBootConfiguration
@RunWith(SpringRunner.class)
@TestPropertySource(locations = "classpath:application.yml")
public class TestA {
@Value("${a.b.c}")
private String abc;
@InjectMocks
private A a;
@Test
public void testFoo() {
this.a.foo();
}
}
When I debugging the test method testFoo()
,
I see that variable abc
is read from the application.yml
file.
But,
inside the foo()
method,
I see that the variable abc
is null.
How can I set variable abc
such that it is available in method foo()
when I trying to test this method?
java spring unit-testing
I have class:
@Service
public class A {
@Value("${a.b.c}")
private String abc;
public void foo() {
sout(abc);
}
}
I Have test class:
@SpringBootTest
@SpringBootConfiguration
@RunWith(SpringRunner.class)
@TestPropertySource(locations = "classpath:application.yml")
public class TestA {
@Value("${a.b.c}")
private String abc;
@InjectMocks
private A a;
@Test
public void testFoo() {
this.a.foo();
}
}
When I debugging the test method testFoo()
,
I see that variable abc
is read from the application.yml
file.
But,
inside the foo()
method,
I see that the variable abc
is null.
How can I set variable abc
such that it is available in method foo()
when I trying to test this method?
java spring unit-testing
java spring unit-testing
edited Nov 27 '18 at 22:38
DwB
29.2k84473
29.2k84473
asked Nov 27 '18 at 18:52
Anton KolosokAnton Kolosok
727
727
Like that:@SpringBootTest({"a.b.c=myValue"})
– piotr szybicki
Nov 27 '18 at 18:59
I tried. Still null
– Anton Kolosok
Nov 27 '18 at 19:09
@AntonKolosok try to replace@InjectMocks
annotation in the classTestA
to the@Autowired
– Mr. Skip
Nov 27 '18 at 19:13
Possible duplicate of How do I mock an autowired @Value field in Spring with Mockito?
– jordiburgos
Nov 27 '18 at 19:14
A better solution is to use constructor injection, which will usually let you avoid involving Spring at all and simply donew A(myTestAbc)
.
– chrylis
Nov 27 '18 at 19:32
add a comment |
Like that:@SpringBootTest({"a.b.c=myValue"})
– piotr szybicki
Nov 27 '18 at 18:59
I tried. Still null
– Anton Kolosok
Nov 27 '18 at 19:09
@AntonKolosok try to replace@InjectMocks
annotation in the classTestA
to the@Autowired
– Mr. Skip
Nov 27 '18 at 19:13
Possible duplicate of How do I mock an autowired @Value field in Spring with Mockito?
– jordiburgos
Nov 27 '18 at 19:14
A better solution is to use constructor injection, which will usually let you avoid involving Spring at all and simply donew A(myTestAbc)
.
– chrylis
Nov 27 '18 at 19:32
Like that:
@SpringBootTest({"a.b.c=myValue"})
– piotr szybicki
Nov 27 '18 at 18:59
Like that:
@SpringBootTest({"a.b.c=myValue"})
– piotr szybicki
Nov 27 '18 at 18:59
I tried. Still null
– Anton Kolosok
Nov 27 '18 at 19:09
I tried. Still null
– Anton Kolosok
Nov 27 '18 at 19:09
@AntonKolosok try to replace
@InjectMocks
annotation in the class TestA
to the @Autowired
– Mr. Skip
Nov 27 '18 at 19:13
@AntonKolosok try to replace
@InjectMocks
annotation in the class TestA
to the @Autowired
– Mr. Skip
Nov 27 '18 at 19:13
Possible duplicate of How do I mock an autowired @Value field in Spring with Mockito?
– jordiburgos
Nov 27 '18 at 19:14
Possible duplicate of How do I mock an autowired @Value field in Spring with Mockito?
– jordiburgos
Nov 27 '18 at 19:14
A better solution is to use constructor injection, which will usually let you avoid involving Spring at all and simply do
new A(myTestAbc)
.– chrylis
Nov 27 '18 at 19:32
A better solution is to use constructor injection, which will usually let you avoid involving Spring at all and simply do
new A(myTestAbc)
.– chrylis
Nov 27 '18 at 19:32
add a comment |
2 Answers
2
active
oldest
votes
Step one is to answer this question: Am I unit testing the code in my class or am I integration testing the combination of Spring and some collection of code that includes my class?
If you are unit testing your code,
then it is not necessary to have Spring do its thing.
Instead,
you only need to instantiate your class,
set the values that Spring would have set for you,
execute the method you are testing,
then verify that your method executed correctly.
Here is your example unit test rewritten as I suggested:
public class TestA
{
private static final String VALUE_ABC = "VALUE_ABC";
private A classToTest;
@Test
public void testFoo()
{
classToTest.foo();
}
@Before
public void preTestSetup()
{
classToTest = new A();
ReflectionTestUtils.setField(
classToTest,
"abc",
VALUE_ABC)
}
}
Some Notes:
ReflectionTestUtils
is part of Spring-test.- You don't need to use
@InjectMocks
because you have no mocks to inject. - I don't know what
sout
is, so I excluded it from the test. You should verify that the sout method was called with the correct value (in this case VALUE_ABC). - If you are just unit testing your code, you don't need Spring, which means that you don't need to use the
@RunWith
annotation.
Im REALY, REALY thank you. I spend a lot of time to beat it by myself.
– Anton Kolosok
Nov 28 '18 at 8:41
add a comment |
You can try to overide the properties like that:
@TestPropertySource(locations = "location.properties",
properties = "a.b.c=123")
Example taken from here
Still have null in foo() method
– Anton Kolosok
Nov 27 '18 at 20:33
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53506333%2fhow-to-read-variable-from-application-properties-from-method-that-is-testing%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Step one is to answer this question: Am I unit testing the code in my class or am I integration testing the combination of Spring and some collection of code that includes my class?
If you are unit testing your code,
then it is not necessary to have Spring do its thing.
Instead,
you only need to instantiate your class,
set the values that Spring would have set for you,
execute the method you are testing,
then verify that your method executed correctly.
Here is your example unit test rewritten as I suggested:
public class TestA
{
private static final String VALUE_ABC = "VALUE_ABC";
private A classToTest;
@Test
public void testFoo()
{
classToTest.foo();
}
@Before
public void preTestSetup()
{
classToTest = new A();
ReflectionTestUtils.setField(
classToTest,
"abc",
VALUE_ABC)
}
}
Some Notes:
ReflectionTestUtils
is part of Spring-test.- You don't need to use
@InjectMocks
because you have no mocks to inject. - I don't know what
sout
is, so I excluded it from the test. You should verify that the sout method was called with the correct value (in this case VALUE_ABC). - If you are just unit testing your code, you don't need Spring, which means that you don't need to use the
@RunWith
annotation.
Im REALY, REALY thank you. I spend a lot of time to beat it by myself.
– Anton Kolosok
Nov 28 '18 at 8:41
add a comment |
Step one is to answer this question: Am I unit testing the code in my class or am I integration testing the combination of Spring and some collection of code that includes my class?
If you are unit testing your code,
then it is not necessary to have Spring do its thing.
Instead,
you only need to instantiate your class,
set the values that Spring would have set for you,
execute the method you are testing,
then verify that your method executed correctly.
Here is your example unit test rewritten as I suggested:
public class TestA
{
private static final String VALUE_ABC = "VALUE_ABC";
private A classToTest;
@Test
public void testFoo()
{
classToTest.foo();
}
@Before
public void preTestSetup()
{
classToTest = new A();
ReflectionTestUtils.setField(
classToTest,
"abc",
VALUE_ABC)
}
}
Some Notes:
ReflectionTestUtils
is part of Spring-test.- You don't need to use
@InjectMocks
because you have no mocks to inject. - I don't know what
sout
is, so I excluded it from the test. You should verify that the sout method was called with the correct value (in this case VALUE_ABC). - If you are just unit testing your code, you don't need Spring, which means that you don't need to use the
@RunWith
annotation.
Im REALY, REALY thank you. I spend a lot of time to beat it by myself.
– Anton Kolosok
Nov 28 '18 at 8:41
add a comment |
Step one is to answer this question: Am I unit testing the code in my class or am I integration testing the combination of Spring and some collection of code that includes my class?
If you are unit testing your code,
then it is not necessary to have Spring do its thing.
Instead,
you only need to instantiate your class,
set the values that Spring would have set for you,
execute the method you are testing,
then verify that your method executed correctly.
Here is your example unit test rewritten as I suggested:
public class TestA
{
private static final String VALUE_ABC = "VALUE_ABC";
private A classToTest;
@Test
public void testFoo()
{
classToTest.foo();
}
@Before
public void preTestSetup()
{
classToTest = new A();
ReflectionTestUtils.setField(
classToTest,
"abc",
VALUE_ABC)
}
}
Some Notes:
ReflectionTestUtils
is part of Spring-test.- You don't need to use
@InjectMocks
because you have no mocks to inject. - I don't know what
sout
is, so I excluded it from the test. You should verify that the sout method was called with the correct value (in this case VALUE_ABC). - If you are just unit testing your code, you don't need Spring, which means that you don't need to use the
@RunWith
annotation.
Step one is to answer this question: Am I unit testing the code in my class or am I integration testing the combination of Spring and some collection of code that includes my class?
If you are unit testing your code,
then it is not necessary to have Spring do its thing.
Instead,
you only need to instantiate your class,
set the values that Spring would have set for you,
execute the method you are testing,
then verify that your method executed correctly.
Here is your example unit test rewritten as I suggested:
public class TestA
{
private static final String VALUE_ABC = "VALUE_ABC";
private A classToTest;
@Test
public void testFoo()
{
classToTest.foo();
}
@Before
public void preTestSetup()
{
classToTest = new A();
ReflectionTestUtils.setField(
classToTest,
"abc",
VALUE_ABC)
}
}
Some Notes:
ReflectionTestUtils
is part of Spring-test.- You don't need to use
@InjectMocks
because you have no mocks to inject. - I don't know what
sout
is, so I excluded it from the test. You should verify that the sout method was called with the correct value (in this case VALUE_ABC). - If you are just unit testing your code, you don't need Spring, which means that you don't need to use the
@RunWith
annotation.
answered Nov 27 '18 at 22:45
DwBDwB
29.2k84473
29.2k84473
Im REALY, REALY thank you. I spend a lot of time to beat it by myself.
– Anton Kolosok
Nov 28 '18 at 8:41
add a comment |
Im REALY, REALY thank you. I spend a lot of time to beat it by myself.
– Anton Kolosok
Nov 28 '18 at 8:41
Im REALY, REALY thank you. I spend a lot of time to beat it by myself.
– Anton Kolosok
Nov 28 '18 at 8:41
Im REALY, REALY thank you. I spend a lot of time to beat it by myself.
– Anton Kolosok
Nov 28 '18 at 8:41
add a comment |
You can try to overide the properties like that:
@TestPropertySource(locations = "location.properties",
properties = "a.b.c=123")
Example taken from here
Still have null in foo() method
– Anton Kolosok
Nov 27 '18 at 20:33
add a comment |
You can try to overide the properties like that:
@TestPropertySource(locations = "location.properties",
properties = "a.b.c=123")
Example taken from here
Still have null in foo() method
– Anton Kolosok
Nov 27 '18 at 20:33
add a comment |
You can try to overide the properties like that:
@TestPropertySource(locations = "location.properties",
properties = "a.b.c=123")
Example taken from here
You can try to overide the properties like that:
@TestPropertySource(locations = "location.properties",
properties = "a.b.c=123")
Example taken from here
answered Nov 27 '18 at 19:17
riorioriorio
1,89831134
1,89831134
Still have null in foo() method
– Anton Kolosok
Nov 27 '18 at 20:33
add a comment |
Still have null in foo() method
– Anton Kolosok
Nov 27 '18 at 20:33
Still have null in foo() method
– Anton Kolosok
Nov 27 '18 at 20:33
Still have null in foo() method
– Anton Kolosok
Nov 27 '18 at 20:33
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53506333%2fhow-to-read-variable-from-application-properties-from-method-that-is-testing%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Like that:
@SpringBootTest({"a.b.c=myValue"})
– piotr szybicki
Nov 27 '18 at 18:59
I tried. Still null
– Anton Kolosok
Nov 27 '18 at 19:09
@AntonKolosok try to replace
@InjectMocks
annotation in the classTestA
to the@Autowired
– Mr. Skip
Nov 27 '18 at 19:13
Possible duplicate of How do I mock an autowired @Value field in Spring with Mockito?
– jordiburgos
Nov 27 '18 at 19:14
A better solution is to use constructor injection, which will usually let you avoid involving Spring at all and simply do
new A(myTestAbc)
.– chrylis
Nov 27 '18 at 19:32