SpringBoot学习16:springboot整合junit单元测试
1、创建maven项目,修改pom.xml文件
<!--springboot项目依赖的父项目-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.RELEASE</version>
</parent> <dependencies>
<!--注入springboot启动器-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <!--添加junit环境的jar包-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
</dependencies>
2、dao层代码
package com.bjsxt.dao; import org.springframework.stereotype.Repository; /**
* Created by Administrator on 2019/2/14.
*/
@Repository
public class UserDaoImpl { public void saveUser(){
System.out.print("insert into user...");
}
}
3、service层代码
package com.bjsxt.service; import com.bjsxt.dao.UserDaoImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; /**
* Created by Administrator on 2019/2/14.
*/
@Service
public class UserServiceImpl { @Autowired
private UserDaoImpl userDaoImpl; public void saveUser(){
userDaoImpl.saveUser();
} }
4、编写启动类
package com.bjsxt; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; /**
* Created by Administrator on 2019/2/14.
*/
@SpringBootApplication
public class App { public static void main(String[] args){
SpringApplication.run(App.class,args);
}
}
5、编写测试文件,运行testSaveUser方法即可
package com.bjsxt.test; /**
* Created by Administrator on 2019/2/14.
*/ import com.bjsxt.App;
import com.bjsxt.service.UserServiceImpl;
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.SpringJUnit4ClassRunner; /**
* SpringBoot 测试类
*
* @RunWith:启动器 SpringJUnit4ClassRunner.class:让 junit 与 spring 环境进行整合
* @SpringBootTest(classes={App.class}) 1, 当前类为 springBoot 的测试类
* @SpringBootTest(classes={App.class}) 2, 加载 SpringBoot 启动类。启动springBoot
* junit 与 spring 整合@Contextconfiguartion("classpath:applicationContext.xml")
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = {App.class})
public class UserServiceTest { @Autowired
private UserServiceImpl userServiceImpl; @Test
public void testSaveUser(){
userServiceImpl.saveUser();
} }
6、目录结构

SpringBoot学习16:springboot整合junit单元测试的更多相关文章
- SpringBoot学习- 4、整合JWT
SpringBoot学习足迹 1.Json web token(JWT)是为了网络应用环境间传递声明而执行的一种基于JSON的开发标准(RFC 7519),该token被设计为紧凑且安全的,特别适用于 ...
- SpringBoot学习- 3、整合MyBatis
SpringBoot学习足迹 1.下载安装一个Mysql数据库及管理工具,同类工具很多,随便找一个都可以,我在windows下做测试项目习惯使用的是haosql 它内部集成了MySql-Front管理 ...
- SpringBoot学习- 5、整合Redis
SpringBoot学习足迹 SpringBoot项目中访问Redis主要有两种方式:JedisPool和RedisTemplate,本文使用JedisPool 1.pom.xml添加dependen ...
- spring框架学习(三)junit单元测试
spring框架学习(三)junit单元测试 单元测试不是头一次听说了,但只是听说从来没有用过.一个模块怎么测试呢,是不是得专门为一单元写一个测试程序,然后将测试单元代码拿过来测试? 我是这么想的.学 ...
- Spring的AOP开发入门,Spring整合Junit单元测试(基于ASpectJ的XML方式)
参考自 https://www.cnblogs.com/ltfxy/p/9882430.html 创建web项目,引入jar包 除了基本的6个Spring开发的jar包外,还要引入aop开发相关的四个 ...
- 十二 Spring的AOP开发入门,整合Junit单元测试(AspectJ的XML方式)
创建web项目,引入jar包 引入Spring配置文件
- SpringBoot学习- 8、整合Shiro
SpringBoot学习足迹 Shiro是什么,引自百度百科:Apache Shiro是一个强大且易用的Java安全框架,执行身份验证.授权.密码和会话管理.使用Shiro的易于理解的API,您可以快 ...
- Spring框架中整合JUnit单元测试的方法
一. 步骤: 1. 拷贝jar包: 1. JUnit-4.9.jar和spring-test-4.2.4.RELEASE.jar ; 2. 替换原来的main函数: 1. 在测试类上使用注解方式替换: ...
- SpringBoot: 16.整合junit单元测试(转)
1.创建maven项目,修改pom.xml文件 <!--springboot项目依赖的父项目--> <parent> <groupId>org.springfram ...
随机推荐
- 通过js控制层的动态隐藏
<style type="text/css"> #dv1{ width:1000px; height:1000px; overflow:hidden; display: ...
- pv-err-watch
# pv-err-watch 这是一个前端监控的小工具,提供了多种信息的查询 ## 快速开始 安装`npm install pv-err-watch -S` ```js import errorWat ...
- css display:table圣杯布局
圣杯布局指的是一个网页由页眉,3等高列(2个固定侧栏和中心内容主体)和贴在页面底部的页脚组成. 主要思路是对整个容器使用地上diaplay:table 的css规则,然后分别对页眉页脚使用displa ...
- Java入门到精通——框架篇之Hadoop概述
一.Hadoop来历 Hadoop的思想来源于Google在做搜索引擎的时候出现一个很大的问题就是这么多网页我如何才能以最快的速度来搜索到,由于这个问题Google发明了倒排索引算法,通过加入了Map ...
- LDAP入门与OpenLDAP使用配置
LDAP入门与OpenLDAP使用配置 1.LDAP简介 LDAP(轻量级目录访问协议,Lightweight Directory Access Protocol)是实现提供被称为目录服务的信息服务. ...
- Multidex (方法数超过限制的处理)
报错 : Conversion to Dalvik format failed: Unable to execute dex: method ID not in [0, 0xffff]: 65536 ...
- Qtl和JS、HTML通信/交互
http://www.cnblogs.com/sigma0/p/7346727.html Qt的QWebChannel和JS.HTML通信/交互驱动百度地图 0 前言 我一个研究嵌入式的,不知道怎么就 ...
- hibernate 性能优化之 1+N 问题
1. 注意 session.clear()的运用,尤其在不断分页查询的时候 a) 在一个大集合中进行遍历,遍历 msg,去除其中的含有敏感字样的对象 b) 另外一种形式的内存泄漏 面试题:Java 有 ...
- java中加密的方式概述
加密是用一种特殊的算法改变原有的数据,使未经授权的用户即使获得了已经加密的信息,但不知其解密的方法,仍然无法了解信息的内容. 大体上分为单向加密和双向加密,双向加密又可分为对称加密和非对称加密 ...
- python3的学习经验
网上资料非常多,颇有些“乱花渐欲迷人眼”的意味,个人看了不少,拖之前从事前端的福,发现廖雪峰大神的网站里有.学了2天之后发觉获益良多,网址:https://www.liaoxuefeng.com/wi ...