[英]Mockito:Java - Unfinished stubbing detection
我一直在与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”指令之前将另一个模拟内部的行为存根
这是因为thenReturn调用从另一个模拟方法scrapeS.scrapePost
这是很难说究竟如何解决这个问题,因为我需要更多的实施细节,但尝试建立之前返回的对象when
,它不应该从另一个模拟回报。 这里有一个很好的解释: https : //stackoverflow.com/a/26319364/1121883
这是一个小例子。 你的代码类似于play
test。 在返回时,您应该提供一个对象,而不是像在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
,因此没有实现。
如果您希望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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.