小编Kai*_*ang的帖子

错误:静态模拟已在当前线程中注册

Junit 测试用例失败:

必须取消注册现有的静态模拟注册

测试类:

@RunWith(MockitoJUnitRunner.class)
public class DeleteReportServiceTest {

    @InjectMocks
    private ReturnCheckController returnCheckController;

    @Mock
    MockedStatic<DigitalGatewayRESTClient> mockDigiGateway;

    @Before
    public void setUp() throws Exception {
        this.returnCheckController = new ReturnCheckController();
        this.mockDigiGateway = Mockito.mockStatic(DigitalGatewayRESTClient.class);
    }

    @Test
    public void testdeleteReport() throws Exception {
        String input = "{\"clientId\": \"1\", \"applicationId\": \"9010\"}";
        String response = "{\"success\":true,\"successText\":\"Manual adjustment record deleted\"}";
        String expected = "{\"status\":200}";
        JSONObject returnJson = new JSONObject(response);
        mockDigiGateway.when((Verification) DigitalGatewayRESTClient.getDGRESTConnection(Mockito.any())).thenReturn(returnJson);
        String actual = returnCheckController.deleteReport(input);
        Assert.assertNotNull(actual);
        Assert.assertEquals(expected, actual);
    }

    @After
    public void after() {
        mockDigiGateway.close();
    }
}
Run Code Online (Sandbox Code Playgroud)

关闭静态模拟仍然出现错误。

java junit mockito

7
推荐指数
1
解决办法
1万
查看次数

标签 统计

java ×1

junit ×1

mockito ×1