java - Mockito issue with List -


i have dao method returns list.

now trying mock dao class in service layer when invoke dao method, giving me empty though have mocked dao method below sample code snippet,

public class abctest {

@injectmocks abc abc = new abc();     @mock private abcdao dao;  @before public void setup() {     mockitoannotations.initmocks(this);     dao = mockito.mock(abcdao.class);     mockito.when(dao.getlistfromdb()).thenreturn(arrays.aslist("1","2","3")); }  @test public void testservicemethod() {     abc.servicemethod();  // inside method when dao method called, giving me empty list though have mocked above. } 

any pointers helpful

  1. don't use mockitoannotations.initmocks(this); use @runwith(mockitojunitrunner.class) instead
  2. you calling dao = mockito.mock(abcdao.class) overrides dao created mockitoannotations.initmocks(this)
  3. the abcdao instance inside abc different dao member of test case.

i can assume following fail:

asserttrue(dao == abc.getdao()) 

solution: remove following line

dao = mockito.mock(abcdao.class); 

Comments

Popular posts from this blog

qt - Using float or double for own QML classes -

Create Outlook appointment via C# .Net -

ios - Swift Array Resetting Itself -