首页 > 其他分享 >【转载】Springboot2.x单元测试

【转载】Springboot2.x单元测试

时间:2023-12-14 11:44:35浏览次数:30  
标签:单元测试 springframework jupiter Springboot2 import test org 转载 junit

参考

注意

  1. 新版spring-boot-starter-test不再集成junit,而是junit-jupiter,无需 @RunWith(本文章使用的版本 2.3.12.RELEASE)
  2. vs code 插件由于安装了无法提供更准确的插件名称,应该罗列的这几个就是。

环境

环境 版本 操作
windows 10
vs code 1.84.2
Spring Boot Extension Pack v0.2.1 vscode插件
Extension Pack for Java v0.25.15 vscode插件
JDK 11
Springboot 2.3.12.RELEASE

正文

junit5

仅仅一部分示例,具体请参考官网

package com.example.demo.service;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import javax.annotation.Resource;

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import com.example.demo.entity.UserEntity;

@SpringBootTest
public class UserServiceTest {

    @Resource
    UserService userService;

    @BeforeAll
    static void beforeAll(){
        System.out.println("==beforeAll==");
    }

    @AfterAll
    static void afterAll(){
        System.out.println("==AfterAll==");
    }

    @BeforeEach
    void beforeEach(){
        System.out.println("==BeforeEach==");
    }

    @AfterEach
    void afterEach(){
        System.out.println("==AfterEach==");
    }

    @Test
    void testAdd() {
        UserEntity userEntity = userService.add();
        // 相等
        assertEquals(userEntity.getId(), 1);
        // 不相等
        assertNotEquals(2, 1);
        // 对象是为空
        assertNull(null);
        // 断言为真
        assertTrue(true);
    }
}

image

控制器测试 WebMvcTest

仅仅一部分简单示例。

  1. src\main\java\com\example\demo\controller\IndexController.java

package com.example.demo.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


import org.springframework.web.bind.annotation.GetMapping;


@RestController
@RequestMapping("/")
public class IndexController {

    @GetMapping("test")
    public String test() {
        return "hello";
    }
    
}

  1. src\test\java\com\example\demo\controller\IndexControllerTest.java
package com.example.demo.controller;

import static org.junit.jupiter.api.Assertions.assertEquals;

import org.assertj.core.data.Index;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;

// @SpringBootTest
// 告诉 JUnit 5 启用 Spring 支持
@ExtendWith(SpringExtension.class)
@WebMvcTest(controllers = IndexController.class)
public class IndexControllerTest {

    /**
     * 加上 @WebMvcTest(controllers = IndexController.class) 就可以自动注入,不加就要在 @BeforeEach 手动注入。
     */
    @Autowired
    private MockMvc mvc;

    // @BeforeEach
    // public void setUp() throws Exception {
    //     mvc = MockMvcBuilders.standaloneSetup(new IndexController()).build();
    //     System.out.println("初始化mock模块");
    // }

    @Test
    void testTest() throws Exception {
        String responseString = mvc.perform(
                                        MockMvcRequestBuilders.get("/test")
                                        .accept(MediaType.APPLICATION_JSON)
                                    )
                                    .andExpect(MockMvcResultMatchers.status().isOk())    //返回的状态是200
                                    .andReturn().getResponse().getContentAsString();   //将相应的数据转换为字符串;
        System.out.println("获取结果为:" + responseString);
        assertEquals(responseString, "hello");
    }

}

标签:单元测试,springframework,jupiter,Springboot2,import,test,org,转载,junit
From: https://www.cnblogs.com/xiaqiuchu/p/17900751.html

相关文章

  • 360N6线刷参考,最新方法(转载)
    转载自:360N6线刷参考,最新方法-思考的菜鸟-博客园(cnblogs.com)360n6线刷参考线刷包校验服务器停服后的线刷方法大部分参考我写的“360n7root参考”导入的文件可能显示不全,放一个word版的教程在这里:https://files.cnblogs.com/files/twpone/360N6%E7%BA%BF%E5%88%B7%E5%......
  • 【转载】SpringBoot2.x使用Assert校验(非单元测试)
    参考https://blog.csdn.net/yangshangwei/article/details/123105926环境环境版本操作windows10JDK11Springboot2.3.12.RELEASE注意引入的包为importorg.springframework.util.Assert;介绍对象和类型断言函数说明notNull()假设对......
  • js实时显示当前时间(转载)
    <!DOCTYPEhtml><htmlxmlns="http://www.w3.org/1999/xhtml"><head><metahttp-equiv="Content-Type"content="text/html;charset=utf-8"/><title>显示时间</title></head><body&g......
  • SpringBoot2
    概述SpringBoot是整合Spring技术栈的一站式框架。SpringBoot是简化Spring技术栈的快速开发脚手架。优点创建独立Spring应用内嵌web服务器自动starter依赖,简化构建配置自动配置Spring以及第三方功能提供生产级别的监控、健康检查及外部化配置无代码生成、无需编写XML入......
  • 【转载】QT学习之路(一)ubuntu 18.04的Qt Creator在线安装
    https://blog.csdn.net/qq_26849933/article/details/127115102前言Qt是嵌入式开发的必备工具之一,在Linux下安装尤其重要。Qt是C++的一个库,或者说是开发框架,里面集成了一些库函数,提高开发效率。QtCreator是一个IDE,就是一个平台,一个开发环境,类似的比如说VS,也可以进行Qt开发,当......
  • 【转载】Springboot2.x接收参数的多种方式
    参考https://blog.csdn.net/suki_rong/article/details/80445880https://zhuanlan.zhihu.com/p/34597391https://juejin.cn/post/6922469125033820168环境环境版本操作windows10JDK11Springboot2.3.12.RELEASE正文packagecom.example.demo.co......
  • vitest&@vue/test-utils组件单元测试
    依赖"vitest":"0.34.6","@vue/test-utils":"2.4.3","axios-mock-adapter":"^1.22.0",示例import{mount}from"@vue/test-utils";import{test,vi}from"vitest";import{gl......
  • 手摸手入门Springboot2.7集成Swagger2.9.2
    环境介绍技术栈springboot+mybatis-plus+mysql+oracle+Swagger软件版本mysql8IDEAIntelliJIDEA2022.2.1JDK1.8SpringBoot2.7.13mybatis-plus3.5.3.2REST软件架构风格REST即表述性状态传递(英文:RepresentationalStateTransfer,简称REST,中文:表示层状态转移)是RoyFielding博士在20......
  • 特斯拉第三方应用开发指南(一)[转载]
    https://www.cnblogs.com/w1570631036/p/17893398.html特斯拉第三方应用开发指南(一)作者:Zephery个人网址:http://www.wenzhihuai.com本文为作者原创,转载请注明出处:https://www.cnblogs.com/w1570631036/p/17893398.html目录一、特斯拉应用申请1.1创建Tesla账户1.2提交访......
  • (转载-自用)前端面试题
    一、CSS  1.说一下CSS的盒模型。    在HTML页面中的所有元素都可以看成是一个盒子    盒子的组成:内容content、内边距padding、边框border、外边距margin    盒模型的类型:      标准盒模型        margin+borde......