【问题标题】:Okhttp3 - How to define a specific Request in an Okhttp3 ClientOkhttp3 - 如何在 Okhttp3 客户端中定义特定请求
【发布时间】:2021-10-26 17:19:57
【问题描述】:

我有一个递归方法,它使用okhttp3 向端点发出连续的分页请求,直到某个属性不再存在。我运行的测试模拟了过程中的最后一次调用,但我在完成 2 步迭代时遇到了问题。

如何在 newCall() 方法中定义一个特定的 ohkttp3.Request 以便它不会每次都抛出 NullPointer?我的代码/测试如下供参考。

错误: java.lang.NullPointerException

递归方法

public JSONArray recursiveMethod(JSONArray array, String authCode, String requestUrl) {
        String nextPageAtt = "newPage";
        String newPage = null;
        Request request = new Request.Builder()
                .url(requestUrl)
                .get()
                .addHeader("Content-Type", "application/json")
                .addHeader("Authorization", authCode)
                .build();
                
        try {
            //Problem child
            okhttp3.Response response = client.newCall(request).execute();
            if (response.code() == SUCCESS_CODE && response.body() != null) {

                //Add JSON data to array 
                
                //if "newPage" attribute in response, set it here
                //only present when page != last page
                
            } else {
            }
        }
        //"newPage" attribute won't be present on the last page of data"
        if (newPage != null) {
            recursiveMethod(array, authCode, baseUrl + newPage);
        }
        return array;
    }

测试方法

@Test
void recursiveMethodNonewPage() throws IOException {
        JSONArray emptyArray = new JSONArray();
    
        final Response successResponse = new Response.Builder()
                .request(new Request.Builder().url("https://localhost:8080/getData").build()) //"newPage" not there (last call)
                .protocol(Protocol.HTTP_1_1)
                .code(SUCCESS_CODE).message("").body(
                        ResponseBody.create(
                                MediaType.parse("application/json"),
                                "/////JSON/////"
                        ))
                .build();
                
        when(remoteCall.execute()).thenReturn(successResponse);
        
        //this works when I pass "any()" but a java.lang.NullPointerException is thrown when I enter a specific Request
        when(mockClient.newCall(any())).thenReturn(remoteCall);
        
        //******Issue******
        //Need to pass a specific request on in order to simulate multiple recursive iterations
        /*Request request = new Request.Builder()
                .url(https://localhost:8080/getData)
                .get()
                .addHeader("Content-Type", "application/json")
                .addHeader("Authorization", authCode)
                .build();*/
                
        //Throws Null
        //when(mockClient.newCall(request))).thenReturn(remoteCall);
        
        JSONArray newArray = getDataUtil.recursiveMethod(emptyArray, authCodeSuccess, "https://localhost:8080/getData");
        assertEquals(4, newArray.length());
    }

【问题讨论】:

    标签: java mockito okhttp


    【解决方案1】:

    终于在这里找到了我的答案:https://stackoverflow.com/a/8395685/2573297

    when( method-call ).thenReturn( value1, value2, value3 );
    

    工作测试

    @Test
    void recursiveMethodNonewPage() throws IOException {
            JSONArray emptyArray = new JSONArray();
        
            final Response pageOne = new Response.Builder()
                    .request(new Request.Builder().url("https://localhost:8080/getData").build())
                    .protocol(Protocol.HTTP_1_1)
                    .code(SUCCESS_CODE).message("").body(
                            ResponseBody.create(
                                    MediaType.parse("application/json"),
                                    "/////JSON/////"
                            ))
                    .build();
                    
            final Response pageTwo = new Response.Builder()
                    .request(new Request.Builder().url("https://localhost:8080/getData").build())
                    .protocol(Protocol.HTTP_1_1)
                    .code(SUCCESS_CODE).message("").body(
                            ResponseBody.create(
                                    MediaType.parse("application/json"),
                                    "/////JSON/////"
                            ))
                    .build();
            
            when(mockClient.newCall(any())).thenReturn(remoteCall);
            when(remoteCall.execute()).thenReturn(pageOne, pageTwo);
            
            JSONArray newArray = getDataUtil.recursiveMethod(emptyArray, authCodeSuccess, "https://localhost:8080/getData");
            assertEquals(4, newArray.length());
        }
    

    【讨论】:

      猜你喜欢
      • 2023-03-09
      • 1970-01-01
      • 2020-04-12
      • 2018-05-29
      • 1970-01-01
      • 1970-01-01
      • 2016-07-29
      • 2019-05-24
      • 1970-01-01
      相关资源
      最近更新 更多