一.开发环境

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. NoSQL 世界交换数据的事实标准

    https://www.elastic.co/guide/cn/elasticsearch/guide/current/data-in-data-out.html An object is a lan ...

  2. Olddriver’s books

    Olddriver 的书多的吓人,什么算法导论,组合数学英 文版(orz)......他把 n 本书都放在身后的桌子上, 每本书有一定的面积,并且书可以覆盖,求 n 本书覆盖桌面 的面积 输入格式: ...

  3. codevs1993 草地排水(最大流)

    1993 草地排水 USACO  时间限制: 2 s  空间限制: 256000 KB  题目等级 : 钻石 Diamond   题目描述 Description 在农夫约翰的农场上,每逢下雨,Bes ...

  4. Java高质量20问

    问题一:在多线程环境中使用HashMap会有什么问题?在什么情况下使用get()方法会产生无限循环? HashMap本身没有什么问题,有没有问题取决于你是如何使用它的.比如,你在一个线程里初始化了一个 ...

  5. Kafka的3节点集群详细启动步骤(Zookeeper是外装)

    首先,声明,kafka集群是搭建在hadoop1.hadoop2和hadoop3机器上. kafka_2.10-0.8.1.1.tgz的1或3节点集群的下载.安装和配置(图文详细教程)绝对干货 如下分 ...

  6. 初步认识kafka

    Kafka是用于日志处理的分布式消息队列,Kafka使用scala语言开发的. 各个开源分布式处理系统Cloudera.Apache Storm.Spark都支持与Kafka集成.其日志处理的一个场景 ...

  7. 消息队列 (2) java实现简单的RabbtMQ

    假设有如下问题: 1.如果消费者连接中断,这期间我们应该怎么办? 2.如何做到负载均衡? 3.如何有效的将数据发送到相关的接收者?就是怎么样过滤 4.如何保证消费者收到完整正确的数据 5.如何让优先级 ...

  8. WinForm 之 使用ListView控件展示数据

    在学习了这么多的WinForm基本控件后,今天在来学习一个比较有意思的包含图片的控件! >>>图像列表控件 ImageList是含有图像对象的集合,可以通过索引或关键字引用该集合中的 ...

  9. Gradle的属性Property设置与调用

    Gradle在默认情况下已经为Project定义了很多Property: project:Project本身 name:Project的名字 path:Project的绝对路径 description ...

  10. DB2使用收集

    db2命令收集 创建带分区的表: create table  table_name( )in table_space index in index_space partition by range(c ...