最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2020年03月06日 10:27:08   作者:borter  
這篇文章主要介紹了Spring Boot Rest控制器單元測試過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

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

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

XML

Gradle用戶可以在build.gradle 文件中添加以下依賴項(xiàng)。

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

在編寫測試用例之前,應(yīng)該先構(gòu)建RESTful Web服務(wù)。 有關(guān)構(gòu)建RESTful Web服務(wù)的更多信息,請參閱本教程中給出的相同章節(jié)。

編寫REST控制器的單元測試

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

首先,需要創(chuàng)建用于通過使用MockMvc創(chuàng)建Web應(yīng)用程序上下文的Abstract類文件,并定義mapToJson()和mapFromJson()方法以將Java對象轉(zhuǎn)換為JSON字符串并將JSON字符串轉(zhuǎn)換為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)
@WebAppConfiguration
public 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);
 }
}

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

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

@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);
}

POST API測試用例的代碼如下。 此API用于創(chuàng)建產(chǎn)品。

@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");
}

下面給出了PUT API測試用例的代碼。 此API用于更新現(xiàn)有產(chǎn)品。

@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");
}

Delete API測試用例的代碼如下。 此API將刪除現(xiàn)有產(chǎn)品。

@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");
}

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

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");
 }
}

創(chuàng)建一個(gè)可執(zhí)行的JAR文件,并使用下面給出的Maven或Gradle命令運(yùn)行Spring Boot應(yīng)用程序 -

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

mvn clean install

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • @Configuration與@Component作為配置類的區(qū)別詳解

    @Configuration與@Component作為配置類的區(qū)別詳解

    這篇文章主要介紹了@Configuration與@Component作為配置類的區(qū)別詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-06-06
  • Spring?Security登錄表單配置示例詳解

    Spring?Security登錄表單配置示例詳解

    這篇文章主要介紹了Spring?Security登錄表單配置,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-06-06
  • Java基于Tcp協(xié)議的socket編程實(shí)例

    Java基于Tcp協(xié)議的socket編程實(shí)例

    這篇文章主要介紹了Java基于Tcp協(xié)議的socket編程實(shí)例,較為詳細(xì)的分析了socket編程客戶端與服務(wù)器端的具體實(shí)現(xiàn)步驟與使用技巧,具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2014-12-12
  • MyBatis集成Spring流程詳解

    MyBatis集成Spring流程詳解

    在實(shí)際開發(fā)中不僅僅是要展示數(shù)據(jù),還要構(gòu)成數(shù)據(jù)模型添加數(shù)據(jù),這篇文章主要介紹了SpringBoot集成Mybatis操作數(shù)據(jù)庫,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-08-08
  • 關(guān)于Spring Boot獲取bean的3種方式

    關(guān)于Spring Boot獲取bean的3種方式

    這篇文章主要介紹了關(guān)于Spring Boot獲取bean的3種方式,在spring中ApplicationContext這個(gè)上下文對象是獲取bean的基礎(chǔ),需要的朋友可以參考下
    2023-04-04
  • Java線程中synchronized和volatile關(guān)鍵字的區(qū)別詳解

    Java線程中synchronized和volatile關(guān)鍵字的區(qū)別詳解

    這篇文章主要介紹了Java線程中synchronized和volatile關(guān)鍵字的區(qū)別詳解,synchronzied既能夠保障可見性,又能保證原子性,而volatile只能保證可見性,無法保證原子性,volatile不需要加鎖,比synchronized更輕量級,不會阻塞線程,需要的朋友可以參考下
    2024-01-01
  • Springboot mybatis plus druid多數(shù)據(jù)源解決方案 dynamic-datasource的使用詳解

    Springboot mybatis plus druid多數(shù)據(jù)源解決方案 dynamic-datasource的使用詳

    這篇文章主要介紹了Springboot mybatis plus druid多數(shù)據(jù)源解決方案 dynamic-datasource的使用,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-11-11
  • Java和C的隨機(jī)數(shù)(Random)詳解

    Java和C的隨機(jī)數(shù)(Random)詳解

    本篇文章主要介紹了Java和C隨機(jī)數(shù)(Random),現(xiàn)在分享給大家,也給大家做個(gè)參考,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2021-09-09
  • Java數(shù)據(jù)結(jié)構(gòu)及算法實(shí)例:冒泡排序 Bubble Sort

    Java數(shù)據(jù)結(jié)構(gòu)及算法實(shí)例:冒泡排序 Bubble Sort

    這篇文章主要介紹了Java數(shù)據(jù)結(jié)構(gòu)及算法實(shí)例:冒泡排序 Bubble Sort,本文直接給出實(shí)現(xiàn)代碼,代碼中包含詳細(xì)注釋,需要的朋友可以參考下
    2015-06-06
  • java 算法 6種排序小結(jié)

    java 算法 6種排序小結(jié)

    這篇文章主要介紹了java 算法 6種排序,排序原理及實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-10-10

最新評論

疏附县| 吴川市| 定州市| 连州市| 葵青区| 阿合奇县| 乌海市| 新宾| 南昌市| 南涧| 砀山县| 广饶县| 安泽县| 阿巴嘎旗| 湘潭市| 施甸县| 云龙县| 海南省| 柘荣县| 石景山区| 龙陵县| 江安县| 巧家县| 韩城市| 甘孜县| 定西市| 广宗县| 新化县| 保山市| 临颍县| 阳东县| 磐石市| 平塘县| 门头沟区| 榕江县| 陇南市| 遵义县| 甘孜县| 永兴县| 吴川市| 扬州市|