欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Spring Boot从Controller层进行单元测试的实现

程序员文章站 2022-04-05 23:02:35
单元测试是程序员对代码的自测,一般公司都会严格要求单元测试,这是对自己代码的负责,也是对代码的敬畏。一般单元测试都是测试service层,下面我将演示从controller层进行单元测试。无参cont...

单元测试是程序员对代码的自测,一般公司都会严格要求单元测试,这是对自己代码的负责,也是对代码的敬畏。

一般单元测试都是测试service层,下面我将演示从controller层进行单元测试。

无参controller单元测试示例:

package com.pingan.bloan.genesis.controller.base;

import org.junit.after;
import org.junit.before;
import org.junit.runner.runwith;
import com.pingan.bloan.genesis.logwrapper.logwrapper;
import org.springframework.boot.test.context.springboottest;
import org.springframework.test.context.junit4.springrunner;
import com.pingan.bloan.genesis.logwrapper.logwrapperfactory;
import org.springframework.test.context.web.webappconfiguration;

@runwith(springrunner.class)
@springboottest
@webappconfiguration
public class basespringboottest {
  protected logwrapper logger = logwrapperfactory.getlogwrapper(basespringboottest.class);
  
  @before
  public void init() {
    logger.info("开始测试...");
  }
  
  @after
  public void after() {
    logger.info("测试结束...");
  }
}

package com.pingan.bloan.genesis.controller;

import org.junit.test;
import org.junit.before;
import org.springframework.test.web.servlet.mockmvc;
import org.springframework.test.web.servlet.mvcresult;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.test.web.servlet.setup.mockmvcbuilders;
import com.pingan.bloan.genesis.controller.base.basespringboottest;
import org.springframework.test.web.servlet.result.mockmvcresulthandlers;
import org.springframework.test.web.servlet.result.mockmvcresultmatchers;
import org.springframework.test.web.servlet.request.mockmvcrequestbuilders;

/**
 * democontroller测试
 * @author 
 *
 */
public class democontrollertest extends basespringboottest {
  @autowired
  private democontroller democontroller;
  
  private mockmvc mockmvc;
  
  @before
  public void setup() {
    mockmvc = mockmvcbuilders.standalonesetup(democontroller).build();
  }
  
  @test
  public void demo() throws exception {
    mvcresult mvcresult = mockmvc.perform(mockmvcrequestbuilders.get("/demo/demo"))
                   .andexpect(mockmvcresultmatchers.status().isok())
                   .anddo(mockmvcresulthandlers.print())
                   .andreturn();

    logger.info(mvcresult.getresponse().getcontentasstring());
  }
}

有参controller单元测试示例一:

package com.pingan.bloan.genesis.controller;

import org.junit.test;
import org.junit.before;
import org.springframework.http.mediatype;
import org.springframework.test.web.servlet.mockmvc;
import org.springframework.test.web.servlet.mvcresult;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.test.web.servlet.setup.mockmvcbuilders;
import com.pingan.bloan.genesis.controller.base.basespringboottest;
import org.springframework.test.web.servlet.result.mockmvcresulthandlers;
import org.springframework.test.web.servlet.result.mockmvcresultmatchers;
import org.springframework.test.web.servlet.request.mockmvcrequestbuilders;

/**
 * 加解密controller测试
 * 
 * @author 
 *
 */
public class messageencryptioncontrollertest extends basespringboottest {
  @autowired
  private messageencryptioncontroller messageencryptioncontroller;
  
  private mockmvc mockmvc;
  
  @before
  public void setup() {
    mockmvc = mockmvcbuilders.standalonesetup(messageencryptioncontroller).build();
  }
  
  /**
   * 加密测试
   * @throws exception 
   */
  @test
  public void encryption() throws exception {
    mvcresult mvcresult = mockmvc.perform(mockmvcrequestbuilders.post("/secret/encryption")
                   .accept(mediatype.application_json).param("origincontent", "15221365094"))
                   .andexpect(mockmvcresultmatchers.status().isok())
                   .anddo(mockmvcresulthandlers.print())
                   .andreturn();
    
    logger.info(mvcresult.getresponse().getcontentasstring());
  }
  
  /**
   * 解密测试
   * @throws exception 
   */
  @test
  public void deciphering() throws exception {
    mvcresult mvcresult = mockmvc.perform(mockmvcrequestbuilders.post("/secret/deciphering")
                   .accept(mediatype.application_json).param("secretcontent", "4bd6ee1a13593f97e6cea20a2ba9e6e4"))
                   .andexpect(mockmvcresultmatchers.status().isok())
                   .anddo(mockmvcresulthandlers.print())
                   .andreturn();

    logger.info(mvcresult.getresponse().getcontentasstring());
  }
}

有参controller单元测试示例二:

package com.pingan.bloan.genesis.controller;

import org.junit.test;
import org.junit.before;
import com.alibaba.fastjson.jsonobject;
import org.springframework.http.mediatype;
import org.springframework.test.web.servlet.mockmvc;
import org.springframework.test.web.servlet.mvcresult;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.test.web.servlet.setup.mockmvcbuilders;
import com.pingan.bloan.genesis.controller.base.basespringboottest;
import org.springframework.test.web.servlet.result.mockmvcresulthandlers;
import org.springframework.test.web.servlet.result.mockmvcresultmatchers;
import org.springframework.test.web.servlet.request.mockmvcrequestbuilders;
import com.pingan.bloan.genesis.model.mobiletelephone.mobiletelephonerequest;

/**
 * 客户手机掩码信息加密处理测试
 * @author 
 *
 */
public class mobiletelephoneidempotentcontrollertest extends basespringboottest {
  @autowired
  private mobiletelephoneidempotentcontroller mobiletelephoneidempotentcontroller;
  
  private mockmvc mockmvc;
  
  @before
  public void setup() {
    mockmvc = mockmvcbuilders.standalonesetup(mobiletelephoneidempotentcontroller).build();
  }
  
  /**
   * 保存加密处理的客户信息
   * @throws exception 
   */
  @test
  public void savecustomerencryptionmessage() throws exception {
    mobiletelephonerequest request = new mobiletelephonerequest();
    request.setcustomerid("rl20180304000099");
    request.setmobiletelephone("18883270484");
    request.setccfmobiletelephone("13904108866");
    
    mvcresult mvcresult = mockmvc.perform(mockmvcrequestbuilders.post("/customerencryption/save")
                   .contenttype(mediatype.application_json)
                   .content(jsonobject.tojsonstring(request)))
                   .andexpect(mockmvcresultmatchers.status().isok())
                   .anddo(mockmvcresulthandlers.print())
                   .andreturn();
    
    logger.info(mvcresult.getresponse().getcontentasstring());
  }
}

suite一次性执行多个单元测试:

package com.pingan.bloan.genesis.controller;

import org.junit.runners.suite;
import org.junit.runner.runwith;

/**
 * 打包测试
 * 配置测试类,一次性执行所有配置的测试类
 * @author 
 *
 */
@runwith(suite.class)
@suite.suiteclasses({messageencryptioncontrollertest.class, democontrollertest.class})
public class suiteexecutetests {
  // 不用写代码,只需要注解即可(在suiteclasses中配置测试类)
}

代码单元测试,让代码更加健壮。

到此这篇关于spring boot从controller层进行单元测试的实现的文章就介绍到这了,更多相关spring boot controller单元测试内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!