spring test demo

package com.mycom.billing;

import java.lang.reflect.InvocationTargetException;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.beanutils.BeanUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.AbstractSingleSpringContextTests;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestExecutionListeners;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;
import org.springframework.test.context.transaction.TransactionConfiguration;
import org.springframework.test.context.transaction.TransactionalTestExecutionListener;
import org.springframework.transaction.annotation.Transactional;

import com.mycom.billing.service.BalanceException;
import com.mycom.billing.service.BalanceFreezeException;
import com.mycom.billing.service.PaymentFaileException;
import com.mycom.billing.service.WithdrawService;
import com.mycom.common.lang.DateUtil;
import com.mycom.exception.TargetAlreadyExistsException;
import com.mycom.model.account.AccBankAccount;
import com.mycom.model.account.AccWithdrawCash;

@RunWith(SpringJUnit4ClassRunner.class)
@TransactionConfiguration(transactionManager = "mycom-persist-transactionManager", defaultRollback = true)
@TestExecutionListeners( { TransactionalTestExecutionListener.class,
		DependencyInjectionTestExecutionListener.class })
@Transactional
@ContextConfiguration(locations = {
		"classpath*:spring/applicationContext-billing-service.xml",
		"classpath*:spring/applicationContext-persist.xml",
		"classpath*:spring/applicationContext-logging.xml",
		"classpath*:spring/applicationContext-base-service.xml",
		"classpath*:spring/applicationContext-*.xml",})
public class WithdrawServiceImplTest extends AbstractSingleSpringContextTests {
	@Resource(name = "withdrawService")
	private WithdrawService withdrawService;

	/**
	 * 查询提现银行
	 */
	@Test
	public void findWithdrawBankTest() {
		AccBankAccount bank = withdrawService.findWithdrawBank(195L);
		System.out.println(bank.getsBankAccount() + ":" + bank.getnAccountId()
				+ ":" + bank.getnCityId());
	}

	/**
	 * 设置提现银行
	 * 
	 * @param accountId
	 * @param bankId
	 * @param branchBankId
	 * @param bankAccount
	 * @param cityId
	 * @throws PaymentFaileException
	 */
	@Test
	// 使用junit4的注释方法进行测试
	@Transactional
	public void setWithdrawBankTest() {
		AccBankAccount bank = new AccBankAccount();
		bank.setnAccountId(195L);
		bank.setnBankId(2137814318L);
		bank.setnBranchId(3L);
		bank.setnCityId(82L);
		bank.setsBankAccount("217381926313123");
		try {
			withdrawService.setWithdrawBank(bank);
		} catch (PaymentFaileException e) {
			e.printStackTrace();
		}
		AccBankAccount bank1 = withdrawService.findWithdrawBank(195L);
		System.out.println(bank1.getsBankAccount() + ":"
				+ bank1.getnAccountId() + ":" + bank1.getnCityId());
	}

        //...
}






//指定测试用例运行器
@RunWith(SpringJUnit4ClassRunner.class)
// 事务配置
// transactionManager="这里是applicationContext.xml文件中事务管理器的名称,默认是transactionManager"
// defaultRollback=true 事务执行后是回滚还是提交,如果不希望测试用数据留在数据库中,可以设置为true
@TransactionConfiguration(transactionManager = "mycom-persist-transactionManager", defaultRollback = true)
// 注册测试用例的监听器。
// 加入TransactionalTestExecutionListener后,它负责解析 @Transactional、@NotTransactional
// 以及 @Rollback 等事务注解的注解。
// @Transaction 注解让测试方法工作于事务环境中,不过在测试方法返回前事务会被回滚。你可以使用 @Rollback(false)
// 让测试方法返回前提交事务。而 @NotTransactional 注解则让测试方法不工作于事务环境中。
// 加入DependencyInjectionTestExecutionListener后,可以识别@Autowired,本类的属性名与applicationContext中bean
// id或name相同,则autowire
@TestExecutionListeners( { TransactionalTestExecutionListener.class,
		DependencyInjectionTestExecutionListener.class })
// 使测试用例的所有方法都将工作于事务环境下
@Transactional
// 上下文配置。
// locations:指定applicationContext.xml文件的路径,可以通过该属性手工指定 Spring
// 配置文件所在的位置,可以指定一个或多个 Spring
// 配置文件@ContextConfiguration(locations={“xx/yy/beans1.xml”,” xx/yy/beans2.xml”})
// inheritLocations:是否要继承父测试用例类中的 Spring 配置文件,默认为 true
@ContextConfiguration(locations = {
		"classpath*:spring/applicationContext-persist.xml",
		"classpath*:spring/applicationContext-account-service.xml",
		"classpath*:spring/applicationContext-logging.xml",
		"classpath*:spring/applicationContext-base-service.xml" })


你可能感兴趣的:(spring,xml,bean,JUnit,配置管理)