在测试中模拟Spring服务时出现空例外

时间:2018-06-05 20:40:40

标签: spring spring-mvc spring-boot mocking mockito

我正在尝试模拟Spring服务并使用Web MVC上下文来测试正确的响应从控制器返回JSON对象。似乎异常是在控制器中捕获的,但指向null,因此返回没有正文的不成功响应。

我不确定这里可能缺少什么注释。该服务确实需要构造函数中的SSRS配置对象,并且在测试时进行调试时,它在控制器try / catch块中为空。

如果那是问题,我不确定如何修复它。如果没有,我不确定如何防止抛出异常。

@Slf4j
@RestController
@RequestMapping("/api/reports")
public class ReportsController {
    private final ReportsService reportsService;

    public ReportsController(@NonNull ReportsService reportsService) {
        this.reportsService = reportsService;
    }

    @PutMapping("/getReport")
    @Step
    public ApiResult getReport(@RequestBody ReportParamJSON params)
    {
        ApiResult result = new ApiResult();

        try {
            byte[] rptStream = reportsService.getReport(params.name, params.params, params.format);
            result.body = new String(Base64.getEncoder().encode(rptStream));
            result.success = true;
        }
        catch (Exception exc)
        {
            result.success = false;
            result.messages.add(exc.toString());
        }

        return result;
    }
}

@Slf4j
@Service
public class ReportsService {
private final SsrsConfig ssrsConfig;

    public ReportsService(@NonNull SsrsConfig ssrsConfig) {
        this.ssrsConfig = ssrsConfig;
    }

    @Step
    public byte[] getReport(String report, List<ReportParam> params, String format) throws Exception{
        // return some binary data.
    }
}

@RunWith(SpringRunner.class)
@WebMvcTest
@WithMockUser
public class ReportsControllerTests {

    @Autowired
    private MockMvc mockMvc;

    @MockBean
    private ReportsService reportsService;

    @Before
    public void initMocks(){
        MockitoAnnotations.initMocks(this);
    }

    @Test
    public void givenGetReportUri_whenRequestReport_thenReturnReport() throws Exception {

        String response = "{\"body\":\"data\",\"success\":true,\"messages\":[]}";

        ArrayList<ReportParam> params = new ArrayList<>();
        params.add(new ReportParam("number", "1000"));
        params.add(new ReportParam("status", "1"));
        when(reportsService.getReport("test", params, "PDF")).thenReturn("data".getBytes());


        mockMvc.perform(put("/api/reports/getReport")
            .content("{\n" +
                    "  \"format\": \"PDF\",\n" +
                    "  \"name\": \"test\",\n" +
                    "  \"params\": [\n" +
                    "    {\n" +
                    "      \"name\": \"number\",\n" +
                    "      \"value\": \"1000\"\n" +
                    "    },\n" +
                    "    {\n" +
                    "      \"name\": \"status\",\n" +
                    "      \"value\": \"1\"\n" +
                    "    }\n" +
                    "  ]\n" +
                    "}")
            .contentType(MediaType.APPLICATION_JSON)).andExpect(status().isOk())
            .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
            .andExpect(content().string(response));
    }
}

@ConfigurationProperties(prefix = "ssrs")
@Component
@Data
public class SsrsConfig {
    private String endpointUri;
}

0 个答案:

没有答案