java - Yaml file under src does not load when Junit testing in spring boot -


i new spring boot.

i have service class below

@service public class aservice{     @value("${sample.time})     private long time;     ...     public int samplemethod(){         ...        return time;      } } 

and application.yml under src/resource has:

sample:     time:1 

and junit test class is:

@runwith(springjunit4classrunner.class) @webappconfiguration @springboottest(classes = mainapplication.class) public class aservicetest{     @autowired    private webapplicationcontext wac;     private aservice aservice;    @before   public void setupmockmvc() {     mockitoannotations.initmocks(this);     aservice = new aservice();     mockmvc = mockmvcbuilders.webappcontextsetup(wac).build();   }   public void samplemethodtest(){     assert.assertequals(1,aservice.samplemethod());   } 

this gives false, because method aservice.samplemethod() returns 0. not able read yml file under src when testing. know since ist injected spring standalone wont work. great if guide me possible solutions or pointers helpful too.

thanks in advance

error due new aservice(). remove new aservice() , use autowired aservice.

try below:

@runwith(springjunit4classrunner.class) @webappconfiguration @springboottest(classes = mainapplication.class) public class aservicetest{     @autowired    private webapplicationcontext wac;     @autowired    private aservice aservice;    @before   public void setupmockmvc() {     mockitoannotations.initmocks(this);      mockmvc = mockmvcbuilders.webappcontextsetup(wac).build();   }   public void samplemethodtest(){     assert.assertequals(1,aservice.samplemethod());    } 

Comments

Popular posts from this blog

neo4j - finding mutual friends in a cypher statement starting with three or more persons -

php - How to remove letter in front of the word laravel -

minify - Minimizing css files -