简体   繁体   English

Spring Boot 集成测试。 如何测试“删除”方法?

[英]Spring Boot integration test. How to test the 'DELETE' method?

I'm using Spring Boot 2.5.6 and JUnit 4.13.2 .我正在使用Spring Boot 2.5.6JUnit 4.13.2 My task is to test the DELETE method我的任务是测试DELETE方法

My REST controller:我的 REST 控制器:

@RestController
public class DomainEndpoint {

    private final SomeService service;

    @DeleteMapping("/domain/{id}")
    public void delete(@PathVariable long id) {
        service.delete(id);
    }
}

My test:我的测试:

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@RunWith(SpringRunner.class)
public class DomainEndpointTest {

    @Autowired
    TestRestTemplate template;

    @MockBean
    SomeService service;

    @Test
    public void delete() {
        String url = "/domain/123";
        ResponseEntity<?> resp = template.exchange(url, HttpMethod.DELETE, new HttpEntity<>(""), String.class);
        assertEquals(HttpStatus.NO_CONTENT, resp.getStatusCode());
    }
}

As you can see, the only solution for testing the 'DELETE' method, which I found, is:如您所见,我发现测试“DELETE”方法的唯一解决方案是:

ResponseEntity<?> resp = template.exchange(url, HttpMethod.DELETE, new HttpEntity<>(""), String.class);

But params for body new HttpEntity<>("") and for return type String.class seem strange to me.但是 body new HttpEntity<>("")和返回类型String.class对我来说似乎很奇怪。 Why should I use them?我为什么要使用它们? Can I do the same more straightway without passing unnecessary parameters?我可以更直接地做同样的事情而不传递不必要的参数吗?
On the other hand, TestRestTemplate template has a set of short and readable methods delete() .另一方面, TestRestTemplate template有一组简短易读的方法delete() The problem with them - they return void and I can't check the response status code in this case.他们的问题 - 他们返回void ,在这种情况下我无法检查响应状态代码。

The main question is how to test DELETE methods correctly?主要问题是如何正确测试DELETE方法?

Instead of passing in new HttpEntity<>("") HttpEntity has a special class variable you can use called HttpEntity.EMPTY for these situations. HttpEntity有一个特殊的类变量,您可以在这些情况下使用名为HttpEntity.EMPTY变量,而不是传入new HttpEntity<>("") You can also use a return type of void.您还可以使用 void 返回类型。

ResponseEntity<Void> resp = template.exchange(url, HttpMethod.DELETE, HttpEntity.EMPTY, Void.class);

Two things you could improve:你可以改进的两件事:

  1. Don't provide a request entity – it is marked as @Nullable不要提供请求实体——它被标记为@Nullable
  2. Use Void.class as return type to express that you don't expect any response body使用Void.class作为返回类型来表示您不期望任何响应正文
ResponseEntity<Void> resp = restTemplate.exchange(url, HttpMethod.DELETE, null, Void.class);

Others have given different options but if you want to use the the delete method - it internally handles the http error by throwing runtime error as you can see here .其他人给出不同的选择,但如果你想使用的delete方法-它在内部通过抛出运行时错误处理HTTP错误,你可以看到在这里 You can check for HttpClientErrorException or HttpServerErrorException by using assertDoesNotThrow from JUnit5您可以使用assertDoesNotThrow的 assertDoesNotThrow 检查HttpClientErrorExceptionHttpServerErrorException

在我看来 RestTemplate.delete 方法是可以使用的方法,返回类型为 void 但状态 2XX 以外的状态将引发异常。

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

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