spring測試父類,使用junit-4.4.jar,spring-test.jar

weixin_34262482發表於2014-07-02
@ContextConfiguration(locations = "classpath:conf/applicationContext.xml")
@RunWith(SpringJUnit4ClassRunner.class)
@Transactional
@TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = true)
public abstract class AbstractTestCase extends
		AbstractTransactionalDataSourceSpringContextTests {

}


將配置項都寫在父類中,測試子類繼承該AbstractTestCase類,測試的時候,不會導致資料庫資料汙染,因為繼承AbstractTransactionalDataSourceSpringContextTests每次資料庫操作完成之後都會回滾。

例如:

public class TransFlowServiceTests extends AbstractTestCase {
	@Autowired
	TransFlowService transFlowService;
	
	@Test
	public void testQueryTransFlow() {
		Map map = new HashMap();
		PageBean pb = new PageBean(1, 20, 10);
		map.put("pb", pb);
		List<TransFlow> lis = transFlowService.queryTransFlow(map);
		for (TransFlow tf : lis) {
			System.out.println(tf.getOrderNo() + "-" + tf.getStatus() + "-" + tf.getStatusView() + "-" + tf.getOrderTime() + "-" + tf.getOrderTimeView());
		}
	}

	@Test
	public void testGetTransFlowCount() {
		Map map = new HashMap();
		PageBean pb = new PageBean(1, 20, 10);
		map.put("pb", pb);
		int i = transFlowService.getTransFlowCount(map);
		System.err.println("i="+i);
	}
	
	@Test
	public void testGetTransFlowNum() {
		int j = transFlowService.getTransFlowNum();
		System.out.println("j="+j);
	}

	@Test
	public void testUpdateTransFlowStatus() {
		int k = transFlowService.updateTransFlowStatus();
		System.err.println("k="+k);
	}

}

 

相關文章