一.开发环境

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. YTU 2720: 删出多余的空格

    2720: 删出多余的空格 时间限制: 1 Sec  内存限制: 128 MB 提交: 338  解决: 201 题目描述 小平在给弟弟检查英语作业时时,发现每个英语句子单词之间的空格个数不等,请你编 ...

  2. go语言笔记——调试还很弱,用gdb来做?可用panic和defer。格式化代码使用gofmt,貌似我的vim插件是自带

    3.3 调试器 应用程序的开发过程中调试是必不可少的一个环节,因此有一个好的调试器是非常重要的,可惜的是,Go 在这方面的发展还不是很完善.目前可用的调试器是 gdb,最新版均以内置在集成开发环境 L ...

  3. P3309 [SDOI2014]向量集

    传送门 达成成就:一人独霸三页提交 自己写的莫名其妙MLE死都不知道怎么回事,照着题解打一直RE一个点最后发现竟然是凸包上一个点求错了--四个半小时就一直用来调代码了-- 那么我们只要维护好这个凸壳, ...

  4. android:scaleType 布局文件加载图片时候的显示方式

    android:scaleType="center" 以原图的几何中心点和ImagView的几何中心点为基准,按图片的原来size居中显示,不缩放,当图片长/宽超过View的长/宽 ...

  5. Kibana里No Marvel Data Found问题解决(图文详解)

    问题详情 http://192.168.80.145:5601/app/marvel#/no-data?_g=(refreshInterval:(display:'10%20seconds',paus ...

  6. SQL Sever语言 存储过程及触发器

    存储过程:就像函数一样的会保存在数据库中-->可编程性-->存储过程 创建存储过程: 保存在数据库表,可编程性,存储过程create proc jiafa --需要的参数@a int,@b ...

  7. 学习Objective-C入门教程(分享)

    原百度文库连接:http://wenku.baidu.com/view/6786064fe518964bcf847c63.html PS:需要原文档的可以留邮箱发送! (我叫雷锋,不要谢我) 学习Ob ...

  8. Scala-基础-变量与常量

    import junit.framework.TestCase import org.junit.Test //变量 //var 代表变量 //val 代表常量 //关键字 class,extends ...

  9. 【1】Jdk1.8中的HashMap实现原理

    HashMap概述 HashMap是基于哈希表的Map接口的非同步实现.此实现提供所有可选的映射操作,并允许使用null值和null键.此类不保证映射的顺序,特别是它不保证该顺序恒久不变. 内部实现 ...

  10. node的api

    一. 1.url: 绝对URI http://user:pass@www.example.com:80/dir/index.html?uid=1#ch1 协议 登录信息 服务器地址 端口 文件路径 查 ...