gpt4 book ai didi

java - Mockito:Java - 未完成的 stub 检测

转载 作者:行者123 更新时间:2023-11-29 04:44:37 26 4
gpt4 key购买 nike

我一直在使用 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: you are stubbing the behaviour of another mock inside before 'thenReturn' instruction if completed

发生这种情况是因为 thenReturn 从另一个 mock scrapeS.scrapePost 调用方法 很难确切地说出如何解决这个问题,因为我需要更多的实现细节,但尝试在 when 之前构建返回对象 并且它不应该是另一个模拟的返回。这里有一个很好的解释:https://stackoverflow.com/a/26319364/1121883

这是一个小例子。您的代码类似于 play 测试。在 then return 中,您应该提供一个对象,而不是像 fix 测试中那样调用 mock。

@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);

关于java - Mockito:Java - 未完成的 stub 检测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37747535/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com