1、加入Junit4及SpringJUnit4支持

  1. <!-- junit -->
  2. <dependency>
  3. <groupId>junit</groupId>
  4. <artifactId>junit</artifactId>
  5. <scope>test</scope>
  6. </dependency>
  7. <!-- spring-test -->
  8. <dependency>
  9. <groupId>org.springframework</groupId>
  10. <artifactId>spring-test</artifactId>
  11. </dependency>

2、创建测试类

(wins:右键菜单------->转到(G)------->测试(E) 可快捷在test包,相同目录下建立相应的测试类)

(ios:IntelliJ IDEA提供了一个快捷操作Cmd + Shift + T作为类和测试之间的导航。同时允许用户在那里创建一个测试类。)

3、生成的代码如下:

  1. package net.wll.web.service.impl;
  2. import org.junit.Test;
  3. import static org.junit.Assert.*;
  4. public class DAreasServiceImplTest {
  5. @Test   // 注:我自己的和这里有一点小差别,即前面没有test而是和待测试方法一模一样
  6. public void testSelectSubDistricts() throws Exception {
  7. }
  8. @Test
  9. public void testSelectSubDistricts0() throws Exception {
  10. }
  11. }

4、增加Spring-test支持

    1. package net.xuele.activity.service.impl;
    2. import net.xuele.activity.domain.DAreas;
    3. import net.xuele.activity.service.DAreasService;
    4. import org.junit.Test;
    5. import org.junit.runner.RunWith;
    6. import org.springframework.test.context.ContextConfiguration;
    7. import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
    8. import javax.annotation.Resource;
    9. import java.util.List;
    10. @RunWith(SpringJUnit4ClassRunner.class)
    11. /** 注入相关的配置文件:可以写入多个配置文件 **/
    12. @ContextConfiguration(locations={"classpath:META-INF/spring/application-services.xml",
    13. "classpath:META-INF/spring/applicationContext-persist.xml"
    14. })
    15. public class DAreasServiceImplTest {
    16. @Resource
    17. private DAreasService dAreasService;
    18. @Test
    19. public void testSelectSubDistricts0() throws Exception {
    20. List<DAreas> dAreases =  this.dAreasService.selectSubDistricts0();
    21. System.out.println(dAreases.size());
    22. }
    23. }
  1. 转载自:https://blog.csdn.net/xcwll_sina/article/details/49277645

idea intellij对Spring进行单元测试的更多相关文章

  1. Spring之单元测试

    引言 是否在程序运行时使用单元测试是衡量一个程序员素质的一个重要指标.使用单元测试既可以让我检查程序逻辑的正确性还可以让我们减少程序测试的BUG,便于调试可以提高我们写程序的效率.以前我们做单元测试的 ...

  2. Spring Boot单元测试(Mock)

    Spring Boot单元测试(Mock) Java个人学习心得 2017-08-12 16:07 Mock 单元测试的重要性就不多说了,我这边的工程一般都是Spring Boot+Mybatis(详 ...

  3. 框架:Intellij搭建Spring框架

    第二章.Intellij搭建Spring框架 前提条件:jdk.jre已经安装完成 方法一.intellij下载jar 附:自带的jar的版本为4.3[2018/11/22] 第一步:选择File&g ...

  4. Intellij IDEA Spring Boot 项目Debug模式启动缓慢问题

    问题 Intellij IDEA Spring Boot 项目Debug模式启动缓慢 环境 os: windows10 idea :2018.1 解决方法 去除所有断点就正常了,很诡异,原因未知.

  5. 【Spring Boot&&Spring Cloud系列】使用Intellij构建Spring Boot和Mybatis项目

    一.创建项目 1.File->New->Project->spring initializer 2.勾选Web SQL Template Engines 3.项目生成之后,点击add ...

  6. Spring MVC学习总结(1)——Spring MVC单元测试

    关于spring MVC单元测试常规的方法则是启动WEB服务器,测试出错 ,停掉WEB 改代码,重启WEB,测试,大量的时间都浪费在WEB服务器的启动上,下面介绍个实用的方法,spring MVC单元 ...

  7. IntelliJ IDEA使用JUnit单元测试

    转载:https://www.cnblogs.com/huaxingtianxia/p/5563111.html 前言 单元测试的基本使用 一.环境配置 使用idea IDE 进行单元测试,首先需要安 ...

  8. Spring Boot 单元测试示例

    Spring 框架提供了一个专门的测试模块(spring-test),用于应用程序的单元测试. 在 Spring Boot 中,你可以通过spring-boot-starter-test启动器快速开启 ...

  9. spring的单元测试

    如果spring 4.3.18这个版本的spring要使用junit,需要使用junit的junit-4.12之上的版本.使用这个版本junit的时 候需要引入hamcrest-all的jar包.之前 ...

随机推荐

  1. [leetcode]122. Best Time to Buy and Sell Stock II 最佳炒股时机之二

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  2. jmeter操作数据库,分布式,在Linux上运行

    jmeter操作数据库: 1.在测试计划中导入数据库jar包 2.添加链接数据库信息 3.mysql:jdc:mysql://192.168.1.116:3307/bugfree?allowMulti ...

  3. IntelliJ IDEA return null with ClassLoader.getSystemResourceAsStream(“configFilename”));

    参考https://stackoverflow.com/questions/49470053/intellij-idea-return-null-with-classloader-getsystemr ...

  4. ContenteProvider

    以前只写过程序中添加背景音乐,在程序一开始就运行音乐,当程序结束后音乐也随即停止.遇到这样的功能,我们一般是通过系统提供的ContentProvider来实现的,系统对于常用的数据也给开发者提供了方便 ...

  5. JS-事件心得

    写在前面的话:就我目前的水平来看,这两种方法不能一起使用,用on添加的事件removeEventListener()没办法删除,反之一样 注册事件的两种方式: on+事件名称 addEventList ...

  6. python自学开始

    95年工科女一枚 java工程师算不上,只能说从事java开发相关的工作,由于对Python有着极其浓厚的兴趣,一周时间了解大概之后,决定从今天开始见缝插针自学Python,为了防止本人三天打鱼两天晒 ...

  7. hdu 5691(状压DP) Sitting in Line

    题目http://acm.hdu.edu.cn/showproblem.php?pid=5691 状态DP,dp[i][j],i 表示的是一种状态,这个状态指的是当前这个数取或不取,j表示的是以第j个 ...

  8. hdu 1541 (基本树状数组) Stars

    题目http://acm.hdu.edu.cn/showproblem.php?pid=1541 n个星星的坐标,问在某个点左边(横坐标和纵坐标不大于该点)的点的个数有多少个,输出n行,每行有一个数字 ...

  9. java8 数据结构的改变(二) 对ConcurrentHashMap影响

    https://www.cnblogs.com/study-everyday/p/6430462.html http://www.importnew.com/22007.html

  10. (O)web缓存

    为什么要用缓存 一般针对静态资源如CSS,JS,图片等使用缓存,原因如下: 请求更快:通过将内容缓存在本地浏览器或距离最近的缓存服务器(如CDN),在不影响网站交互的前提下可以大大加快网站加载速度. ...