spring测试父类,使用junit-4.4.jar,spring-test.jar
@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); } }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。