Spring集成Junit

Spring集成Junit可以采用注解的方法注入对应的依赖接口,使但与测试更加简洁,易用

一:准备工作

spring版本3.2.4

下载地址:http://repo.spring.io/libs-release-local/org/springframework/spring/3.2.4.RELEASE/spring-framework-3.2.4.RELEASE-dist.zip

Junit版本:junit-4.8.2

开发工具:eclipse3.6  tomcat6

任意创建一个项目  springJunit

扫描二维码关注公众号,回复: 507296 查看本文章

在下载的减压后的spring-framework-3.2.4.RELEASE\libs目录中加入jar包

spring-test-3.2.4.RELEASE.jar 

spring-beans-3.2.4.RELEASE.jar

spring-context-3.2.4.RELEASE.jar

spring-core-3.2.4.RELEASE.jar

以及下载的junit-4.8.2.jar

在src下创建applicationContext.xml

在WEB-INF/web.xml中配置

//web应用启动初始加载spring资源文件 applicationContext.xml
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
     classpath:applicationContext.xml
    </param-value>
 </context-param>
 //配置spring监听
 <listener>
 
    <listener-class>
         org.springframework.web.context.ContextLoaderListener
    </listener-class>
</listener>

applicationContext.xml中配置内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
    xmlns:p="http://www.springframework.org/schema/p"  
    xmlns:context="http://www.springframework.org/schema/context"  
    xmlns:aop="http://www.springframework.org/schema/aop"   
    xmlns:tx="http://www.springframework.org/schema/tx"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans   
    http://www.springframework.org/schema/beans/spring-beans-3.2.xsd  
    http://www.springframework.org/schema/context  http://www.springframework.org/schema/context/spring-context-3.2.xsd  
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd  
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">   
    
 
 
    
    <bean id="person" class="com.model.Person">
  	<constructor-arg index="0" type="java.lang.String" value="Hello" />
  	<constructor-arg index="1" type="java.lang.String" value="Word" />
    </bean>
</beans>

其中增加一个测试类包目录是com.model

package com.model; 

/** 
 * @author  HL Y 
 * @date    创建时间:2014年11月26日 上午10:27:40  
 */

public class Person {
	
	private String name;
	
	private String password;
	
	public Person(String name,String password){
		this.password = password;
		this.name = name;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}
	

}

在src下创建包名:com.junit   并创建一个TestCase 文件名JunitMain.java

 在类名上增加

@RunWith(SpringJUnit4ClassRunner.class) 

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

代码如下:

@RunWith(SpringJUnit4ClassRunner.class)  
@ContextConfiguration(locations={"classpath:applicationContext.xml"}) 
public class JunitMain {
 
	@Autowired(required=true)
	@Qualifier(value="person")  //指定名字后就依照 字段名来注入
	private Person  person;
	
	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
	}
	
	@Test
	public void test() {
	    System.out.println(person.getName());
	    System.out.println(person.getPassword()); 
	}

}

测试步骤:

第一、在配置完spring框架完之后可以直接启动tocmat服务,看是否正常启动

第二、在创建实体类Person并使用构造器注入可以进一步测试,如能正确打印则表明配置成功

猜你喜欢

转载自wo-niu.iteye.com/blog/2161602