java Spring单元测试详解

2017-12-26 10:33:54来源:oschina作者:乱舞人点击

分享

介绍在Spring的框架下,做单元测试的两种办法。


一、使用spring中对Junit框架的整合功能

除了junit4和spring的jar包,还需要spring-test.jar。引入如下依赖:



org.springframework
spring-test
3.1.1.RELEASE

@ContextConfiguration需要配上spring的配置文件,这样就可以在测试类中使用注解简单的注入需要的bean了。简单高效。


import com.zhisland.weizhan.common.configs.Contants;
import com.zhisland.weizhan.mp.model.sendMsg.SendMsgLog;
import com.zhisland.weizhan.mp.service.RedisCommonService;
import com.zhisland.weizhan.mp.service.SendMsgService;
import com.zhisland.weizhan.mp.service.WmpUserService;
import com.zhisland.wz.model.WxAppConfig;
import com.zhisland.wz.service.MpWeixinApi;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.transaction.TransactionConfiguration;
import java.util.List;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:wmp-servlet.xml")
@TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = false)
public class RedisServiceImplTest extends AbstractTransactionalJUnit4SpringContextTests {
private final static Logger LOGGER = LoggerFactory.getLogger(meetingTest.class);
@Autowired
private SendMsgService sendMsgService;
@Autowired
private MpWeixinApi mpWeixinApi;
@Autowired
private WmpUserService wmpUserService;
@Autowired
private Contants contants;
@Autowired
private RedisCommonService redisCommonService;
@Before
public void setUp() throws Exception {

}
@After
public void tearDown() throws Exception {
}
@Test
public void testGet() {
WxAppConfig wxAppConfig = mpWeixinApi.getWeixinAppConfigByAppId(contants.appId);
List sendMsgLogList = sendMsgService.getWaittingLogListByMsgId(wxAppConfig.getId(), 4, 3, 0);
int first = sendMsgLogList.get(0).getId();
int last = sendMsgLogList.get(sendMsgLogList.size() - 1).getId();
LOGGER.info("first={},last={}", first, last);
// redisCommonService.set("wjt_subscribe_return_url_2","fff哈哈");
//// System.out.println("get redis value="+redisCommonService.get("wjt_subscribe_return_url_2"));
// System.out.println("get redis value="+redisCommonService.getString("wjt_subscribe_return_url_1"));
// System.out.println("get redis value="+redisCommonService.getString("wjt_subscribe_return_url_o8cvjt0Ua1fJDJ0iNwfvrBGi9sbc"));
}}
二、手动加载spring的配置文件,并启动spring容器
public class ReadDaoImplTest {publicstatic void main(String[] args){
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); context.start(); ReadDao fqaService = (ReadDao) context.getBean("readDao");
System.out.println(fqaService);
}}
三、详解

import java.lang.reflect.InvocationTargetException; import java.util.List;


import org.apache.commons.beanutils.BeanUtils; import org.junit.Assert; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.annotation.DirtiesContext.ClassMode; import org.springframework.test.annotation.Rollback; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.support.GenericXmlContextLoader; import org.springframework.test.context.transaction.TransactionConfiguration; import org.springframework.transaction.annotation.Transactional;


import com.cgodo.mvc.service.Result; import com.security.entity.SecurityPermission; import com.security.model.AddSecurityPermissionModel; import com.security.model.UpdateSecurityPermissionModel; import com.security.service.SecurityPermissionI;


/** * * 权限服务层测试类 */


//测试时需要的


@RunWith(value = SpringJUnit4ClassRunner.class)//在使用所有注释前必须使用@RunWith(SpringJUnit4ClassRunner.class),让测试运行于Spring测试环境 @ContextConfiguration(locations = {"classpath:/com/security/resource/spring/context/application/applicationContext.xml"} , loader = GenericXmlContextLoader.class)//【@ContextConfiguration用来指定加载的Spring配置文件的位置,会加载默认配置文件,


@ContextConfiguration 注解有以下两个常用的属性:locations:可以通过该属性手工指定 Spring 配置文件所在的位置,可以指定一个或多个 Spring 配置文件用,分开。如下所示: @ContextConfiguration(locations={“aa/aa.xml”,” aa/bb.xml”})


inheritLocations:是否要继承父测试用例类中的 Spring 配置文件,默认为 true。】

@DirtiesContext(classMode = ClassMode.AFTER_CLASS)//@DirtiesContext在测试方法上出现这个注解时,表明底层Spring容器在该方法的执行中被“污染”,从而必须在方法执行结束后重新创建(无论该测试是否通过)。


@TransactionConfiguration( transactionManager = "transactionManager" , defaultRollback = false)//【@TransactionConfiguration为配置事务性测试定义了类级别的元数据。PlatformTransactionManager默认的实例叫transactionManager,如果需要的PlatformTransactionManager不是“transactionManager”的话,那么可以显式配置驱动事务的PlatformTransactionManager的bean的名字。此外, 可以将defaultRollback标志改为false,表示不回滚。通常, @TransactionConfiguration与@ContextConfiguration搭配使用。


@ContextConfiguration @TransactionConfiguration(transactionManager="transactionManager", defaultRollback=false)】


@Transactional//开启类级别的事物 public class TestSecurityPermissionService {


//注解生成SecurityPermissionI 的代理 @Autowired SecurityPermissionI securityPermissionI;/***描述:初始化测试数据* @throws InvocationTargetException* @throws IllegalAccessException*/ @Before public void before() throws IllegalAccessException, InvocationTargetException{ //添加删除测试数据 SecurityPermission securityPermissionDelete = new SecurityPermission(); securityPermissionDelete.setPermissionId("TEST_permissionId-Delete"); securityPermissionDelete.setPermissionDescription("TEST_权限描述信息-Delete"); securityPermissionDelete.setPermissionType("TEST_权限类型-Delete"); Result> resultDelete = securityPermissionI.getPermissionsByType(securityPermissionDelete);AddSecurityPermissionModel addSecurityPermissionModelDelete = new AddSecurityPermissionModel(); BeanUtils.copyProperties(addSecurityPermissionModelDelete, securityPermissionDelete); if(resultDelete.getData() == null){ securityPermissionI.addPermission(addSecurityPermissionModelDelete); }//添加删除测试数据 SecurityPermission securityPermissionDeleteNot = new SecurityPermission(); securityPermissionDeleteNot.setPermissionId("TEST_permissionId-DeleteNot"); securityPermissionDeleteNot.setPermissionDescription("TEST_权限描述信息-DeleteNot"); securityPermissionDeleteNot.setPermissionType("TEST_权限类型-DeleteNot"); Result> resultDeleteNot = securityPermissionI.getPermissionsByType(securityPermissionDeleteNot);AddSecurityPermissionModel addSecurityPermissionModelDeleteNot = new AddSecurityPermissionModel(); BeanUtils.copyProperties(addSecurityPermissionModelDeleteNot, securityPermissionDeleteNot); if(resultDeleteNot.getData() == null){ securityPermissionI.addPermission(addSecurityPermissionModelDeleteNot); }//添加更新测试数据 SecurityPermission securityPermissionUpdate = new SecurityPermission(); securityPermissionUpdate.setPermissionId("TEST_permissionId-Update"); securityPermissionUpdate.setPermissionDescription("TEST_权限描述信息-Update"); securityPermissionUpdate.setPermissionType("TEST_权限类型-Update"); Result> resultUpdate = securityPermissionI.getPermissionsByType(securityPermissionUpdate);AddSecurityPermissionModel addSecurityPermissionModelUpdate = new AddSecurityPermissionModel(); BeanUtils.copyProperties(addSecurityPermissionModelUpdate, securityPermissionUpdate); if(resultUpdate.getData() == null){ securityPermissionI.addPermission(addSecurityPermissionModelUpdate); }//添加查询测试数据 SecurityPermission securityPermissionSelect = new SecurityPermission(); securityPermissionSelect.setPermissionId("TEST_permissionId-Select"); securityPermissionSelect.setPermissionDescription("TEST_权限描述信息-Select"); securityPermissionSelect.setPermissionType("TEST_权限类型-Select"); Result> resultSelect = securityPermissionI.getPermissionsByType(securityPermissionSelect);


AddSecurityPermissionModel addSecurityPermissionModelSelect = new AddSecurityPermissionModel(); BeanUtils.copyProperties(addSecurityPermissionModelSelect, securityPermissionSelect); if(resultSelect.getData() == null){ securityPermissionI.addPermission(addSecurityPermissionModelSelect); }}//@Test这个将不会被测试 public void Test(){ boolean bb = false; Assert.assertFalse(bb); }

@Rollback(true)//设置测试后回滚 @Transactional//为方法开启事物 @Test//标注要测试的方法,不添加@Test将不会进行测试 public void TestAddPermission(){ SecurityPermission securityPermission = new SecurityPermission(); securityPermission.setPermissionId("TEST_permissionId-Add"); securityPermission.setPermissionDescription("TEST_权限描述信息-Add"); securityPermission.setPermissionType("TEST_权限类型-Add");AddSecurityPermissionModel model = new AddSecurityPermissionModel(); try { BeanUtils.copyProperties(model, securityPermission); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InvocationTargetException e) { // TODO Auto-generated catch block e.printStackTrace(); }//测试权限不存在的情况 Result isNotExists; try { isNotExists = securityPermissionI.addPermission(model);


Assert.assertTrue(isNotExists.isSuccess()); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InvocationTargetException e) { // TODO Auto-generated catch block e.printStackTrace(); }//测试权限已存在的情况 Result isExists; try { isExists = securityPermissionI.addPermission(model); Assert.assertFalse(isExists.isSuccess()); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InvocationTargetException e) { // TODO Auto-generated catch block e.printStackTrace(); } }}

微信扫一扫

第七城市微信公众平台