色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

您的位置:首頁技術文章
文章詳情頁

Spring Boot Rest控制器單元測試過程解析

瀏覽:70日期:2023-09-18 14:40:12

Spring Boot提供了一種為Rest Controller文件編寫單元測試的簡便方法。在SpringJUnit4ClassRunner和MockMvc的幫助下,可以創建一個Web應用程序上下文來為Rest Controller文件編寫單元測試。單元測試應該寫在src/test/java目錄下,用于編寫測試的類路徑資源應該放在src/test/resources目錄下。對于編寫單元測試,需要在構建配置文件中添加Spring Boot Starter Test依賴項,如下所示。

<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope></dependency>

XML

Gradle用戶可以在build.gradle 文件中添加以下依賴項。

testCompile(‘org.springframework.boot:spring-boot-starter-test‘)

在編寫測試用例之前,應該先構建RESTful Web服務。 有關構建RESTful Web服務的更多信息,請參閱本教程中給出的相同章節。

編寫REST控制器的單元測試

在本節中,看看如何為REST控制器編寫單元測試。

首先,需要創建用于通過使用MockMvc創建Web應用程序上下文的Abstract類文件,并定義mapToJson()和mapFromJson()方法以將Java對象轉換為JSON字符串并將JSON字符串轉換為Java對象。

package com.yiibai.demo;import java.io.IOException;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import org.springframework.test.context.web.WebAppConfiguration;import org.springframework.test.web.servlet.MockMvc;import org.springframework.test.web.servlet.setup.MockMvcBuilders;import org.springframework.web.context.WebApplicationContext;import com.fasterxml.jackson.core.JsonParseException;import com.fasterxml.jackson.core.JsonProcessingException;import com.fasterxml.jackson.databind.JsonMappingException;import com.fasterxml.jackson.databind.ObjectMapper;@RunWith(SpringJUnit4ClassRunner.class)@SpringBootTest(classes = DemoApplication.class)@WebAppConfigurationpublic abstract class AbstractTest { protected MockMvc mvc; @Autowired WebApplicationContext webApplicationContext; protected void setUp() { mvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build(); } protected String mapToJson(Object obj) throws JsonProcessingException { ObjectMapper objectMapper = new ObjectMapper(); return objectMapper.writeValueAsString(obj); } protected <T> T mapFromJson(String json, Class<T> clazz) throws JsonParseException, JsonMappingException, IOException { ObjectMapper objectMapper = new ObjectMapper(); return objectMapper.readValue(json, clazz); }}

接下來,編寫一個擴展AbstractTest類的類文件,并為每個方法(如GET,POST,PUT和DELETE)編寫單元測試。

下面給出了GET API測試用例的代碼。 此API用于查看產品列表。

@Testpublic void getProductsList() throws Exception { String uri = '/products'; MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.get(uri) .accept(MediaType.APPLICATION_JSON_VALUE)).andReturn(); int status = mvcResult.getResponse().getStatus(); assertEquals(200, status); String content = mvcResult.getResponse().getContentAsString(); Product[] productlist = super.mapFromJson(content, Product[].class); assertTrue(productlist.length > 0);}

POST API測試用例的代碼如下。 此API用于創建產品。

@Testpublic void createProduct() throws Exception { String uri = '/products'; Product product = new Product(); product.setId('3'); product.setName('Ginger'); String inputJson = super.mapToJson(product); MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post(uri) .contentType(MediaType.APPLICATION_JSON_VALUE).content(inputJson)).andReturn(); int status = mvcResult.getResponse().getStatus(); assertEquals(201, status); String content = mvcResult.getResponse().getContentAsString(); assertEquals(content, 'Product is created successfully');}

下面給出了PUT API測試用例的代碼。 此API用于更新現有產品。

@Testpublic void updateProduct() throws Exception { String uri = '/products/2'; Product product = new Product(); product.setName('Lemon'); String inputJson = super.mapToJson(product); MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.put(uri) .contentType(MediaType.APPLICATION_JSON_VALUE).content(inputJson)).andReturn(); int status = mvcResult.getResponse().getStatus(); assertEquals(200, status); String content = mvcResult.getResponse().getContentAsString(); assertEquals(content, 'Product is updated successsfully');}

Delete API測試用例的代碼如下。 此API將刪除現有產品。

@Testpublic void deleteProduct() throws Exception { String uri = '/products/2'; MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.delete(uri)).andReturn(); int status = mvcResult.getResponse().getStatus(); assertEquals(200, status); String content = mvcResult.getResponse().getContentAsString(); assertEquals(content, 'Product is deleted successsfully');}

完整的控制器測試類文件代碼如下 -

package com.yiibai.demo;import static org.junit.Assert.assertEquals;import static org.junit.Assert.assertTrue;import org.junit.Before;import org.junit.Test;import org.springframework.http.MediaType;import org.springframework.test.web.servlet.MvcResult;import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;import com.yiibai.demo.model.Product;public class ProductServiceControllerTest extends AbstractTest { @Override @Before public void setUp() { super.setUp(); } @Test public void getProductsList() throws Exception { String uri = '/products'; MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.get(uri) .accept(MediaType.APPLICATION_JSON_VALUE)).andReturn(); int status = mvcResult.getResponse().getStatus(); assertEquals(200, status); String content = mvcResult.getResponse().getContentAsString(); Product[] productlist = super.mapFromJson(content, Product[].class); assertTrue(productlist.length > 0); } @Test public void createProduct() throws Exception { String uri = '/products'; Product product = new Product(); product.setId('3'); product.setName('Ginger'); String inputJson = super.mapToJson(product); MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post(uri) .contentType(MediaType.APPLICATION_JSON_VALUE) .content(inputJson)).andReturn(); int status = mvcResult.getResponse().getStatus(); assertEquals(201, status); String content = mvcResult.getResponse().getContentAsString(); assertEquals(content, 'Product is created successfully'); } @Test public void updateProduct() throws Exception { String uri = '/products/2'; Product product = new Product(); product.setName('Lemon'); String inputJson = super.mapToJson(product); MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.put(uri) .contentType(MediaType.APPLICATION_JSON_VALUE) .content(inputJson)).andReturn(); int status = mvcResult.getResponse().getStatus(); assertEquals(200, status); String content = mvcResult.getResponse().getContentAsString(); assertEquals(content, 'Product is updated successsfully'); } @Test public void deleteProduct() throws Exception { String uri = '/products/2'; MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.delete(uri)).andReturn(); int status = mvcResult.getResponse().getStatus(); assertEquals(200, status); String content = mvcResult.getResponse().getContentAsString(); assertEquals(content, 'Product is deleted successsfully'); }}

創建一個可執行的JAR文件,并使用下面給出的Maven或Gradle命令運行Spring Boot應用程序 -

對于Maven,可以使用下面給出的命令

mvn clean install

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: Spring
相關文章:
主站蜘蛛池模板: 一本一本久久a久久精品综合麻豆 | 日韩高清一级毛片 | 国产一区二区三区亚洲综合 | 亚洲大片免费 | 视频一区久久 | 国产精品成久久久久三级 | 国产成人啪精品视频免费软件 | 国产日本在线 | 国产一区二区免费在线观看 | 女人张开腿男人猛桶视频 | 国产美女一级特黄毛片 | 欧美成人午夜片一一在线观看 | 波多野结衣中文在线播放 | 国产精品18久久久久久久久久 | 国产在线手机视频 | 久久99久久精品国产只有 | 亚洲在线精品 | 一级毛片免费观看不卡的 | 日本欧美一区二区三区高清 | 日韩aⅴ在线观看 | 久久99这里只有精品国产 | 兔子先生节目在线观看免费 | 欧美性色黄大片www 欧美性色黄大片一级毛片视频 | 日本特级淫片免费看 | 中文一区 | 最新中文字幕一区二区乱码 | 亚洲国产精品成人综合久久久 | 九九九九热精品视频 | aaa级大片| 免费一级特黄 欧美大片 | 亚洲成年网站在线观看 | 在线观看精品自拍视频 | ⅹxx中国xxx人妖 | 欧美aaaaa| 国产精品久久自在自线观看 | 精品视频在线播放 | 免费欧美一级片 | 中文字幕一级毛片视频 | 国产精在线 | 日韩欧美在线观看视频一区二区 | 最新国产三级在线观看不卡 |