java - Test fails when mocking Hibernate's SessionFactory using Mockito -


ok, heres code

test runs mockitojunitrunner , in @before method execute

mockitoannotations.initmocks(this);  @test public void verifytimestamptest(){     targethistorypk thistorypk = mockito.mock(targethistorypk.class);      targethistorydao = mockito.mock(targethistorydaoimpl.class);             session = mockito.mock(session.class);     sessionfactory = mockito.mock(sessionfactory.class);      mockito.when(sessionfactory.getcurrentsession()).thenreturn(session);      targethistory th = mockito.mock(targethistory.class);     mockito.when(session.save(th)).thenreturn(thistorypk);      boolean h = targethistorydao.addtargethistory(th);      system.out.println("erh: "+ th.gettarget_update() + h);     assertnotnull("timestamp null", th.gettarget_update()); } 

and tested method

public class targethistorydaoimpl implements targethistorydao {       @autowired     private sessionfactory sessionfactory;      public targethistorydaoimpl() {          }      public targethistorydaoimpl(sessionfactory sessionfactory) {         this.sessionfactory = sessionfactory;     }     @override     public boolean addtargethistory(targethistory th) {          if(th.gettarget_update() == null){             date = new date();             timestamp timestamp = new timestamp(now.gettime());             th.settarget_update(timestamp);         }         session session = sessionfactory.getcurrentsession();         targethistorypk pk = (targethistorypk)session.save(th);         if(pk != null)             return true;          return false;     }  } 

normally addtargethistory() being called service class method presented below

    @transactional     public boolean registertargetactivity(targethistory th) {         // todo auto-generated method stub         return targethistorydao.addtargethistory(th);     } 

can explain me why test verifytimestamptest() fails?

edit added line test(i cant debug - getting sts error when trying debug)

if(th == null)         system.out.println("rggrgr"); 

like this

@test public void verifytimestamptest(){     targethistorypk thistorypk = mockito.mock(targethistorypk.class);     targethistorydao = mockito.mock(targethistorydaoimpl.class);             session = mockito.mock(session.class);     sessionfactory = mockito.mock(sessionfactory.class);      mockito.when(sessionfactory.getcurrentsession()).thenreturn(session);      targethistory th = new targethistory();     mockito.when(session.save(th)).thenreturn(thistorypk);      boolean h = targethistorydao.addtargethistory(th);      if(th == null)         system.out.println("targethistory null!");      system.out.println("erh: "+ th.gettarget_update() + h);     assertnotnull("timestamp null", th.gettarget_update());   } 

and sts warning "dead code". why???? why dead code?

it's because targethistory th mock. don't setup behaviours mock's methods. that's why nothing , return null. on this mockito documentation page says:

by default, methods return value, mock returns null, empty collection or appropriate primitive/primitive wrapper value (e.g: 0, false, ... int/integer, boolean/boolean, ...).

beware void methods on mocks nothing default!

you need replace targethistory mock real object or stub.


Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -

How to provide Authorization & Authentication using Asp.net, C#? -