zoukankan      html  css  js  c++  java
  • spring boot快速入门 9: 单元测试

    进行单元测试:

    service第一种方式:

    第一步:在指定service中创建一个方法进行测试

     /**
         * 通过ID查询一个女生的信息
         * @param id
         * @return
         */
        public Girl findOne(Integer id){
            return girlRespository.findOne(id);
        }

    第二步:在test文件夹下指定的包中创建GirlServiceTest

    package com.payease;
    
    import com.payease.domain.Girl;
    import com.payease.service.GirlService;
    import org.junit.Assert;
    import org.junit.Test;
    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.SpringRunner;
    
    /**
     * service 测试类
     * Created by liuxiaoming on 2017/11/8.
     */
    @RunWith(SpringRunner.class)
    @SpringBootTest
    public class GirlServiceTest{
    
        @Autowired
        private GirlService girlService;
    
        @Test
        public void findOneTest(){
            Girl girl = girlService.findOne(13);
            Assert.assertEquals((Object) new Integer(20), girl.getAge());
        }
    
    }

    第三步:查看数据库信息  启动测试类

     测试通过:

    若下图原数字20改为21:

    测试结果:

     service第二种方式:

     第一步:在service中找到该方法。 鼠标右键 选择 go to--test--Create New Test. . .--勾选你所要测试的方法--OK

    第二步:点击OK后 在test目录中自动生成包和文件

    controller单元测试:

     第一步:找到对应 controller中的将要测试的方法 点击鼠标右键 选择 go to--test--Create New Test. . .--勾选你所要测试的方法--OK

    第二步:点击OK后 在test目录中自动生成包和文件

    第三步:编写GirlControllerTest 期望返回状态: .andExpect(MockMvcResultMatchers.status().isOk());

    package com.payease.controller;
    
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.test.context.junit4.SpringRunner;
    import org.springframework.test.web.servlet.MockMvc;
    import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
    import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
    
    /**
     * controller测试类
     * Created by liuxiaoming on 2017/11/8.
     */
    @RunWith(SpringRunner.class)
    @SpringBootTest
    @AutoConfigureMockMvc
    public class GirlControllerTest {
    
        @Autowired
        private MockMvc mvc;
    
        @Test
        public void testGirlList() throws Exception {
            mvc.perform(MockMvcRequestBuilders.get("/girls"))  //请求方法方式和请求名称
                    .andExpect(MockMvcResultMatchers.status().isOk());  //请求返回状态码比对
        }
    }

    第四步:运行该测试类

    测试成功:

    注:修改GirlControllerTest /girls 改为 /girlss 路径不存在

    测试结果:

    注:修改GirlControllerTest  新增期望返回内容判断:.andExpect(MockMvcResultMatchers.content().string("abc"));

    测试结果:

     

    注:在终端对项目进行打包时 会自动进行单元测试

    测试成功的结果:

    注:跳过单元测试直接打包命令: mvn clean package -Dmaven.test.skip=true

    测试结果:

  • 相关阅读:
    Django踩坑记录2
    Django踩坑记录1
    pycharm2020激活破解和汉化
    python 读写文件
    python截取视频制作动态表情包+文字
    jenkins运行错误解决办法
    python读取xlsx文件
    python读取csv文件
    python+requests之接口测试
    win10安装jenkins忘记密码的解决方法
  • 原文地址:https://www.cnblogs.com/liuxiaoming123/p/7803112.html
Copyright © 2011-2022 走看看