简体   繁体   English

SpringBoot和REST保证,抛出404时得到406

[英]SpringBoot and REST-assured, getting 406 when 404 is thrown

In this answer , I was using REST-assured to test a post action of a file. 这个答案中 ,我使用了REST保证测试文件的后操作。 The controller was: 控制器是:

@RestController
@RequestMapping("file-upload")
public class MyRESTController {

  @Autowired
  private AService aService;

  @RequestMapping(method = RequestMethod.POST, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
  @ResponseStatus(HttpStatus.CREATED)
  public void fileUpload(
      @RequestParam(value = "file", required = true) final MultipartFile file,
      @RequestParam(value = "something", required = true) final String something) {
   aService.doSomethingOnDBWith(file, value);
  }
}

The controller was tested as in this answer . 控制器已按照此答案进行测试。

Now I have an exception: 现在我有一个例外:

@ResponseStatus(value=HttpStatus.NOT_FOUND)
public class XNotFoundException extends RuntimeException {

    public XNotFoundException(final String message) {
        super(message);
    }
}

and I test the case when the service throws that exception as follows: 当服务引发该异常时,我将测试这种情况,如下所示:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = MyApplication.class)
@WebAppConfiguration
@IntegrationTest({"server.port:0"})
public class ControllerTest{

    {
      System.setProperty("spring.profiles.active", "unit-test");
    }


    @Autowired
    @Spy
    AService aService;

    @Autowired
    @InjectMocks
    MyRESTController controller;

    @Value("${local.server.port}")
    int port;    


  @Before public void setUp(){
    RestAssured.port = port;

    MockitoAnnotations.initMocks(this);
  }

  @Test
  public void testFileUpload() throws Exception{
        final File file = getFileFromResource(fileName);

        doThrow(new XNotFoundException("Keep calm, is a test")).when(aService)  
             .doSomethingOnDBWith(any(MultipartFile.class), any(String.class));

        given()
          .multiPart("file", file)
          .multiPart("something", ":(")
          .when().post("/file-upload")
          .then().(HttpStatus.NOT_FOUND.value());
    }
}

But when I run the test I obtain: 但是当我运行测试时,我得到:

java.lang.AssertionError: Expected status code <404> doesn't match actual status code <406>.

How can I solve this? 我该如何解决?

You may need to set the content-type header to "multipart/form-data". 您可能需要将内容类型标头设置为“ multipart / form-data”。 For example: 例如:

given()
       .contentType(MediaType.MULTIPART_FORM_DATA_VALUE)
       .multiPart("file", file)
       .multiPart("something", ":(")
       .when().post("/file-upload")
       .then()...;

If that doesn't work you can try specify it for the individual multiparts: 如果那不起作用,您可以尝试为各个多部分指定它:

given()
       .multiPart("file", file, MediaType.MULTIPART_FORM_DATA_VALUE)
       .multiPart("something", ":(", MediaType.MULTIPART_FORM_DATA_VALUE)
       .when().post("/file-upload")
       .then()...;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM