弹簧启动和角度测试获取错误406

时间:2019-01-25 13:47:24

标签: angular unit-testing spring-boot karma-jasmine

地狱。我正在与Jasmine,Karma并进行角度测试。当我执行下一个测试时,出现此异常:

feign.FeignException:状态406读取AppRbp#getReports(DTOReport)

代码: 我正在测试的方法

  exportReportSwitch() {
switch (this.modulo) {
  case 'RBP':
    this.serviceRbp.getReports(this.reportSelected).subscribe(response => this.savePdf(response, this.reportSelected));
    break;
  case 'CCR':
    this.serviceCcr.getReports(this.reportSelected).subscribe(response => this.savePdf(response, this.reportSelected));
  break;
  case 'Micro':
    this.serviceMicro.getReports(this.reportSelected).subscribe(response => this.savePdf(response, this.reportSelected));
  break;
  default:
  break;
}

测试

it('Test unitario metodo exportReportSwitch() en caso de que Sea Micro', () => {
//Arrange
const serviceMicro = fixture.debugElement.injector.get(MicroService);
const serviceSpy = spyOn(serviceMicro, 'getReports').and.callThrough();
when(serviceMicro.getReports('Listado 1').subscribe()).thenReturn();
component.modulo = 'Micro';
//Act
component.exportReportSwitch();
//Assert
expect(serviceSpy).toHaveBeenCalled();  });

后端方法

@CrossOrigin
@RequestMapping(value = "/getReports", method = RequestMethod.POST, produces = "application/json", consumes = "application/json")
@ResponseBody
public ResponseEntity<byte[]> getReports(@RequestBody DTOReports dto) {
    String nameFile = dto.getReport();
    String jrxmlDir = "src/main/resources/jrxml/";
    String jasperDir = jrxmlDir + "/jasper/";
    byte[] outputPDF=null;
    try {
        File mainfile = ResourceUtils.getFile(jrxmlDir +nameFile + ".jrxml");
        compilerToJasper(jrxmlDir, jasperDir, nameFile);

        Map<String, Object> parameters = new HashMap<>();
        parameters.put("SUBREPORT_DIR", jasperDir);
        JasperReport report=JasperCompileManager.compileReport(mainfile.getAbsolutePath());
        JasperPrint jasperPrint = JasperFillManager.fillReport(report,parameters,this.dataSource.getConnection());

        JRPdfExporter exporter = new JRPdfExporter();
        exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
        ByteArrayOutputStream pdfReportStream = new ByteArrayOutputStream();
        exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(pdfReportStream));
        exporter.exportReport();

        outputPDF = pdfReportStream.toByteArray();
    } catch (Exception e) {
        System.out.println("Se ha producido la siguiente excepcion: " + e);
    }

    HttpHeaders responseHeaders = new HttpHeaders();
    responseHeaders.setContentType(MediaType.valueOf("application/pdf"));
    responseHeaders.setContentLength(outputPDF.length);
    return new ResponseEntity<byte[]>(outputPDF, responseHeaders, HttpStatus.OK);

}

角度服务

    getReports(file: String): Observable<any> {
    let body = JSON.stringify({
        'report': file
    });
    return this.llamarServicioDownloadFile(this.baseURL_RBP + GET_REPORTS_RBP, body);
}

最后是图片中的错误

error 如果您需要更多信息,请不要问我。我在StackOverflow中是菜鸟 对不起,我的英语,我是西班牙语

1 个答案:

答案 0 :(得分:0)

最后,我找到了解决方案。我需要Smock服务。

 const serviceSpy = spyOn(serviceMicro, 'getReports').and.returnValue(Observable.of());

代替:

     const serviceSpy = spyOn(serviceMicro, 'getReports').and.callThrough();;