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. day 12 内置函数,装饰器,递归函数

    内置函数 内置函数:python给咱们提供了一些他认为你会经常用到的函数,68种      内置函数     abs() dict() help() min() setattr() all()  di ...

  2. shell中颜色的设置

    linux启动后环境变量加载的顺序为:etc/profile → /etc/profile.d/*.sh → ~/.bash_profile → ~/.bashrc → [/etc/bashrc] 想 ...

  3. linux命令学习之:cp

    cp命令用来复制文件或者目录,是Linux系统中最常用的命令之一. 如同时指定两个以上的文件或目录,且最后的目的地是一个已经存在的目录,则它会把前面指定的所有文件或目录复制到此目录中.若同时指定多个文 ...

  4. 在做excel导出时如何将workbook直接写在输出流中

    参考网址 https://blog.csdn.net/u011109420/article/details/51330677 https://blog.csdn.net/u012116457/arti ...

  5. Ado.net简单快捷帮助类

    using System; using System.Collections.Generic; using System.Data; using System.Data.SqlClient; usin ...

  6. Android TV开发 焦点控制

    转载:http://www.eoeandroid.com/thread-264177-1-1.html 最近在做一款基于Android的互联网电视客户端,开发与phone/pad差不多,但是有一个值得 ...

  7. org.springframework.web.util.Log4jWebConfigurer

    org.springframework.web.util.Log4jWebConfigurer @Deprecated Deprecated. as of Spring 4.2.1, in favor ...

  8. VSCode一直弹框错误Linter pylint is not installed

    确保已经安装Python编译环境 点击下图位置(这个是我已经安装过后的文字,原本显示“搜索Python”字样) 点击后显示如下,点击安装 然后出现一大坨命令 最终出现“Successfully ins ...

  9. IDEA配置maven,jdk,编码

    1.电脑已解压maven,修改好settings.xml文件,本地仓库,下载远程镜像 2.

  10. Sublime Text3 常用快捷键必看

    Sublime Text3 常用快捷键必看  https://blog.csdn.net/md1688/article/details/53043525