一.开发环境

eclipse版本:4.6.1

maven版本:3.3.3

junit版本:4.12

spring版本:4.1.5.RELEASE

JDK版本:1.8.0_111

二.项目结构

Spring整合Junit框架进行单元测试Demo_xml

三.文件清单

pom.xml



[java]​view plain​​ ​​copy​

​print​​​​?​

  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



[java]​view plain​​ ​​copy​

​print​​​​?​

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

public String sayHelloWorld(String word);

}


JunitTestImpl.java



[java]​view plain​​ ​​copy​

​print​​​​?​

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



[java]​view plain​​ ​​copy​

​print​​​​?​

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