1 基类整合spring容器
package com.spring;
import org.junit.Before;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestContextManager;
import com.SupplierJingBeanJsfFacade;
//@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring-config.xml")
public class SpringTestBase {
@Autowired
public SupplierJingBeanJsfFacade consumer;
private TestContextManager testContextManager;
// 每次运行都会执行 与 @BeforeClass的区别
@Before
public void setUp() {
// 自动注解与@RunWith(SpringJUnit4ClassRunner.class) 效果一样
testContextManager = new TestContextManager(getClass());
try {
testContextManager.prepareTestInstance(this);
} catch (Exception e) {
e.printStackTrace();
}
}
public SupplierJingBeanJsfFacade getConsumer() {
return consumer;
}
}
2 子类,参数化测试
package com.spring;
import java.util.Arrays;
import java.util.List;
import org.apache.commons.lang.builder.ReflectionToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import org.apache.log4j.Logger;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import junit.framework.Assert;
@RunWith(Parameterized.class)
public class SupplierJingBeanJsfFacadeImplTest extends SpringTestBase {
private static Logger logger = Logger.getLogger(SupplierJingBeanJsfFacadeImplTest.class);
private KV param;
private String result;
@Parameters
public static List<Object[]> data() {
KV kv1 = new KV();
kv1.supplierId = 4512667L;
kv1.operType = 1;//1.关闭
KV kv2 = new KV();
kv2.supplierId = 4512667L;
kv2.operType = 2;//2.冻结
KV kv3 = new KV();
kv3.supplierId = 4512667L;
kv3.operType = 3;//3.解冻
return Arrays.asList(new Object[][] { { kv1, "0000" }, { kv2, "0000" }, { kv3, "0000" } });
}
public SupplierJingBeanJsfFacadeImplTest(KV param, String result) {
this.param = param;
this.result = result;
}
/**
*
* 2 在关闭状态下冻结,
* 3 在关闭状态下,进行解冻,报0005非冻结状态不可进行解冻
*/
@Test
public void operationStatus() {
SupplierJingBeanResult r = this.getConsumer().operationStatus(this.param.supplierId, this.param.operType);
Assert.assertEquals(this.result , r.getResultCode());
logger.error(ReflectionToStringBuilder.toString(r, ToStringStyle.MULTI_LINE_STYLE));
logger.error(ReflectionToStringBuilder.toString(r.getBuyBeansActivityVo(), ToStringStyle.MULTI_LINE_STYLE));
logger.error("------------------------------------------------------------");
}
}
class KV {
Long supplierId;
Integer operType;// 1.关闭 2.冻结 3.解冻
}
3 运行子类
进行junit测试,依赖类可以自动加载
Junit4 同时使用@Parameterized参数化测试和Spring容器
猜你喜欢
转载自angie.iteye.com/blog/2374069
今日推荐
周排行