使用 Spring + Mockito+PowerMock +spring-test-dbunit+hsqldb 进行单元测试可实现100%覆盖率

使用 Mockito 比easyMock 更简单

使用 PowerMock 可模拟静态、私有方法

使用spring-test-dbunit 可以实现注解方式导入xml 到数据库,为数据库提供测试数据如:

@DatabaseSetup 
@DatabaseTearDown

使用hsqldb 内置数据库,不会导致数据被误删或其它误操作后导致单元测试跑不过。
 
  
 
  
具体实现
 
  
1、maven 依赖
	    
                junit
                junit
                4.11
                test
            
            
                org.springframework
                spring-test
                3.2.4.RELEASE
                test
            
			
                org.dbunit
                dbunit
                2.4.9
                test
            
            
                com.github.springtestdbunit
                spring-test-dbunit
                1.0.1
                test
            
            
                org.hsqldb
                hsqldb
                2.3.1
                test
            
			
                org.powermock
                powermock-module-junit4-rule-agent
                1.5.5
                test
            
			
                org.mockito
                mockito-all
                ${mockito.version}
                test
            

            
                org.powermock
                powermock-module-junit4
                ${powermock.version}
                test
            

            
                org.powermock
                powermock-api-mockito
                ${powermock.version}
                test
            

 
  
2、单元测试父类BaseSpringTest

 
  
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:spring/bcs-spring-context-test.xml")
@TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, DbUnitTestExecutionListener.class })
@Transactional
public abstract class BaseSpringTest {

}


3、编写单元测试
 
  
@PrepareForTest({ HttpClientUtil.class })//使用powermock
public class IdCardValidateServiceImplTest extends BaseSpringTest {

    @Resource
    @InjectMocks  //把被模拟对象注入
    private IdCardValidateService idCardValidateService;

    @Resource
    private AppConfig appConfig;

    @Mock //被模拟对象
    private SequenceDao sequenceDao;

    @Rule  //spring 与 powermock 组合需要使用PowerMockRule
    public PowerMockRule rule = new PowerMockRule();

    private static  final String  MOCK_RETURN_SUCC_XML = "0000043101180050000013xf89Hoqpk0VIH/9k=100112320150120154442C0AED4A3EE24D4913C71E6DBD323D1EB";
   
    @Before
    public void setUp() {
        MockitoAnnotations.initMocks(this);
        PowerMockito.mockStatic(HttpClientUtil.class);//使用powermock 模拟静态类
    }

    //正常流程,先查库,库里没有再调接口。库里面没有该身份证信息
    @Test
    @DatabaseSetup({"classpath:/dbunit/T_BCS_IDCARD_INFO.xml",
            "classpath:/dbunit/T_BCS_PLAT.xml"})
    @DatabaseTearDown(value = {"classpath:/dbunit/T_BCS_IDCARD_INFO.xml",
            "classpath:/dbunit/T_BCS_PLAT.xml"},type = DatabaseOperation.TRUNCATE_TABLE)
    public void testValidate() throws Exception {
        String seq =RandomStringUtils.randomNumeric(7);
        Mockito.when(sequenceDao.getIdcardLogSeq()).thenReturn(Long.valueOf(seq));//Mockito模拟返回
        Mockito.when(sequenceDao.getIdcardInfoSeq()).thenReturn(Long.valueOf(seq));//Mockito模拟返回
        IdCardReq req = new IdCardReq();
        req.setReqSeq("123");
        req.setPlatCode("1001");
        req.setRequestTime(new Date());
        req.setNeedPhoto(false);
        IDCardVO idCardVO = new IDCardVO();
        idCardVO.setIdentityNo("430022198509136854");
        idCardVO.setIdentityType("00");
        idCardVO.setRealName("刘德华");
        req.setIdCard(idCardVO);
        List params = IdCardValidateServiceImpl.getParamList(req, appConfig);
		//PowerMock模拟返回
        PowerMockito.when(HttpClientUtil.doPost(appConfig.getGztUrl(),params,"UTF-8")).thenReturn(MOCK_RETURN_SUCC_XML);
        BcsResponse res =  idCardValidateService.validate(req);
        Assert.assertEquals(ErrorCodeEnum.SUCCEED_CODE.getErrorcode(),res.getErrorCode());
        Assert.assertEquals(ErrorCodeEnum.SUCCEED_CODE.getErrordesc(),res.getErrorMsg());

    }
}


 
  用 hsqldb 时候注意 
  

数据类型区别
oracle           hsqldb 
varchar2         varchar
number           NUMERIC 
date             TIMESTAMP


另外hsqldb获取sequence与oracle方式不一样,所以需要模拟             



你可能感兴趣的:(junit,java)