首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >用Mockito模拟OkHttpClient时出错

用Mockito模拟OkHttpClient时出错
EN

Stack Overflow用户
提问于 2022-01-14 19:44:37
回答 1查看 132关注 0票数 0

我想用Mockito在Junit5中为以下方法编写测试。

代码语言:javascript
运行
复制
 public List<Data> getData(Long id) {
        UriComponents uriComponents = UriComponentsBuilder.fromUriString("test.com")
                .pathSegment("/test")
                .queryParam("filters[id]", id)
                .build();
    
        Request request = new Request.Builder()
                .url(uriComponents.toUriString())
                .method("GET", null)
                .build();
    
        try {
            Response response;
            response = okHttpClient.newCall(request).execute();
    
            TypeReference<List<Data>> listType = new TypeReference<>() {};
            List<Data> list = objectMapper.readValue(Objects.requireNonNull(response.body()).string(), listType);
            response.close();
    
            return list;
        } catch (IOException ioEx) {
            throw new RuntimeException(ioEx);
        }
    }

测试

代码语言:javascript
运行
复制
 @Test
    public void shouldReturnData() throws IOException {
        try (MockedStatic<UriComponentsBuilder> mocked = mockStatic(UriComponentsBuilder.class)) {
            mocked.when(() -> UriComponentsBuilder.fromUriString("test.go").pathSegment("test")
                    .queryParam("filters[1]").build()).thenReturn(uriComponents);

            TypeReference<List<Data>> listType = new TypeReference<>() {};
            List<Data> expectedData = new ObjectMapper().readValue("{}", listType);
            List<Data> actualData = MyClient.getData(1234L);

            Assertions.assertEquals(expectedData, actualData);
        }
    }

考试失败了,以下是例外情况:-

代码语言:javascript
运行
复制
org.mockito.exceptions.misusing.WrongTypeOfReturnValue: 
UriComponents$MockitoMock$2089413864 cannot be returned by fromUriString()
fromUriString() should return UriComponentsBuilder

我无法模仿UriComponentsBuilder,因为这个exception.Can,这里有人能帮我吗?

EN

回答 1

Stack Overflow用户

发布于 2022-01-14 21:13:31

您正在尝试模拟这行中的四个方法调用:

代码语言:javascript
运行
复制
    mocked.when(() -> UriComponentsBuilder.fromUriString("test.go").pathSegment("test")
            .queryParam("filters[1]").build()).thenReturn(uriComponents);

你不能这么做。一次只能模拟一个方法调用。

如果您想模拟出UriComponentsBuilder,那么您必须创建一个模拟UriComponentsBuilder,模拟pathSegmentqueryParam方法来返回模拟UriComponentsBuilder实例,最后模拟build()方法以返回uriComponents

代码语言:javascript
运行
复制
    UriComponentsBuilder uriComponentsBuilder = mock(UriComponentsBuilder.class);
    mocked.when(() -> UriComponentsBuilder.fromUriString("test.go"))
        .thenReturn(uriComponentsBuilder); 
    when(uriComponentsBuilder.pathSegment("test")).thenReturn(uriComponentsBuilder);
    when(uriComponentsBuilder.queryParam("filters[1]")).thenReturn(uriComponentsBuilder);
    when(uriComponentsBuilder.build()).thenReturn(uriComponents);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70715811

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档