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 ...
随机推荐
- 关于IQueryable和IEnumerable
园里对这两个已经有很多文章作了深入的介绍,我总结些,当成笔记用. 一.具体判断用哪个上,如果是运行在本地内存中的,用IEnumerable,枚举该对象时,会立即反应查询结果. 如果是远程数据源,比如数 ...
- 探寻hashmap
Hashmap源码 1. 构造器: a)获得默认数组大小:1>>4 :16 b) 获得负载因子:0.75:衡量hashmap的空间使用程度 i.过大:使用空间更加充分,但是查找效率变低, ...
- 通过tomcat shutdown port关闭tomcat
在tomcat server.xml配置文件中,有个配置项 <Server port="8005" shutdown="SHUTDOWN"> 通过向 ...
- ssh-keygen使用
ssh-keygen 作用就是验证主机和用户公钥加密 值得注意的是passphrase选项询问 是对自身密钥的保护,因为在ssh通信前,密钥是不受保护的,如果填来的话通常会使用aes256-cbc的对 ...
- Drupal中hook_theme函数用法
在开发的时候不免要使用到drupal theme 定义.举个简单的例子: 复制代码 代码如下: <?phpfunction modulename_theme() { //开始定义自己的theme ...
- Stage2--Python的数据类型
说在前面: Stage1-Stage4简单介绍一下Python语法,Stage5开始用python实现一些实际应用,语法的东西到处可以查看到,学习一门程序语言的最终目的是应用,而不是学习语法,语法本事 ...
- ppt写作的注意事项
PPT推荐字体及大小: 宋体严谨,适合正文,显示最清晰 黑体庄重,适合标题,或者强调区 隶书楷体,艺术性强,不适合投影 如果通过文字排版突出重点:加粗.加大字号.变色 PPT文字太多怎么办? 1.抽象 ...
- SharePoint Designer - Workflow
另一篇文章 SharePoint 2013 - Designer Workflow 1. Set field in current item : 不要连续多次使用,否则在发布时会出现unexpecte ...
- 【Eclipse】在Eclipse上安装Spket
转自:https://www.cnblogs.com/HDK2016/p/7099383.html 1,Spket是什么? Spket是一种编辑javaScript和XML代码的工具,可以用他自己的 ...
- 浅谈SQL Server中的事务日志(五)----日志在高可用和灾难恢复中的作用
简介 日志的作用是保证持久性和数据一致性,通过日志可以实现数据的Undo与Redo,因此通过日志,SQL Server不仅仅可以实现灾难恢复,还可以通过日志的Redo来实现高可用性.本篇文章主要讲述日 ...