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
- don't use
mockitoannotations.initmocks(this);
use@runwith(mockitojunitrunner.class)
instead - you calling
dao = mockito.mock(abcdao.class)
overridesdao
createdmockitoannotations.initmocks(this)
- the
abcdao
instance inside abc differentdao
member of test case.
i can assume following fail:
asserttrue(dao == abc.getdao())
solution: remove following line
dao = mockito.mock(abcdao.class);
Comments
Post a Comment