欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Spring Boot Rest控制器單元測(cè)試過(guò)程解析

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

Spring Boot提供了一種為Rest Controller文件編寫單元測(cè)試的簡(jiǎn)便方法。在SpringJUnit4ClassRunner和MockMvc的幫助下,可以創(chuàng)建一個(gè)Web應(yīng)用程序上下文來(lái)為Rest Controller文件編寫單元測(cè)試。
單元測(cè)試應(yīng)該寫在src/test/java目錄下,用于編寫測(cè)試的類路徑資源應(yīng)該放在src/test/resources目錄下。
對(duì)于編寫單元測(cè)試,需要在構(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‘)

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

編寫REST控制器的單元測(cè)試

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

首先,需要?jiǎng)?chuàng)建用于通過(guò)使用MockMvc創(chuàng)建Web應(yīng)用程序上下文的Abstract類文件,并定義mapToJson()和mapFromJson()方法以將Java對(duì)象轉(zhuǎn)換為JSON字符串并將JSON字符串轉(zhuǎn)換為Java對(duì)象。

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

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

下面給出了GET API測(cè)試用例的代碼。 此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測(cè)試用例的代碼如下。 此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測(cè)試用例的代碼。 此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測(cè)試用例的代碼如下。 此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");
}

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

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)用程序 -

對(duì)于Maven,可以使用下面給出的命令

mvn clean install

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

相關(guān)文章

  • java實(shí)現(xiàn)String字符串處理各種類型轉(zhuǎn)換

    java實(shí)現(xiàn)String字符串處理各種類型轉(zhuǎn)換

    在日常的程序開發(fā)中,經(jīng)常會(huì)涉及到不同類型之間的轉(zhuǎn)換,本文主要介紹了String字符串處理各種類型轉(zhuǎn)換,具有一定的參考價(jià)值,感興趣的可以了解一下
    2023-10-10
  • 利用java監(jiān)聽(tīng)器實(shí)現(xiàn)在線人數(shù)統(tǒng)計(jì)

    利用java監(jiān)聽(tīng)器實(shí)現(xiàn)在線人數(shù)統(tǒng)計(jì)

    過(guò)去使用ASP和ASP.NET兩種編程的時(shí)候,都寫過(guò)在線人數(shù)統(tǒng)計(jì)能,實(shí)現(xiàn)功能挺簡(jiǎn)單的!今天使用java來(lái)實(shí)現(xiàn)在線人數(shù)統(tǒng)計(jì)有點(diǎn)另類,是通過(guò)Java監(jiān)聽(tīng)器實(shí)現(xiàn)的,需要的朋友可以參考下
    2015-09-09
  • 詳解spring boot配置單點(diǎn)登錄

    詳解spring boot配置單點(diǎn)登錄

    本篇文章主要介紹了詳解spring boot配置單點(diǎn)登錄,常用的安全框架有spring security和apache shiro。shiro的配置和使用相對(duì)簡(jiǎn)單,本文使用shrio對(duì)接CAS服務(wù)。
    2017-03-03
  • Java使用設(shè)計(jì)模式中的代理模式構(gòu)建項(xiàng)目的實(shí)例展示

    Java使用設(shè)計(jì)模式中的代理模式構(gòu)建項(xiàng)目的實(shí)例展示

    這篇文章主要介紹了Java使用設(shè)計(jì)模式中的代理模式構(gòu)建項(xiàng)目的實(shí)例展示,代理模式中的代理對(duì)象可以在客戶端和目標(biāo)對(duì)象之間起到中介的作用,需要的朋友可以參考下
    2016-05-05
  • 配置Ant執(zhí)行Jmeter腳本過(guò)程詳解

    配置Ant執(zhí)行Jmeter腳本過(guò)程詳解

    這篇文章主要介紹了配置Ant執(zhí)行Jmeter腳本過(guò)程詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-09-09
  • java實(shí)現(xiàn)找出兩個(gè)文件中相同的單詞(兩種方法)

    java實(shí)現(xiàn)找出兩個(gè)文件中相同的單詞(兩種方法)

    這篇文章主要介紹了java實(shí)現(xiàn)找出兩個(gè)文件中相同的單詞(兩種方法),需要的朋友可以參考下
    2020-08-08
  • JAVA8之函數(shù)式編程Function接口用法

    JAVA8之函數(shù)式編程Function接口用法

    這篇文章主要介紹了JAVA8之函數(shù)式編程Function接口用法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-12-12
  • Java向數(shù)據(jù)庫(kù)中插入數(shù)據(jù)后獲取自增ID的常用方法

    Java向數(shù)據(jù)庫(kù)中插入數(shù)據(jù)后獲取自增ID的常用方法

    有時(shí)候因?yàn)樾略龅男枨笮枰@取剛剛新增的數(shù)據(jù)的自增的主鍵ID,下面這篇文章主要給大家介紹了關(guān)于Java向數(shù)據(jù)庫(kù)中插入數(shù)據(jù)后獲取自增ID的常用方法,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-11-11
  • SpringBoot中Zookeeper分布式鎖的原理和用法詳解

    SpringBoot中Zookeeper分布式鎖的原理和用法詳解

    Zookeeper是一個(gè)分布式協(xié)調(diào)服務(wù),它提供了高可用、高性能、可擴(kuò)展的分布式鎖機(jī)制,SpringBoot是一個(gè)基于Spring框架的開發(fā)框架,它提供了對(duì)Zookeeper分布式鎖的集成支持,本文將介紹SpringBoot中的 Zookeeper分布式鎖的原理和使用方法,需要的朋友可以參考下
    2023-07-07
  • zookeeper集群搭建超詳細(xì)過(guò)程

    zookeeper集群搭建超詳細(xì)過(guò)程

    這篇文章主要介紹了zookeeper集群搭建超詳細(xì)過(guò)程,本文對(duì)zookeeper集群測(cè)試通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2022-06-06

最新評(píng)論