Junit工具类--读取xml文件

工具类

MockDataGenerationUtil:

 

public class MockDataGenerationUtil {

	private static ApplicationContext ctx;

	public MockDataGenerationUtil(String classPath) {
		ctx = new ClassPathXmlApplicationContext(classPath.split(","));
	}

	public <T> T getBean(String paramString, Class<T> paramClass)
			throws BeansException {
		return ctx.getBean(paramString, paramClass);
	}

	@SuppressWarnings("unchecked")
	public <T> List<T> getBeanList(String paramString, Class<T> paramClass)
			throws BeansException {
		List<T> list = ctx.getBean(paramString, List.class);
		return list;
	}
	
	@SuppressWarnings("unchecked")
	public <T> Set<T> getBeanSet(String paramString, Class<T> paramClass)
			throws BeansException {
		Set<T> set = ctx.getBean(paramString, Set.class);
		return set;
	}

	@SuppressWarnings("unchecked")
	public <K, V> Map<K, V> getBeanMap(String paramString,
			Class<K> paramClassKey, Class<V> paramClassValue)
			throws BeansException {
		Map<K, V> map = ctx.getBean(paramString, Map.class);
		return map;
	}

	public Object getDesiriedObject(String beanName) {
		return ctx.getBean(beanName);
	}

}

 

Junit 测试类:

TestUtils

 

public class TestUtils extends DistributionCase {

	@BeforeClass
	public static void setUpBeforeClass() {
		mockDataGenerationUtil = new MockDataGenerationUtil(
				"/net/TestUtils.xml");
	}

	@Test(expected = Exception.class)
	public void testAll() throws Exception {

	}
}

DistributionCase:

 

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {
		"classpath:/spring/Common-config.xml",
		"classpath:/spring/Test-Config.xml" })
public abstract class DistributionCase extends DDSJunit {
	@Autowired
	private Mockery mockContext;

	/**
	 * eliminate effects of errors occurred by other test case 
	 */
	@Before
	public void clearMockContext() {
		try {
			Field f = mockContext.getClass().getDeclaredField("dispatcher");
			f.setAccessible(true);
			f.set(mockContext, new InvocationDispatcher());
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			mockContext.assertIsSatisfied();
		}
	}

	@Test
	public void testProcess() throws Exception {

	}
}

DDSJunit::

@TransactionConfiguration(transactionManager = "txManager", defaultRollback = true)
public abstract class DDSJunit extends
		AbstractTransactionalJUnit4SpringContextTests {
	protected static MockDataGenerationUtil mockDataGenerationUtil;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值