【问题标题】:Mockito Multipart file argument matcherMockito 多部分文件参数匹配器
【发布时间】:2019-10-02 02:32:54
【问题描述】:

我正在尝试进行一个集成测试,该测试会为 void 方法引发异常以模拟停机服务。该方法有一个字符串参数和一个多部分文件作为参数,即使为具有两个字符串参数的 void 方法引发异常,它似乎也不起作用。

工作集成测试:

    @Test
    @DisplayName("500 response -- downed case mgmt microservice")
    public void downedCaseMgmt() throws Exception {
        BDDMockito.doThrow(new RuntimeException("mocking an error")).when(reportEventService).reportDocUpload(ArgumentMatchers.any(String.class), ArgumentMatchers.anyString());

        //Rest assured portion
        given().
                multiPart("file", xlsxGoodFile).
                params(paramsMap).
        when().
                post("").
        then().
                statusCode(500);
    }

非工作集成测试:

    @Test
    @DisplayName("500 response -- downed object storage")
    public void downedObjectStorage() throws Exception {
        BDDMockito.doThrow(new RuntimeException("mocking an error")).when(objectStorageService).saveFileToObjectStorage(ArgumentMatchers.anyString(), ArgumentMatchers.any(File.class));

        //Rest assured portion
        given().
                multiPart("file", xlsxGoodFile).
                params(paramsMap).
        when().
                post("").
        then().
                statusCode(500);
    }

【问题讨论】:

    标签: mockito integration-testing void


    【解决方案1】:

    原来函数 saveFileToObjectStorage 有一个空值,因为 objectStorageService 上的 mockbean 以及我在模拟返回的事实。我的错误,我用以下代码解决了它:

        @Test
        @DisplayName("500 response -- downed db")
        public void downedDb() throws Exception {
            BDDMockito.doThrow(new RuntimeException("mocking an error")).when(excelDataRepository).
                    save(ArgumentMatchers.any());
    
            //Rest assured portion
            given().
                    multiPart("file", xlsxGoodFile).
                    params(paramsMap).
            when().
                    post("").
            then().
                    statusCode(500);
        }
    

    注意:ArgumentMatchers 的 any()

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-05-03
      • 1970-01-01
      • 2011-11-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-22
      相关资源
      最近更新 更多