使用Mockito mock静态方法

@Test
    public void queryAreas() {
        String result = "{\"code\":\"0\",\"message\":\"\",\"data\":{\"provinceList\":[]}}";
        MockedStatic<HttpClientUtil> httpClient = mockStatic(HttpClientUtil.class);
        httpClient.when(() -> HttpClientUtil.postJSON(any(), any(), anyMap())).thenReturn(result);

        try {
            List<ProvinceDTO> provinces = xydLocalService.queryAreas(null);
            System.err.println(provinces);
        } finally {
            httpClient.close(); // 注意每次要关掉这个静态对象
        }

        Assert.assertEquals(1, 1);
    }

你可能感兴趣的:(单元测试,windows,apache,java)