java - How to test a method that calls different methods inside? -


i have method unit test called addsong(song,userid) in service class.i calling 3 methods inside dao class.i using easy mock mock dao class . in setup first mock methods calling in addsong(song,userid) , , calling service.addsong(song,userid) method fot test.

but getting following error:

java.lang.illegalstateexception: missing behavior definition preceding method call: musicplayerdao.addsong(song) usage is: expect(a.foo()).andxxx() @ org.easymock.internal.mockinvocationhandler.invoke(mockinvocationhandler.java:42) @ org.easymock.internal.objectmethodsfilter.invoke(objectmethodsfilter.java:94) @ org.easymock.internal.classproxyfactory$mockmethodinterceptor.intercept(classproxyfactory.java:97) @ service.musicplayerdao$$enhancerbycglib$$45bc3ca1.addsong() @ service.musicplayerserviceimpl.addsong(musicplayerserviceimpl.java:43) @ addsongtest.addsongs(addsongtest.java:90)

here code:

    private void addsongsetup() throws sqlexception{     this.album = new album();     album.setalbumname("album");     this.genre = new genre();     genre.setgenrename("genre");     this.song = new song("song",this.album,3,"artist","composer",this.genre);     easymock.expect(this.dao.addsong(song)).andreturn(1).anytimes();     easymock.expect(this.dao.addgenre(genre, 1)).andreturn(1).anytimes();     easymock.expect(this.dao.addalbum(album, 1)).andreturn(1).anytimes();     easymock.expect(this.dao.useridsongsmapping(1,1)).andreturn(1).anytimes(); }  @test public void addsongs(){      this.album = new album();     album.setalbumname("album");     this.genre = new genre();     genre.setgenrename("genre");     this.song = new song("song",this.album,3,"artist","composer",this.genre);     try {         system.out.println(this.dao.addsong(song));         boolean status = this.service.addsong(song, 1);         assertequals(true,status);     } catch (exception e) {         // todo auto-generated catch block         e.printstacktrace();     } } 

my addsong method in service class: public boolean addsong(song song, int userid) throws exception {

    musicplayerdaointerface musicplayerdao = musicplayerdao.getinstance();     boolean status = false;     int songid = 0;      transactionmanager transactionmanager = transactionmanagerimpl             .getinstance();     try {         if (song != null) {             if (song.gettitle() != null) {                 transactionmanager.begin();                 songid = musicplayerdao.addsong(song);                 song.setsongid(songid);                 if (song.getgenre() != null                         && song.getgenre().getgenrename() != null) {                     musicplayerdao.addgenre(song.getgenre(),                             song.getsongid());                 }                 if (song.getalbum() != null                         && song.getalbum().getalbumname() != null) {                     musicplayerdao.addalbum(song.getalbum(),                             song.getsongid());                 }                 if (userid != 0 && songid != 0) {                     musicplayerdao.useridsongsmapping(userid,                             song.getsongid());                 }                 transactionmanager.commit();                 status = true;             }         }     } catch (sqlexception | rollbackexception | heuristicmixedexception             | heuristicrollbackexception e) {         transactionmanager.rollback();         status = false;         throw e;      }      return status; } 

i dont know going wrong.please help.

i think missing easymock.replay statement after record expected behaviour. like

easymock.replay(this.dao); 

from easymock guide:

to mock object, need to

  1. create mock object interface simulate
  2. record expected behavior
  3. switch mock object replay state

Comments

Popular posts from this blog

c++ - QTextObjectInterface with Qml TextEdit (QQuickTextEdit) -

javascript - angular ng-required radio button not toggling required off in firefox 33, OK in chrome -

xcode - Swift Playground - Files are not readable -