Spring整合JUnit

Spring整合Junit

Spring整合Junit,让Junit通知Spring加载配置文件。Spring容器自动进行注入。直观上来看,简化了Junit测试类的书写。

1、导入Jar包【整合Junit的包】

spring-test-3.2.0.RELEASE.jar

2、步骤

编写DAO接口

public interface AccountDAO {
	//汇款是pay,收款是Receipt
	
	public void pay(String outer,Integer money);
	public void receipt(String inner,Integer money);

}


编写DAO实现类

public class AccountDAOImpl extends JdbcDaoSupport implements AccountDAO {

	/* (non-Javadoc)
	 * @see com.Lily.SpringLearning.J_tx_xml.AccountDAO#pay(java.lang.String, java.lang.Integer)
	 */
	@Override
	public void pay(String outer, Integer money) {
		// TODO Auto-generated method stub
		this.getJdbcTemplate().update("update account set money = money - ? where username = ?", money,outer);
		
	}

	/* (non-Javadoc)
	 * @see com.Lily.SpringLearning.J_tx_xml.AccountDAO#receipt(java.lang.String, java.lang.Integer)
	 */
	@Override
	public void receipt(String inner, Integer money) {
		// TODO Auto-generated method stub
		this.getJdbcTemplate().update("update account set money = money + ? where username = ?", money,inner);
		
	}

}


编写SERVICE接口

public interface AccountService {
	
	public void transfer(String outer ,String inner ,Integer money);

}


编写SERVICE实现类

public class AccountSerImpl implements AccountService {

	/* (non-Javadoc)
	 * @see com.Lily.SpringLearning.J_tx_xml.Ser#transfer(java.lang.String, java.lang.String, java.lang.Integer)
	 */
	private AccountDAO accountDao;
	
	
	
	public AccountDAO getAccountDao() {
		return accountDao;
	}



	public void setAccountDao(AccountDAO accountDao) {
		this.accountDao = accountDao;
	}



	@Override
	public void transfer(String outer, String inner, Integer money) {
		// TODO Auto-generated method stub
		accountDao.pay(outer, money);
		//模拟故障
		//int i=1/0;
		accountDao.receipt(inner, money);
	}

}


配置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:context="http://www.springframework.org/schema/context"
         xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
       					   http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
       					   http://www.springframework.org/schema/aop 
       					   http://www.springframework.org/schema/aop/spring-aop.xsd
       					   http://www.springframework.org/schema/context 
       					   http://www.springframework.org/schema/context/spring-context.xsd">
	
	
	
	
	<context:property-placeholder location="classpath:com/Lily/SpringLearning/I_c3p0/JdbcInfo.properties" />
	
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="driverClass" value="${jdbc.driverClass}"></property>
		<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>	
		<property name="user" value="${jdbc.user}"></property>
		<property name="password"  value="${jdbc.password}"></property>
	</bean>
	
	
	
	<bean id="accountDao" class="com.Lily.SpringLearning.k_Junit.AccountDAOImpl">
		<property name="dataSource" ref="dataSource"></property>
	</bean>
	
	
	<bean id="accountService" class="com.Lily.SpringLearning.k_Junit.AccountSerImpl">
		<property name="accountDao" ref="accountDao"></property>
	</bean>
	
			

</beans>


编写Junit测试类

这里是唯一要加上注解的地方。

在开头需要加两个注解:

@RunWith(SpringJUnit4ClassRunner.class)

@ContextConfiguration(locations="classpath:com/Lily/SpringLearning/k_Junit/applicationContext.xml")

然后需要定义一个private的Service接口,并在它前面加上注解@Autowired

 

/**
 * 
 */
package com.Lily.SpringLearning.k_Junit;

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;



/** 
 * * 
 * @author   LilyLee
 * @date     2017年6月26日
 * @time     下午9:02:32
 * @Version  1.0
 * @email    lilylee_1213@foxmail.com
 *
 */

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:com/Lily/SpringLearning/k_Junit/applicationContext.xml")

public class txTest {
	
	@Autowired  //与junit整合,不需要在spring xml配置扫描
	private AccountService accountService;
	

	@Test
	public void test() {
		
		accountService.transfer("dianer", "Lily", 6);
	}

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值