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. 如何给PDF设置全屏动画

    PPT文件可以播放全屏,并且可以实现飞入.分割.闪烁等动画模式播放.那么PDF文件可以吗?我们想要给PDF文件加入动画效果应该怎么做呢,也有很多的小伙伴不知道该怎么把PDF文件切换为全屏动画模式想要知 ...

  2. Hdu 1022 Train Problem I 栈

    Train Problem I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  3. spark MLlib BasicStatistics 统计学基础

    一, jar依赖,jsc创建. package ML.BasicStatistics; import com.google.common.collect.Lists; import org.apach ...

  4. arraylist 为什么 删除元素时要使用迭代器而不能使用遍历

    因为你要是遍历了,arraylist 的长度就变了,容易数组越界和下标问题 public class Test {     public static void main(String[] args) ...

  5. 按比例缩放DIV

    class ResponsiveDiv extends React.Component { constructor(props) { super(props); this.state = { widt ...

  6. Django——在线教育项目总结

    项目简介 在线教育平台 软件依赖: WEB框架:Django(1.11.7).Django REST framework 前端框架:Vue(2.5.16) 数据库: MySql.redis 支付平台: ...

  7. HtmlHelper使用示例

    在使用Razor时,有时想要在页面内知道对象的完整信息,或服务器的详细信息,可以通过HtmlHelper. 具体使用示例如下: <div>测试一: @ServerInfo.GetHtml( ...

  8. js异步

    1.定时器都是异步操作 2.时间绑定都是异步操作 3.AJAX中一般我们采用异步操作 4.回调函数可以理解为异步操作 异步:指的是每一个任务有一个或多个回调函数,前一个任务结束后,不是执行后一个任务, ...

  9. 使用gitflow流程完成功能时报错

    报错 fatal: could not read Username for 'https://github.com': ······ 原因 使用https方式的时候 在https url 里面没有用户 ...

  10. h5 canvas与SVG的比较

    画布 什么是canvas? HTML5的canvas标签使用JavaScript可以在网页上绘制图像,画布为一个矩形. 画布本身没有绘制能力,只能通过脚本来绘制. 画布例子: <canvas i ...