1、创建maven项目,修改pom.xml文件

  1. <!--springboot项目依赖的父项目-->
  2. <parent>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter-parent</artifactId>
  5. <version>2.0.0.RELEASE</version>
  6. </parent>
  7.  
  8. <dependencies>
  9. <!--注入springboot启动器-->
  10. <dependency>
  11. <groupId>org.springframework.boot</groupId>
  12. <artifactId>spring-boot-starter-web</artifactId>
  13. </dependency>
  14.  
  15. <!--添加junit环境的jar包-->
  16. <dependency>
  17. <groupId>org.springframework.boot</groupId>
  18. <artifactId>spring-boot-starter-test</artifactId>
  19. </dependency>
  20. </dependencies>

2、dao层代码

  1. package com.bjsxt.dao;
  2.  
  3. import org.springframework.stereotype.Repository;
  4.  
  5. /**
  6. * Created by Administrator on 2019/2/14.
  7. */
  8. @Repository
  9. public class UserDaoImpl {
  10.  
  11. public void saveUser(){
  12. System.out.print("insert into user...");
  13. }
  14. }

3、service层代码

  1. package com.bjsxt.service;
  2.  
  3. import com.bjsxt.dao.UserDaoImpl;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.stereotype.Service;
  6.  
  7. /**
  8. * Created by Administrator on 2019/2/14.
  9. */
  10. @Service
  11. public class UserServiceImpl {
  12.  
  13. @Autowired
  14. private UserDaoImpl userDaoImpl;
  15.  
  16. public void saveUser(){
  17. userDaoImpl.saveUser();
  18. }
  19.  
  20. }

4、编写启动类

  1. package com.bjsxt;
  2.  
  3. import org.springframework.boot.SpringApplication;
  4. import org.springframework.boot.autoconfigure.SpringBootApplication;
  5.  
  6. /**
  7. * Created by Administrator on 2019/2/14.
  8. */
  9. @SpringBootApplication
  10. public class App {
  11.  
  12. public static void main(String[] args){
  13. SpringApplication.run(App.class,args);
  14. }
  15. }

5、编写测试文件,运行testSaveUser方法即可

  1. package com.bjsxt.test;
  2.  
  3. /**
  4. * Created by Administrator on 2019/2/14.
  5. */
  6.  
  7. import com.bjsxt.App;
  8. import com.bjsxt.service.UserServiceImpl;
  9. import org.junit.Test;
  10. import org.junit.runner.RunWith;
  11. import org.springframework.beans.factory.annotation.Autowired;
  12. import org.springframework.boot.test.context.SpringBootTest;
  13. import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
  14.  
  15. /**
  16. * SpringBoot 测试类
  17. *
  18. * @RunWith:启动器 SpringJUnit4ClassRunner.class:让 junit 与 spring 环境进行整合
  19. * @SpringBootTest(classes={App.class}) 1, 当前类为 springBoot 的测试类
  20. * @SpringBootTest(classes={App.class}) 2, 加载 SpringBoot 启动类。启动springBoot
  21. * junit 与 spring 整合@Contextconfiguartion("classpath:applicationContext.xml")
  22. */
  23. @RunWith(SpringJUnit4ClassRunner.class)
  24. @SpringBootTest(classes = {App.class})
  25. public class UserServiceTest {
  26.  
  27. @Autowired
  28. private UserServiceImpl userServiceImpl;
  29.  
  30. @Test
  31. public void testSaveUser(){
  32. userServiceImpl.saveUser();
  33. }
  34.  
  35. }

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. openlayers 3 读取展示shp文件

    简单的思路如下: 1.在arcgis中获得shp文件 2.将其转成geojson文件 3.用openlayers进行展示 第一步直接省略 第二步这里推荐一个shp转geojson很方便的工具网站htt ...

  2. Introduction of Servlet Filter(了解Servlet之Filter)

    API文档中介绍了public Interface Filter(公共接口过滤器) Servlet API文档中是这样介绍的: ‘A filter is an object that performs ...

  3. VS2012 无法启动 IIS Express Web

    用记事本打开项目的.csproj文件,定位到<WebProjectProperties>,把关于IIS的配置<DevelopmentServerPort>.<Develo ...

  4. scss-#{}插值

    一般我们定义的变量都为属性值,可直接使用,但是如果变量作为属性或在某些特殊情况下则必须要以 #{$variables} 形式使用. 例如:scss代码 $borderDirection: top !d ...

  5. ubuntu GITLAB完全导入SVN(提交历史,用户)项目

    从SVN导入到GITLAB目前没有直接的方案,通常需要通过GIT转换:SVN –>GIT –>GITLAB.通过这种方式,将SVN的提交历史,用户信息一并导入到gitlab 注:本文只适用 ...

  6. Eclipse One Inspector

    net.sf.yari.eclipse.EclipseInspectorViewPart Through the outline of EclipseInspectorViewPart, we can ...

  7. Linux学习笔记之Linux第一课-基本介绍

    Linux简介 Linux内核最初只是由芬兰人李纳斯·托瓦兹(Linus Torvalds)在赫尔辛基大学上学时出于个人爱好而编写的. Linux是一套免费使用和自由传播的类Unix操作系统,是一个基 ...

  8. Laravel 单元测试-模拟认证的用户

    在 Laravel 编写单元测试时经常会遇到需要模拟认证用户的时候,比如新建文章.创建订单等,那么在 Laravel unit test 中如何来实现呢? 官方解决方法 Laravel 的官方文档中的 ...

  9. 【Leetcode】【Easy】Roman to Integer

    Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 t ...

  10. yjh_study_command

    1.show current user in oralce ansower:show user 2.search  name of table  in current user model. answ ...