spring - Mockito mocking get methods from the database services -
i trying mock getby() method after adding element mocked service add.
this have:
feeditem feeditem = feedservicetested.createfeeditem("text test", "category test", "author test"); mockito.verify(feedrepository).add(feeditem); mockito.verify(feedrepository).findallbycategory("category test");
however following error:
wanted not invoked: feedrepository.findallbycategory( "category test" ); -> @ ie.cit.adf.services.feedserviceimpltest.testsearchfeeditemsbycategory(feedserviceimpltest.java:55) however, there other interactions mock: -> @ ie.cit.adf.services.feedserviceimpl.createfeeditem(feedserviceimpl.java:44) @ ie.cit.adf.services.feedserviceimpltest.testsearchfeeditemsbycategory(feedserviceimpltest.java:55)
any idea how mock findallbycategory()?
here 2 classes:
repository:
@secured("role_user") public class jdbcfeedrepository implements feedrepository { private jdbctemplate jdbctemplate; private feeditemsmapper feeditemsmapper = new feeditemsmapper(); public jdbcfeedrepository(datasource datasource) { jdbctemplate = new jdbctemplate(datasource); } @override public feeditem findbyid(string feeditemid) { return jdbctemplate.queryforobject( "select id, text, category, author feeditems id=?", feeditemsmapper, feeditemid ); } @override public list<feeditem> findall() { return jdbctemplate.query( "select id, text, category, author feeditems", feeditemsmapper ); } @override public list<feeditem> findallbycategory(string category) { return jdbctemplate.query( "select id, text, category, author feeditems category=?", feeditemsmapper, category ); } @override public list<feeditem> findallbyauthor(string author) { return jdbctemplate.query( "select id, text, category, author feeditems author=?", feeditemsmapper, author ); } @override public void add(feeditem feeditem) { jdbctemplate.update( "insert feeditems values(?,?,?,?)", feeditem.getid(), feeditem.gettext(), feeditem.getcategory(), feeditem.getauthor() ); } @override public void delete(string feeditemid) { jdbctemplate.update("delete feeditems id=?", feeditemid); } /** * returns name of logged in author. * * @return string */ private string getcurrentuser() { return securitycontextholder.getcontext().getauthentication().getname(); } } class feeditemsmapper implements rowmapper<feeditem> { @override public feeditem maprow(resultset rs, int rownum) throws sqlexception { feeditem feeditem = new feeditem(); feeditem.setid(rs.getstring("id")); feeditem.settext(rs.getstring("text")); feeditem.setcategory(rs.getstring("category")); feeditem.setauthor(rs.getstring("author")); return feeditem; } }
service:
@transactional public class feedserviceimpl implements feedservice { private feedrepository repo; public feedserviceimpl(feedrepository repo) { this.repo = repo; } @override public feeditem get(string feeditemid) { return repo.findbyid(feeditemid); } @override public list<feeditem> getallfeeditems() { return repo.findall(); } @override public list<feeditem> getallfeeditemsbycategory(string category) { return repo.findallbycategory(category); } @override public list<feeditem> getauthorfeeditems(string author) { return repo.findallbyauthor(author); } @override public feeditem createfeeditem(string text, string category, string author) { feeditem feeditem = new feeditem(); feeditem.settext(text); feeditem.setcategory(category); feeditem.setauthor(author); repo.add(feeditem); return feeditem; } @override public void delete(string feeditemid) { repo.delete(feeditemid); } }
it seems code never calls:
feedrepository.findallbycategory("category test");
but added verifier it. mockito verify ensures method called 1 time in test. when did not happen complains exception.
your test calls:
feedservicetested.createfeeditem(...)
which calls following methods on repo:
add(feeditem)
which first verify. @ moment seems code did not use findallbycategory , verify throws exception.
or there call in feeditem repo? please provide code class too.
Comments
Post a Comment