PowerMock mock 静态方法

静态方法内部有调用,先Mock内部方法,再正常调用

 

    public void testUpdateInfo() {
        // Setup
        final Config config = new Config();
        try{
            final JSONObject expectedResult = new JSONObject(0, false);
            expectedResult.put("code", "200");
            PowerMockito.mockStatic(HttpUtils.class);
            PowerMockito.when(HttpUtils.postForObject(Mockito.anyString(), Mockito.anyMap(), Mockito.any())).thenReturn(expectedResult.toString());
            // Run the test
            final JSONObject result = TodoUtil.updateInfo(config, "recordRequest");
            // Verify the results
            assertEquals(expectedResult, result);
        }catch (Exception e) {
            e.printStackTrace();
        }
    }

 

posted @ 2023-02-14 14:32  r1-12king  阅读(570)  评论(0编辑  收藏  举报