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
Post a Comment