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单元测试的更多相关文章

  1. SpringBoot学习- 4、整合JWT

    SpringBoot学习足迹 1.Json web token(JWT)是为了网络应用环境间传递声明而执行的一种基于JSON的开发标准(RFC 7519),该token被设计为紧凑且安全的,特别适用于 ...

  2. SpringBoot学习- 3、整合MyBatis

    SpringBoot学习足迹 1.下载安装一个Mysql数据库及管理工具,同类工具很多,随便找一个都可以,我在windows下做测试项目习惯使用的是haosql 它内部集成了MySql-Front管理 ...

  3. SpringBoot学习- 5、整合Redis

    SpringBoot学习足迹 SpringBoot项目中访问Redis主要有两种方式:JedisPool和RedisTemplate,本文使用JedisPool 1.pom.xml添加dependen ...

  4. spring框架学习(三)junit单元测试

    spring框架学习(三)junit单元测试 单元测试不是头一次听说了,但只是听说从来没有用过.一个模块怎么测试呢,是不是得专门为一单元写一个测试程序,然后将测试单元代码拿过来测试? 我是这么想的.学 ...

  5. Spring的AOP开发入门,Spring整合Junit单元测试(基于ASpectJ的XML方式)

    参考自 https://www.cnblogs.com/ltfxy/p/9882430.html 创建web项目,引入jar包 除了基本的6个Spring开发的jar包外,还要引入aop开发相关的四个 ...

  6. 十二 Spring的AOP开发入门,整合Junit单元测试(AspectJ的XML方式)

    创建web项目,引入jar包 引入Spring配置文件

  7. SpringBoot学习- 8、整合Shiro

    SpringBoot学习足迹 Shiro是什么,引自百度百科:Apache Shiro是一个强大且易用的Java安全框架,执行身份验证.授权.密码和会话管理.使用Shiro的易于理解的API,您可以快 ...

  8. Spring框架中整合JUnit单元测试的方法

    一. 步骤: 1. 拷贝jar包: 1. JUnit-4.9.jar和spring-test-4.2.4.RELEASE.jar ; 2. 替换原来的main函数: 1. 在测试类上使用注解方式替换: ...

  9. SpringBoot: 16.整合junit单元测试(转)

    1.创建maven项目,修改pom.xml文件 <!--springboot项目依赖的父项目--> <parent> <groupId>org.springfram ...

随机推荐

  1. C#简单实现读取txt文本文件并分页存储到数组

    最近做一个VR项目,需要把某个中草药的介绍信息分页显示到unity场景里然后用VR手柄切换信息. unity的脚本是c#,就先在本地写了个代码测试了一下,利用控制台测试输出,到时候拷贝函数过去再结合交 ...

  2. 运用Edraw为WPF应用程序嵌入Office文档的方法总结

    具体描述了运用Edraw Office Viewer Component为WPF应用长须嵌入MS Word,Excel以及Power Point的方法. 打开Visual Studio,并创建一个新的 ...

  3. C++基础--malloc和new的区别

    (1)malloc在C和C++中都可以使用,用来申请一段内存:申请的内存一定要用free释放,然后把指针置为null: new只能在C++中使用,用于动态内存分配:new的对象要delete掉: (2 ...

  4. Ubuntu上使用vnc服务

    http://www.aichengxu.com/linux/8479752.htm 1. sudo apt-get install x11vnc 2. sudo x11vnc -storepassw ...

  5. ant用法

    下载ant包,解压版,解压到某目录后配置环境变量即可,这里不在赘述.配置成功cmd ant-version,显示ant版本即OK.           ant是Apache的跨平台的构建工具,他可以实 ...

  6. 转:C函数调用理解

    1 空函数 int main() { 00411360 push ebp ;压入ebp 00411361 mov ebp,esp ;ebp = esp,保留esp,待函数调用完再恢复,因为函数调用中肯 ...

  7. oracle查询时间

    oracle查询和时间有关的命令: 方法一:select * from dual where time between to_date('2012-06-18 00:00:00','yyyy-mm-d ...

  8. java maven项目 导入jar包注意

    1.将jar 包放到lib目录下 2.build path 3.点击maven主项目右键选择properties 4.选择myeclipse 下的第一个选项 5.点击add   选择archives ...

  9. SourceTree Win10 安装不成功解决过程记录

    简介 SourceTree 是一款拥有可视化界面的项目版本控制软件,适用于git项目管理,同时它集成了 git flow 工作流程,对于不熟悉 git 命令的初学者来说,可以通过 SourceTree ...

  10. PHP腾讯与百度坐标转换

    function coordinate_switch($a,$b){//百度转腾讯坐标转换 $a = Latitude , $b = Longitude $x = (double)$b - 0.006 ...