I have been working with Mockito and have run into this error message:
org.mockito.exceptions.misusing.UnfinishedStubbingException:
Unfinished stubbing detected here:
-> at com.rbc.rewards.catalogue.service.B2SServicesTest.getProductList_Success(B2SServ icesTest.java:52)
E.g. thenReturn() may be missing.
Examples of correct stubbing:
when(mock.isOk()).thenReturn(true);
when(mock.isOk()).thenThrow(exception);
doThrow(exception).when(mock).someVoidMethod();
Hints:
1. missing thenReturn()
2. you are trying to stub a final method, you naughty developer!
3: you are stubbing the behaviour of another mock inside before 'thenReturn' instruction if completed
I have read in two posts on Stack Overflow about this issue but they do not go into full detail. I believe it has something to do with nesting mocks inside a mocking (from what I read). However I do not see or fully understand by the small snippets people have posted.
My test class is as followed (leaving out unnecessary code):
// Mock: uses Java Reflection in order to create mock objects of each class
@Mock
private Scrapes scrapeS;
@Mock
private SsoS ssoS;
@Mock
private BScrape bScrape;
//@InjectMocks annotation is used to create and inject the mock object
@Mock
private BService bService;
@Before
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
}
// Testing:
@Test
public void getProductList_Success() throws Exception{
when(BService.getProductList("cookie", "6753"))
.thenReturn(
scrapeS.scrapePost(new String(),
new String(),
new HashMap<>(),
new bService()));
}
Method I need to call:
public List<prodItem> getProdList(String raw_cookie, String catId) throws Exception {
String url = ssoSetting.getUrls().BProductList();
String post = "url" + catId +"url";
BScrape scraper = new BScrape ();
Map<String, String> postRequest = new HashMap();
postRequest.put("searchRequestParams", post);
return scrapeS.scrapePost(url, raw_cookie, postRequest, scraper);
}
I am also using resources from TutorialsPoint.
It is happening within the @test method and I believe this mocking inside mocking is because I am using it wrong I am assuming.
After Implementation from Answer (working):
@Mock
private SSetting sSetting = new SSetting ();
// Mock: uses Java Reflection in order to create mock objects and is injected into InjectMocks
@Mock
private ProdItem productItem = new ProdItem ();
@Mock
private sService scrapeService = new sService ();
@Mock
private BScrape bScrape ;
//@InjectMocks annotation is used to create and inject the mock object
@InjectMocks
private BService bService = new BService ();
@Before
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
}
// Testing:
@Test
public void getProductList_Success() throws Exception{
List<ProductItem> retList = new ArrayList<>();
retList.add(new ProductItem());
retList.add(new ProductItem());
//try{
when(b2SServices.getProductList("cookie", "6753")).thenCallRealMethod();
//test the add functionality
when(BService .getProdList("cookie", "6753")).thenReturn(retList);
}catch(Exception exception){
System.out.println(exception);
}
}