@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); } }