工具类
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;
}