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. SideBar 选择城市时右侧边上的 选择bar

    需要定义一个SideBar的视图类  在布局文件中引用   同时在布局中设置一个textView默认不可见 当触摸时才显示   在调用的Activity中 sideBar.setOnTouchingL ...

  2. jquery的validate表单验证

    html: <form id="reg" action="123.html"> <p class="myerror"> ...

  3. JS获取Dropdownlist选中值

    var dropDownList = document.getElementById("ddl_sheng"); //获取DropDownList控件 var dropDownLi ...

  4. URLEncoder.encode转译后“空格”变“加号”的问题的解决方案

    我用dst_fname=URLEncoder.encode(dst_fname);对字符串dst_fname进行编码,但是发现空格全部都变成了加号,我们提需求的傻B非得要空格的,但是不编码有很多非常特 ...

  5. 引爆你的Javascript代码进化

    转自:http://www.hicss.net/evolve-your-javascript-code/ 方才在程序里看到一段JS代码,写法极为高明,私心想着若是其按照规范来写,定可培养对这门语言的理 ...

  6. Luogu 2467[SDOI2010]地精部落 - DP

    Solution 这题真秒啊,我眼瞎没有看到这是个排列 很显然, 有一条性质: 第一个是山峰 和 第一个是山谷的情况是一一对应的, 只需要把每个数 $x$  变成 $n-x+1$ 然后窝萌定义数组 $ ...

  7. Python之路(第二十五篇) 面向对象初级:反射、内置方法

    [TOC] 一.反射 反射的概念是由Smith在1982年首次提出的,主要是指程序可以访问.检测和修改它本身状态或行为的一种能力(自省).这一概念的提出很快引发了计算机科学领域关于应用反射性的研究.它 ...

  8. jquery plugin 之 form表单验证插件

    基于h5表单验证系统.扩展了对easyui组件的支持 先上图: 提示样式用到了伪对象的 {content: attr(xxx)}函数方法,实现提示信息能动态切换. 1.关键属性说明: type: 表单 ...

  9. https的名词解释

    原文 http://www.cnblogs.com/guogangj/p/4118605.html 那些证书相关的玩意儿(SSL,X.509,PEM,DER,CRT,CER,KEY,CSR,P12等) ...

  10. 【WebService】调用第三方提供的webService服务(七)

    互联网上面有很多的免费webService服务,我们可以调用这些免费的WebService服务,将一些其他网站的内容信息集成到我们的Web应用中显示,下面就以获取天气预报数据和查询国内手机号码归属地为 ...