testing - Mock service in Spring MVC -
i have problem mocking service in spring mvc:
@controller public class companycontroller { @autowired private companyservice companyservice; @autowired private companyrelationservice companyrelationservice; @getmapping({"/", "/companies"}) public string displaycompanies(model model) { model.addattribute("company", new company()); list<company> companies = companyservice.findall(); model.addattribute("companies", companies); return "companies"; } } and test:
@runwith(springjunit4classrunner.class) @springboottest public class companytests { @autowired private webapplicationcontext webapplicationcontext; @mock companyservice companyservicemock; private mockmvc mockmvc; @before public void setup() { mockito.reset(companyservicemock); mockmvc = mockmvcbuilders.webappcontextsetup(webapplicationcontext).build(); mockitoannotations.initmocks(this); } @test public void shouldlistallcompanies() throws exception { company company1 = new company("company1", new address()); company company2 = new company("company2", new address()); when(companyservicemock.findall()).thenreturn(arrays.aslist(company1, company2)); mockmvc.perform(get("/companies")) .andexpect(status().isok()) .andexpect(view().name("companies")) .andexpect(model().attribute("companies", hassize(2))) .andexpect(model().attribute("companies", hasitem( allof( hasproperty("name", is("company1"))) ))) .andexpect(model().attribute("companies", hasitem( allof( hasproperty("name", is("company2")) ) ))); } } the question why companies real service instead of mock (company1, company2):
java.lang.assertionerror: model attribute 'companies' expected: collection containing (hasproperty("name", "company1")) but: hasproperty("name", "company1") property 'name' "companyfromrealservice", hasproperty("name", "company1") property 'name' "companyfromrealservice2" updated test class, removed setup , changed @bean @mockbean, remain @springboottest , works:
@runwith(springjunit4classrunner.class) @springboottest @autoconfiguremockmvc public class companytests { @mockbean private companyservice companyservicemock; @autowired private mockmvc mockmvc; @test @withmockuser(roles = "admin") public void shouldlistallcompanies() throws exception { company company1 = new company("company1", new address()); company company2 = new company("company2", new address()); when(companyservicemock.findall()).thenreturn(arrays.aslist(company1, company2)); mockmvc.perform(get("/companies")) .andexpect(status().isok()) .andexpect(view().name("companies")) .andexpect(model().attribute("companies", hassize(2))) .andexpect(model().attribute("companies", hasitem( allof( hasproperty("name", is("companyfromrealservice1"))) ))) .andexpect(model().attribute("companies", hasitem( allof( hasproperty("name", is("companyfromrealservice2")) ) ))); } }
/it looks post code; please add more details.it looks post code; please add more details.
first of all, if testing controller slice of application, should use @webmvctest annotation instead of @springboottest (you can find more information here). can use : @webmvctest(companycontroller.class).
secondly why getting trouble mockmvc in setup() method? can erase setup method people suggest in comments , @autowire mockmvc.
finally, using spring boot, better use @mockbean instead of @mock wrapped version of inside spring library.
Comments
Post a Comment