我一直在使用Mockito,遇到了以下错误消息:
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
我已经在两篇关于Stack Overflow的帖子中读到了这个问题,但它们没有详细介绍。我相信这与在模拟中嵌套模拟有关(从我读到的内容来看)。但是,我没有看到或完全理解人们发布的小片段。
我的测试类如下(省略不必要的代码):
// 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()));
}
我需要调用的方法:
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);
}
我也在使用TutorialsPoint的资源。
它发生在@test方法中,我相信这种嘲笑中的嘲笑是因为我认为它使用错误。
从答案实施后(工作):
@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);
}
}
您处于第三种情况:
3:你之前在里面刺另一个模拟的行为 "然后返回"指令(如果已完成)
发生这种情况是因为 thenReturn 从另一个模拟scrapeS.scrapePost
调用方法 很难确切地说如何解决这个问题,因为我需要更多的实现细节,但尝试在when
之前构建返回对象,它不应该是从另一个模拟返回的。这里有一个很好的解释:https://stackoverflow.com/a/26319364/1121883
这里有一个小例子。您的代码类似于play
测试。在 then return 中,您应该提供一个对象,而不是像测试fix
那样对模拟的调用。
@Test
public void play(){
A a = mock(A.class);
B b = mock(B.class);
when(a.a("input")).thenReturn(b.b("input"));
}
@Test
public void fix(){
A a = mock(A.class);
B b = mock(B.class);
String returnString = "b";
when(a.a("input")).thenReturn(returnString);
}
static class A{
String a(String in){
return "a";
}
}
static class B{
String b(String in){
return "b";
}
}
您的代码应如下所示:
List<prodItem> retList = new ArrayList<>();
retList.add(new ProdItem());
retList.add(new ProdItem());
when(bService.getProductList("cookie", "6753")).thenReturn(retList);
代码的(有点简化)计算顺序
when(bService.getProductList("cookie", "6753"))
.thenReturn(
scrapeS.scrapePost(new String(),
new String(),
new HashMap<>(),
new bService()));
首先是:
bService.getProductList("cookie", "6753")
第二
when(/*...*/)
第三
scrapeS.scrapePost(new String(),
new String(),
new HashMap<>(),
new bService())
.
因此,在尝试模拟bService
时,您使用模拟scrapeS
.
请注意,这根本没有意义。基本上,您目前正在尝试为bService.getProductList
提供一个应该使用scrapeS
的实现。但bService
是模拟,因此没有实现。
如果您希望 bService
和 scrapeS
的调用返回相同的对象,请将该对象存储到局部变量中,并在两种方法的 thenReturn
子句中使用该局部变量。
Object returnValue = /*whatever the return value is*/
when(bService.getProductList("cookie", "6753")).thenReturn(returnValue);
when(scrapeS.scrapePost(new String(), new String(), new HashMap<>(), new bService())).thenReturn(returnValue);