Spring集成Junit testNG
2015-10-28 18:38
302 查看
Spring专门为Junit testNG提供了一套测试集成接口类——AbstractSpringContextTests类,对于testNG就是其子类:AbstractTestNGSpringContextTests。Spring和testNG整合后,进行单元测试的时只要test类继承该类,就可以方便的使用spring注入。实现了spring和testNG的无缝整合,我们可以像写普通类那样测试被spring IoC容器所管理的类(否则我们必须在开始执行单元测试前重新加载Spring beanfactory,再用getBean("xxx")的方式获取IoC容器中类。) 除此以外,对测试类spring beanfactory缓存,使得多个测试类之间可以共享同一个的beanfactory实例,从而减少了重复生成beanfactory,提高了运行效率。 继承该类的测试用例在spring管理的事务中进行,测试完后对数据库的记录不会造成任何影响。你对数据库进行一些操作后,它会自动把数据库回滚,这样就保证了你的测试对于环境没有任何影响 集成代码如下 @ContextConfiguration(locations = {"classpath:spring/spring-mvc.xml"}) public class MyFirstTestNg extends AbstractTestNGSpringContextTests {}package com.bestpay.cif.product.service;
import cn.com.bestpay.Response;
import com.bestpay.cif.core.enummodel.*;
import com.bestpay.cif.product.bizparammodel.CustomerCreateReqDto;
import com.bestpay.cif.product.bizparammodel.CustomerCreateResDto;
import com.bestpay.cif.product.facade.CustomerProdFacade;
import org.junit.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
/**
* Created by laiwenhua on 2015/10/28.
*/
//@ContextConfiguration(locations = "classpath:spring/spring-mvc.xml")
@ContextConfiguration(locations = {"classpath:spring/spring-mvc.xml"})
public class MyFirstTestNg extends AbstractTestNGSpringContextTests {
@Autowired
private CustomerProdFacade customerService;
@BeforeClass
public void beforeClass() {
System.out.println(customerService+"-----个人开户测试start---------------");
}
@Test
public void TestNgLearn() {
CustomerCreateReqDto dto = new CustomerCreateReqDto();
dto.setCustomerType(CustomerType.PERSON.getCode());
dto.setLoginName("13600000005");
dto.setBindMobile("13600000005");
dto.setTelecomCompany(TelecomCompany.DX.getCode());
dto.setRegChannel(CustRegChannel.STATIION.getCode());
dto.setDataSourceType(DataSourceType.NORMAL.getCode());
dto.setCertificateType(CertificateType.ID.getCode());
dto.setCertificateNo("22222222222014111304");
dto.setCustomerName("用户111304");
dto.setLoginType(LoginType.MOBILE.getCode());
dto.setLoginPwd("123456");
dto.setPayPassword("654321");
dto.setCreatedBy("webSys");
dto.setOccupation("IT");
dto.setGender("F");
dto.setEmail("lcd@163.com");
dto.setPostCode("200000");
dto.setHomePhone("66666666");
System.out.println("-------------------"+customerService);
Response<CustomerCreateResDto> res = customerService.createPersonCustomer(dto, "createPersonCustomerTest");
System.out.println("createPersonCustomerTest 返回结果:" + res.isSuccess() + " | " + res.getErrorCode() + " | " + res.getErrorMsg());
Assert.assertEquals(true, res.isSuccess());
}
@AfterClass
public void afterClass() {
System.out.println("-----个人开户测试end---------------");
}
}
相关文章推荐
- 使用 Spring 进行单元测试
- TestNG基本概念和注解
- [selenium + eclipse + testng]Build environment
- ant testng 问题
- ant testng eclipse 环境调试
- JUnit 4 Vs TestNG 对比
- TestNG使用Eclipse建立Test Case - 就是爱Java
- Effective Java单元测试TestNG - 就是爱Java
- testng集成spring测试
- 使用ant运行testng的testng.xml并且使用testng-results.xsl美化结果
- TestNG分组测试
- testng进行单元测试时报错Software caused connection abort: socket write error解决方法
- 利用Maven执行TestNG(( linux/windows)
- Windows命令行执行TestNG
- TestNG 编写带有依赖的测试用例
- TestNG 参数化测试
- Selenium 实现WEB测试持续回归测试
- Eclipse TestNG环境
- 简单的TestNG.xml在Eclipse中跑TestNG suite
- TestNG入门指南