當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
使用Spring Boot对REST URL进行集成测试
生活随笔
收集整理的這篇文章主要介紹了
使用Spring Boot对REST URL进行集成测试
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
我們正在構建一個具有REST接口的Spring Boot應用程序,并且在某個時候我們想測試我們的REST接口,并在可能的情況下將此測試與常規單元測試集成。 一種方法是@Autowire我們的REST控制器,并使用它來調用我們的端點。 但是,這不會完全融合,因為它將跳過JSON反序列化和全局異常處理之類的事情。 因此,對我們而言,理想的情況是在單元測試開始時啟動應用程序,并在上一次單元測試后再次關閉它。
碰巧的是,Spring Boot用一個注釋為我們完成了所有工作: @IntegrationTest 。
這是一個抽象類的示例實現,您可以將其用于單元測試,該類將在啟動單元測試,緩存并最終關閉之前自動啟動應用程序。
package demo;import java.util.ArrayList; import java.util.List; import java.util.Map;import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.test.IntegrationTest; import org.springframework.boot.test.SpringApplicationConfiguration; import org.springframework.boot.test.TestRestTemplate; import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; import org.springframework.http.ResponseEntity; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.web.WebAppConfiguration;import com.fasterxml.jackson.databind.ObjectMapper;@RunWith(SpringJUnit4ClassRunner.class) // Your spring configuration class containing the @EnableAutoConfiguration // annotation @SpringApplicationConfiguration(classes = Application.class) // Makes sure the application starts at a random free port, caches it throughout // all unit tests, and closes it again at the end. @IntegrationTest("server.port:0") @WebAppConfiguration public abstract class AbstractIntegrationTest {// Will contain the random free port number@Value("${local.server.port}")private int port;/*** Returns the base url for your rest interface* * @return*/private String getBaseUrl() {return "http://localhost:" + port;}// Some convenience methods to help you interact with your rest interface/*** @param requestMappingUrl* should be exactly the same as defined in your RequestMapping* value attribute (including the parameters in {})* RequestMapping(value = yourRestUrl)* @param serviceReturnTypeClass* should be the the return type of the service* @param parametersInOrderOfAppearance* should be the parameters of the requestMappingUrl ({}) in* order of appearance* @return the result of the service, or null on error*/protected <T> T getEntity(final String requestMappingUrl, final Class<T> serviceReturnTypeClass, final Object... parametersInOrderOfAppearance) {// Make a rest template do do the service callfinal TestRestTemplate restTemplate = new TestRestTemplate();// Add correct headers, none for this examplefinal HttpEntity<String> requestEntity = new HttpEntity<String>(new HttpHeaders());try {// Do a call the the urlfinal ResponseEntity<T> entity = restTemplate.exchange(getBaseUrl() + requestMappingUrl, HttpMethod.GET, requestEntity, serviceReturnTypeClass,parametersInOrderOfAppearance);// Return resultreturn entity.getBody();} catch (final Exception ex) {// Handle exceptions}return null;}/*** @param requestMappingUrl* should be exactly the same as defined in your RequestMapping* value attribute (including the parameters in {})* RequestMapping(value = yourRestUrl)* @param serviceListReturnTypeClass* should be the the generic type of the list the service* returns, eg: List<serviceListReturnTypeClass>* @param parametersInOrderOfAppearance* should be the parameters of the requestMappingUrl ({}) in* order of appearance* @return the result of the service, or null on error*/protected <T> List<T> getList(final String requestMappingUrl, final Class<T> serviceListReturnTypeClass, final Object... parametersInOrderOfAppearance) {final ObjectMapper mapper = new ObjectMapper();final TestRestTemplate restTemplate = new TestRestTemplate();final HttpEntity<String> requestEntity = new HttpEntity<String>(new HttpHeaders());try {// Retrieve listfinal ResponseEntity<List> entity = restTemplate.exchange(getBaseUrl() + requestMappingUrl, HttpMethod.GET, requestEntity, List.class, parametersInOrderOfAppearance);final List<Map<String, String>> entries = entity.getBody();final List<T> returnList = new ArrayList<T>();for (final Map<String, String> entry : entries) {// Fill return list with converted objectsreturnList.add(mapper.convertValue(entry, serviceListReturnTypeClass));}return returnList;} catch (final Exception ex) {// Handle exceptions}return null;}/*** * @param requestMappingUrl* should be exactly the same as defined in your RequestMapping* value attribute (including the parameters in {})* RequestMapping(value = yourRestUrl)* @param serviceReturnTypeClass* should be the the return type of the service* @param objectToPost* Object that will be posted to the url* @return*/protected <T> T postEntity(final String requestMappingUrl, final Class<T> serviceReturnTypeClass, final Object objectToPost) {final TestRestTemplate restTemplate = new TestRestTemplate();final ObjectMapper mapper = new ObjectMapper();try {final HttpEntity<String> requestEntity = new HttpEntity<String>(mapper.writeValueAsString(objectToPost));final ResponseEntity<T> entity = restTemplate.postForEntity(getBaseUrl() + requestMappingUrl, requestEntity, serviceReturnTypeClass);return entity.getBody();} catch (final Exception ex) {// Handle exceptions}return null;} }翻譯自: https://www.javacodegeeks.com/2015/03/integration-testing-on-rest-urls-with-spring-boot.html
總結
以上是生活随笔為你收集整理的使用Spring Boot对REST URL进行集成测试的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 余额宝利率是多少?
- 下一篇: 在非托管对象中使用Spring托管Bea