How to read variable from application.properties from method that is testing












0















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?










share|improve this question

























  • 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 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











  • 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
















0















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?










share|improve this question

























  • 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 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











  • 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














0












0








0








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?










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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











  • 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



















  • 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 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











  • 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

















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












2 Answers
2






active

oldest

votes


















1














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:





  1. ReflectionTestUtils is part of Spring-test.

  2. You don't need to use @InjectMocks because you have no mocks to inject.

  3. 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).

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






share|improve this answer
























  • Im REALY, REALY thank you. I spend a lot of time to beat it by myself.

    – Anton Kolosok
    Nov 28 '18 at 8:41



















0














You can try to overide the properties like that:



@TestPropertySource(locations = "location.properties",
properties = "a.b.c=123")


Example taken from here






share|improve this answer
























  • Still have null in foo() method

    – Anton Kolosok
    Nov 27 '18 at 20:33











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


}
});














draft saved

draft discarded


















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









1














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:





  1. ReflectionTestUtils is part of Spring-test.

  2. You don't need to use @InjectMocks because you have no mocks to inject.

  3. 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).

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






share|improve this answer
























  • Im REALY, REALY thank you. I spend a lot of time to beat it by myself.

    – Anton Kolosok
    Nov 28 '18 at 8:41
















1














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:





  1. ReflectionTestUtils is part of Spring-test.

  2. You don't need to use @InjectMocks because you have no mocks to inject.

  3. 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).

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






share|improve this answer
























  • Im REALY, REALY thank you. I spend a lot of time to beat it by myself.

    – Anton Kolosok
    Nov 28 '18 at 8:41














1












1








1







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:





  1. ReflectionTestUtils is part of Spring-test.

  2. You don't need to use @InjectMocks because you have no mocks to inject.

  3. 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).

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






share|improve this answer













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:





  1. ReflectionTestUtils is part of Spring-test.

  2. You don't need to use @InjectMocks because you have no mocks to inject.

  3. 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).

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







share|improve this answer












share|improve this answer



share|improve this answer










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



















  • 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













0














You can try to overide the properties like that:



@TestPropertySource(locations = "location.properties",
properties = "a.b.c=123")


Example taken from here






share|improve this answer
























  • Still have null in foo() method

    – Anton Kolosok
    Nov 27 '18 at 20:33
















0














You can try to overide the properties like that:



@TestPropertySource(locations = "location.properties",
properties = "a.b.c=123")


Example taken from here






share|improve this answer
























  • Still have null in foo() method

    – Anton Kolosok
    Nov 27 '18 at 20:33














0












0








0







You can try to overide the properties like that:



@TestPropertySource(locations = "location.properties",
properties = "a.b.c=123")


Example taken from here






share|improve this answer













You can try to overide the properties like that:



@TestPropertySource(locations = "location.properties",
properties = "a.b.c=123")


Example taken from here







share|improve this answer












share|improve this answer



share|improve this answer










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



















  • 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


















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Contact image not getting when fetch all contact list from iPhone by CNContact

count number of partitions of a set with n elements into k subsets

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks