SpringBoot

一、Service层Junit单元测试

需要的jar包

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

Springboot 1.3的版本与1.4的版本稍有不同

1.3及以下版本

package com.suning.epp.fmasosweb.service.impl;

import com.suning.epp.fmasosweb.FmasosWebApplication;
import com.suning.epp.fmasosweb.result.RankGenreResult;
import com.suning.epp.fmasosweb.service.intf.CommentService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration; import java.util.HashMap;
import java.util.List;
import java.util.Map; /**
* 〈一句话功能简述〉
* 〈功能详细描述〉
*
* @author 17090889
* @see [相关类/方法](可选)
* @since [产品/模块版本] (可选)
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = FmasosWebApplication.class)
@WebAppConfiguration
public class CommentServiceImplTest { @Autowired
private CommentService commentService; @Test
public void queryAppRankGenreResultTest() {
Map<String, String> param = new HashMap<>();
List<RankGenreResult> rankGenreResultList = commentService.queryAppRankGenreResult(param);
System.out.println(rankGenreResultList);
} }

1.4及以上版本

@SpringApplicationConfiguration 注解标记为过时了

提供了注解@SpringBootTest

使用SpringRunner 替代 SpringJUnit4ClassRunner

package com.suning.epp.fmasosweb.service.impl;

import com.suning.epp.fmasosweb.result.RankGenreResult;
import com.suning.epp.fmasosweb.service.intf.CommentService;
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; import java.util.HashMap;
import java.util.List;
import java.util.Map; /**
* 〈一句话功能简述〉
* 〈功能详细描述〉
*
* @author 17090889
* @see [相关类/方法](可选)
* @since [产品/模块版本] (可选)
*/
@RunWith(SpringRunner.class)
@SpringBootTest
public class CommentServiceImplTest { @Autowired
private CommentService commentService; @Test
public void queryAppRankGenreResultTest() {
Map<String, String> param = new HashMap<>();
List<RankGenreResult> rankGenreResultList = commentService.queryAppRankGenreResult(param);
System.out.println(rankGenreResultList);
} }

二、Controller层Mock测试

1.3及以下版本

package com.suning.epp.fmasosadmin.mapper;

import com.suning.epp.fmasos.FmasosApplication;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.SpringApplicationConfiguration;
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.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.context.WebApplicationContext; /**
* 〈一句话功能简述〉
* 〈功能详细描述〉
*
* @author 17090889
* @see [相关类/方法](可选)
* @since [产品/模块版本] (可选)
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = FmasosApplication.class)
@WebAppConfiguration
@Transactional
public class ProcessorServiceTest { // @Autowired
// @Qualifier("commentProcessorServiceImpl")
// private CommentProcessorService commentProcessorServiceImpl; @Autowired
private WebApplicationContext webApplicationContext; private MockMvc mockMvc; @Before
public void setUp() throws Exception {
//构造MockMvc
mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build(); } @Test
public void spiderRun() throws Exception {
String url = "/comment/spiderRun2";
mockMvc.perform(MockMvcRequestBuilders.get(url));
} }

1.4及以上版本

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;
import static org.junit.Assert.*; @RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
public class CommentControllerTest {
@Autowired
private MockMvc mvc; @Test
public void spiderRun() throws Exception {
mvc.perform(MockMvcRequestBuilders.get("/spiderRun"))
.andExpect(MockMvcResultMatchers.status().isOk());
//.andExpect(MockMvcResultMatchers.content().string("365")); //测试接口返回内容
} }

Spring

  1、需要在test/resources下新建spring配置文件,扫描注入测试需要的所有bean及依赖bean

/**
* @author yangyongjie
* @date 2020/2/26
* @desc
*/
@RunWith(SpringJUnit4ClassRunner.class) // 启动 Spring 对测试类的支持
@ContextConfiguration("classpath:spring-*.xml") // 指定 Spring 配置文件或者配置类的位置,classpath路径为test/resources
public class AutoRenewCheckTaskTest { @Autowired
private AutoRenewCheckTask autoRenewCheckTask; @Test
public void executeTest(){
autoRenewCheckTask.execute();
} }

  2、不在test/resources下新建spring配置文件也可,使用main/resources 下的Spring配置文件,此时需要使用 @ContextConfiguration 注解的 locations 属性指定配置文件在计算机上的绝对路径,如:

@RunWith(SpringJUnit4ClassRunner.class) // 启动 Spring 对测试类的支持
@ContextConfiguration(locations = {"file:D:\\IdeaProjects\\taskModuleOptimize\\bssadmin-task\\src\\main\\webapp\\WEB-INF\\spring\\spring-*.xml"}) // 指定 Spring 配置文件或者配置类的位置
public class AutoRenewCheckTaskTest { @Autowired
private AutoRenewCheckTask autoRenewCheckTask; @Test
public void executeTest(){
autoRenewCheckTask.execute();
} }

end

SpringBoot单元测试的更多相关文章

  1. Springboot单元测试Junit深度实践

    Springboot单元测试Junit深度实践 前言 单元测试的好处估计大家也都知道了,但是大家可以发现在国内IT公司中真正推行单测的很少很少,一些大厂大部分也只是在核心产品推广单测来保障质量,今天这 ...

  2. springmvc,springboot单元测试配置

    1. springmvc单元测试配置 <dependency> <groupId>junit</groupId> <artifactId>junit&l ...

  3. SpringBoot单元测试中的事务和Session

    1.Springboot中使用junit编写单元测试,并且测试结果不影响数据库. 2.

  4. springboot(十二):springboot单元测试、打包部署

    单元测试 1.在pom包中添加spring-boot-starter-test包引用 <dependency> <groupId>org.springframework.boo ...

  5. springboot系列三、springboot 单元测试、配置访问路径、多个配置文件和多环境配置,项目打包发布

    一.单元测试 生成的demo里面包含spring-boot-starter-test :测试模块,包括JUnit.Hamcrest.Mockito,没有的手动加上. <dependency> ...

  6. Springboot单元测试(MockBean||SpyBean)

    转载:https://blog.csdn.net/maiyikai/article/details/78483423 本来要写springboot集成netty实现的,但是想起来单元测试没总结,那就趁 ...

  7. 五、springboot单元测试

    1.为什么要写测试用例 1. 可以避免测试点的遗漏,为了更好的进行测试,可以提高测试效率 2. 可以自动测试,可以在项目打包前进行测试校验 3. 可以及时发现因为修改代码导致新的问题的出现,并及时解决 ...

  8. springBoot单元测试-基础单元测试

    1)在pom文件中加入junit支持 <!-- spring-boot-starter-test 单元测试 --> <dependency> <groupId>or ...

  9. 【使用篇二】SpringBoot单元测试(10)

    SpringCloud单元测试:https://www.cnblogs.com/myitnews/p/11796321.html 1. 创建项目Maven Project,修改pom.xml < ...

随机推荐

  1. jQuery 第六章 jQuery在Ajax应用

    1.本章目标 ajax 2.ajax 异步刷新技术,我们的网页不需要全部刷新,按需实现局部刷新,上线后台的交互 用户体验好 地图,前台验证,表单提交,修改,查询等等 原生的js和ajax packag ...

  2. 关于 win10 系统中 Anaconda3 中修改 Jupyter Notebook 默认启动目录

    目标: 修改Anaconda3中自带的Jupyter Notebook默认启动目录. 步骤:     1.提前创建好你想要启动位置文件夹.     2.寻找配置文件,"jupyter_not ...

  3. 纯css画直角三角形

    所有的三角形,都是通过盒子模型来设定. border(边框)的不同大小来决定 border-width: 边框的宽度 border-style: 边框的样式 border-color: 边框的颜色 1 ...

  4. SQL反模式学习笔记11 限定列的有效值

    目标:限定列的有效值,将一列的有效字段值约束在一个固定的集合中.类似于数据字典. 反模式:在列定义上指定可选值 1. 对某一列定义一个检查约束项,这个约束不允许往列中插入或者更新任何会导致约束失败的值 ...

  5. fillder---工具栏隐藏/显示

    显示隐藏工具栏方法:view---show toolbar

  6. 我是如何用redis做实时订阅推送的

    前阵子开发了公司领劵中心的项目,这个项目是以redis作为关键技术落地的.       先说一下领劵中心的项目吧,这个项目就类似京东app的领劵中心,当然图是截取京东的,公司的就不截了...   其中 ...

  7. web服务-2、四种方法实现并发服务器-多线程,多进程,协程,(单进程-单线程-非堵塞)

    知识点:1.使用多线程,多进程,协程完成web并发服务器 2.单进程-单线程-非堵塞也可以实现并发服务器 1.多进程和协程的代码在下面注释掉的部分,我把三种写在一起了 import socket im ...

  8. Ubuntu中eclipse端口被占

    我的eclipse有时候会闪退,然后再次打开运行HTML程序的时候会提示端口被占,即使之前改过端口也会提示,然后我就在网上搜索解决办法,目前亲测有效的是 输入以下命令: lsof -i:8888 // ...

  9. 树莓派开启samba服务

    安装samba 和 samba-common-bin 启动树莓派以后,在命令行输入: sudo apt-get update sudo apt-get install samba samba-comm ...

  10. 转 Using Async for File Access

    原文:https://msdn.microsoft.com/en-us/library/jj155757.aspx using System; using System.Collections.Gen ...