一.开发环境

eclipse版本:4.6.1

maven版本:3.3.3

junit版本:4.12

spring版本:4.1.5.RELEASE

JDK版本:1.8.0_111

二.项目结构

三.文件清单

pom.xml

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  3. <modelVersion>4.0.0</modelVersion>
  4. <groupId>springJunit</groupId>
  5. <artifactId>springJunit</artifactId>
  6. <version>1.0</version>
  7. <packaging>war</packaging>
  8. <properties>
  9. <!-- spring版本号 -->
  10. <spring.version>4.1.5.RELEASE</spring.version>
  11. <!-- junit版本号 -->
  12. <junit.version>4.12</junit.version>
  13. </properties>
  14. <dependencies>
  15. <dependency>
  16. <groupId>org.springframework</groupId>
  17. <artifactId>spring-test</artifactId>
  18. <version>${spring.version}</version>
  19. </dependency>
  20. <dependency>
  21. <groupId>org.springframework</groupId>
  22. <artifactId>spring-context</artifactId>
  23. <version>${spring.version}</version>
  24. </dependency>
  25. <dependency>
  26. <groupId>junit</groupId>
  27. <artifactId>junit</artifactId>
  28. <version>${junit.version}</version>
  29. <scope>test</scope>
  30. </dependency>
  31. </dependencies>
  32. </project>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>springJunit</groupId>
<artifactId>springJunit</artifactId>
<version>1.0</version>
<packaging>war</packaging>
<properties>
<!-- spring版本号 -->
<spring.version>4.1.5.RELEASE</spring.version>
<!-- junit版本号 -->
<junit.version>4.12</junit.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

JunitTest.java

  1. package com.dqiang;
  2. public interface JunitTest {
  3. public String sayHelloWorld(String word);
  4. }
package com.dqiang;

public interface JunitTest {

public String sayHelloWorld(String word);

}

JunitTestImpl.java

  1. package com.dqiang.impl;
  2. import org.springframework.stereotype.Service;
  3. import com.dqiang.JunitTest;
  4. @Service
  5. public class JunitTestImpl implements JunitTest {
  6. public String sayHelloWorld(String word) {
  7. return "hello->" + word;
  8. }
  9. }
package com.dqiang.impl;

import org.springframework.stereotype.Service;

import com.dqiang.JunitTest;

@Service

public class JunitTestImpl implements JunitTest {

public String sayHelloWorld(String word) {

return "hello->" + word;

}

}

JTest.java

  1. package springJunit;
  2. import org.junit.Test;
  3. import org.junit.runner.RunWith;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.test.context.ContextConfiguration;
  6. import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
  7. import com.dqiang.JunitTest;
  8. @RunWith(SpringJUnit4ClassRunner.class)
  9. @ContextConfiguration(locations = { "classpath:springmvc.xml" })
  10. public class JTest {
  11. @Autowired
  12. private JunitTest junitTest;
  13. @Test
  14. public void sayHelloWorld() {
  15. String result = junitTest.sayHelloWorld("junit Test");
  16. System.out.println(result);
  17. }
  18. }
package springJunit;

import org.junit.Test;

import org.junit.runner.RunWith;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.test.context.ContextConfiguration;

import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import com.dqiang.JunitTest; @RunWith(SpringJUnit4ClassRunner.class)

@ContextConfiguration(locations = { "classpath:springmvc.xml" })

public class JTest {

@Autowired

private JunitTest junitTest;
@Test
public void sayHelloWorld() {
String result = junitTest.sayHelloWorld("junit Test");
System.out.println(result);
}

}

说明:

@RunWith 使用RunWith注解改变JUnit的默认执行类,并实现自已的Listener在平时的单元测试,如果不使用RunWith注解,那么JUnit将会采用默认的执行类Suite执行。

@ContextConfiguration 可以通过该属性手工指定 Spring 配置文件所在的位置,可以指定一个或多个 Spring 配置文件。

Demo下载:点击下载

Spring整合Junit框架进行单元测试Demo的更多相关文章

  1. Spring整合JUnit框架进行单元测试代码使用详解

    一.Spring提供的JUnit框架扩展:   1. AbstractSpringContextTests:spring中使用spring上下文测试的Junit扩展类,我们一般不会使用这个类来进行单元 ...

  2. Spring整合Junit框架

    一.开发环境 eclipse版本:4.6.1 maven版本:3.3.3 junit版本:4.12 spring版本:4.1.5.RELEASE JDK版本:1.8.0_111 二.项目结构 图 三. ...

  3. 【SSH框架】系列之 Spring 整合 Hibernate 框架

    1.SSH 三大框架整合原理 Spring 与 Struts2 的整合就是将 Action 对象交给 Spring 容器来负责创建. Spring 与 Hibernate 的整合就是将 Session ...

  4. Spring整合EHCache框架

    在Spring中使用缓存可以有效地避免不断地获取相同数据,重复地访问数据库,导致程序性能恶化. 在Spring中已经定义了缓存的CacheManager和Cache接口,只需要实例化便可使用. Spr ...

  5. 【Spring】Spring系列7之Spring整合MVC框架

    7.Spring整合MVC框架 7.1.web环境中使用Spring 7.2.整合MVC框架 目标:使用Spring管理MVC的Action.Controller 最佳实践参考:http://www. ...

  6. spring整合junit报错

    1.Could not autowire field: private javax.servlet.http.HttpServletRequest 参考:https://www.cnblogs.com ...

  7. Spring整合Struts2框架的第二种方式(Action由Spring框架来创建)(推荐大家来使用的)

    1. spring整合struts的基本操作见我的博文:https://www.cnblogs.com/wyhluckdog/p/10140588.html,这里面将spring与struts2框架整 ...

  8. Spring整合Struts2框架的第一种方式(Action由Struts2框架来创建)。在我的上一篇博文中介绍的通过web工厂的方式获取servcie的方法因为太麻烦,所以开发的时候不会使用。

    1. spring整合struts的基本操作见我的上一篇博文:https://www.cnblogs.com/wyhluckdog/p/10140588.html,这里面将spring与struts2 ...

  9. Spring整合junit测试

    本节内容: Spring整合junit测试的意义 Spring整合junit测试 一.Spring与整合junit测试的意义 在没整合junit之前,我们在写测试方法时,需要在每个方法中手动创建容器, ...

随机推荐

  1. bzoj3894: 文理分科(还是那道最小割)

    3894: 文理分科 题目:传送门 感谢波老师没有来D飞我,让我做出了这题... 题解: 这题其实和我做的上一题(bzoj2132)很像,所以就不写题意了. 依然是那最小割... 这题给出了四个利益矩 ...

  2. 面向画布(Canvas)的JavaScript库

    面向画布(Canvas)的JavaScript库 总结 每个库各有特色,根据需求选择   学习要点 面向画布(Canvas)的JavaScript库 EaselJS 是一个封装了 HTML5 画布(C ...

  3. 关于OpenFileDialog的使用(转)

    OpenFileDialog控件有以下基本属性 InitialDirectory 对话框的初始目录 Filter 要在对话框中显示的文件筛选器,例如,"文本文件(*.txt)|*.txt|所 ...

  4. LuoguP4246 [SHOI2008]堵塞的交通

    https://zybuluo.com/ysner/note/1125078 题面 给一个网格,每次把相邻两点连通性改为\(1\)或\(0\),询问两点是否联通. 解析 线段树神题... 码量巨大,细 ...

  5. bzoj1018

    线段树分治+并查集 线段树本身就是分治结构,碰见这种带删除修改的题目是再合适不过的,我们对于每段修改区间在线段树上打标记,每次路过就进行修改,叶子结点表及答案,先把所有修改在线段树上标记,然后dfs就 ...

  6. 单纯形&&线性规划

    沦为了背板子...wyfcyx的ppt #include<bits/stdc++.h> using namespace std; ; , inf = 1e18; int n, m, l, ...

  7. vi编辑文件保存后,提示“Can't open file for writing Press ENTER or type command to continue”

    在linux上使用vi命令修改或者编辑一个文件内容的时候,最后发现使用<Esc+:+wq!>无法保存退出,却出现,如下提示: E212: Can't open file for writi ...

  8. Java系列学习(零)-写在前面的话

    1.为什么写这套笔记 理由一:因为需求,所以学习,然后就要记笔记 理由二:同时学几种相似的语言,怕搞混,所以写 2.笔记修改日志

  9. C语言保留字

    数值变量相关: int float double char long short unsigned signed 储存说明符 const 用于声明常量 static用于限制变量/函数的作用范围等等 e ...

  10. 基于证书的MS SQL2005数据库镜像搭建

    一.准备工作: 3台服务器同版本,硬盘分区大小相同,安装相同版本数据库软件. host中分别标注3台服务器IP和主机名称. 主体服务器上创建数据库,并进行完整备份数据库和数据库事务. 拷贝备份文件给镜 ...