我们经常要写junit测试,在不启动整个web项目的情况下,测试自己的service实现或者是dao实现,我们来充分利用下junit4的强大功能。
1.junit4的测试类
import java.util.ArrayList; import java.util.List; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.annotation.Rollback; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import com.founder.mrp.domain.webservice.ErpProductXml; import com.founder.mrp.domain.webservice.ErpTypeSetInfoXml; import com.founder.mrp.service.webservice.ErpTypeSetProductService; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = {"classpath:spring-*.xml"}) public class JunitWebservice { @Autowired ErpTypeSetProductService erpTypeSetProductService; @Test @Rollback(false) public void TestErpTypeSetInfoXml(){ List<ErpTypeSetInfoXml> erpTypeSetInfoXmlList = new ArrayList<ErpTypeSetInfoXml>(); ErpTypeSetInfoXml erpTypeSetInfoXml = new ErpTypeSetInfoXml(); erpTypeSetInfoXml.setAUFNR("1000021571"); erpTypeSetInfoXml.setZ_EDT_2000("ty00001"); erpTypeSetInfoXml.setZ_EDT_2002("试试书"); erpTypeSetInfoXml.setZ_EDT_2003("t00001"); erpTypeSetInfoXml.setZ_EDT_2008("1"); ErpTypeSetInfoXml erpTypeSetInfoXml2 = new ErpTypeSetInfoXml(); erpTypeSetInfoXml2.setAUFNR("1000021582"); erpTypeSetInfoXml2.setZ_EDT_2000("ty00002"); erpTypeSetInfoXml2.setZ_EDT_2002("测试计划"); erpTypeSetInfoXml2.setZ_EDT_2003("t00002"); erpTypeSetInfoXml2.setZ_EDT_2008("1"); erpTypeSetInfoXmlList.add(erpTypeSetInfoXml); erpTypeSetInfoXmlList.add(erpTypeSetInfoXml2); erpTypeSetProductService.saveTypeSetInfo(erpTypeSetInfoXmlList); } @Test @Rollback(false) public void TestErpProductXml(){ List<ErpProductXml> erpProductXmlList = new ArrayList<ErpProductXml>(); ErpProductXml erpProductXml = new ErpProductXml(); erpProductXml.setAUFNR("5000022550"); erpProductXml.setZ_EDT_2000("pr00001"); erpProductXml.setZ_EDT_2001("神射手"); erpProductXml.setZ_PUB_5928("测试印刷厂"); erpProductXml.setZ_EDT_2003("p00001"); erpProductXml.setZ_PUB_5008("100"); ErpProductXml erpProductXml2 = new ErpProductXml(); erpProductXml2.setAUFNR("5000022551"); erpProductXml2.setZ_EDT_2000("pr00002"); erpProductXml2.setZ_EDT_2001("水电费科技"); erpProductXml2.setZ_PUB_5928("测试印刷厂"); erpProductXml2.setZ_EDT_2003("p00002"); erpProductXml2.setZ_PUB_5008("100"); erpProductXmlList.add(erpProductXml); erpProductXmlList.add(erpProductXml2); erpTypeSetProductService.saveErpProduct(erpProductXmlList); } }
2.解释常用到的注解
@RunWith(SpringJUnit4ClassRunner.class)SpringJUnit支持,由此引入Spring-Test框架支持! @ContextConfiguration(locations = "classpath:applicationContext.xml") 多个配置文件的话可以用数组表示{“applicationContext.xml”,“applicationContext1.xml”}; @ContextConfiguration("/spring-context.xml")放在根路径下(即类路径下),然后<import resource="spring-dao.xml" />所有的配置文件和资源文件 @Transactional这个非常关键,如果不加入这个注解配置,事务控制就会完全失效! @TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = true)这里的事务关联到配置文件中的事务控制器(transactionManager = "transactionManager"),
同时指定自动回滚(defaultRollback = true)。这样做操作的数据才不会污染数据库! AbstractTransactionalDataSourceSpringContextTests要想构建这一系列的无污染纯绿色事务测试框架就必须找到这个基类!(即所有事务均不生效) @ActiveProfiles(value="dev")配置环境选择
然后在方法上使用@Test,@RollBack,@Transaction等注解单独修饰