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. JAVA8 ARRAY、LIST操作 汇【5】)- JAVA8 LAMBDA LIST统计(求和、最大、最小、平均)

    public class Apple { private Integer id; private String name; private BigDecimal money; private Inte ...

  2. [leetcode]347. Top K Frequent Elements K个最常见元素

    Given a non-empty array of integers, return the k most frequent elements. Example 1: Input: nums = [ ...

  3. xampp配置多个监听端口和不同的网站目录

    1.配置Apache文件httpd.conf 打开xampp安装目录下的Apache->conf文件夹下的httpd.conf,用记事本打开 首先在Listen 80端口下添加其他监听端口: L ...

  4. 在windows7中配置ant环境变量

    假设本地以及安装好JDK并且配置好环境变量. 新建/修改系统环境变量 ANT_HOME   D:\develop\apache-ant-1.10.3 PATH       %ANT_HOME%\bin ...

  5. 从django的序列化到rest-framework 序列化

    1.利用Django的view实现返回json数据 from django.views.generic import View from goods.models import Goods class ...

  6. MFC 一个无参线程的CreateThread 使用

    最近想把c#的一个工作中用到的软件用MFC 实现出来, 刚下手 要了解的东西挺多,不但要对c++的语法,大体看一遍. 还要看MFC 内一些窗体,之类的相关的定义,比如cpp ,.h 内的类的定义方式等 ...

  7. find和find_if,value_type

    find算法:返回 [first,end)中第一个值等于value元素的位置 线性复杂度:最多比较次数:元素的总个数 find函数的最后一个参数,必须是string,float,char,double ...

  8. kvm介绍 转载

    KVM 介绍(1):简介及安装 学习 KVM 的系列文章: (1)介绍和安装 (2)CPU 和 内存虚拟化 (3)I/O QEMU 全虚拟化和准虚拟化(Para-virtulizaiton) (4)I ...

  9. C#程序如何以管理员身份运行

    VISTA 和 Windows 7 都使用了UAC来控制程序访问,对于一些需要使用管理员身份运行的程序就得右键以管理员身份运行. C# 编程中可以使程序自动使用管理员身份运行,也就是我们常常看到一些程 ...

  10. UI设计教程:如何在设计中运用颜色

    灰度优先 我们习惯在设计阶段的早期就开始调整颜色和色调.但是,当你意识到自己花了3个小时来调整主色调的时候,你发现这种行为毫无帮助.虽然把玩颜色很有吸引力,但是你应该避免在设计初期进行这种行为. 相反 ...