基于JUnit4扩展老项目的UT框架且自动DI
- JUnit4的ClassRunner
- MockMvc直接對接口發(fā)起請求
- 橋接ibatis的bean
- Web到App的路由
- 后記
在公司維護的項目使用的框架很老(內(nèi)部自研,基于Spring2實現(xiàn)的),單元測試框架使用的JUnit3。日常工作開發(fā)調(diào)試和自測兩種辦法:啟動服務(wù)(weblogic,要打包啟動,慢)、單元測試(較快,調(diào)試方便)。但老的寫單測實在是很繁瑣:先繼承一個單元測試基類,覆蓋其中獲取配置文件方法(相當(dāng)于配置context文件),再在另外兩個配置文件中修改(與業(yè)務(wù)耦合的很緊),然后開始從context中g(shù)etBean,然后你的準(zhǔn)備工作終于做好了可以開始測試了。尤其對于新同事,有人指導(dǎo)還行,沒有的話簡直抓瞎(當(dāng)然如果深入了解一下,也是能輕易搞定的,比如我哈哈哈)。思來想去決定:controller的單測,可以簡化步驟(比如獲取controller bean然后再調(diào)用對應(yīng)方法這一步);加入自動依賴注入,就像使用@Autowired一樣(當(dāng)前項目中還是使用的全XML配置方式);將配置集中起來一個地方管理(使用注解);升級到JUnit4.12。
JUnit4的ClassRunner
基于JUnit4的擴展,主要是利用其提供的ClassRunner,JUnit4.12默認的是BlockJUnit4ClassRunner,于是我們擴展該類,看看能在這里做點什么。
首先來看必須覆蓋的構(gòu)造器,構(gòu)造參數(shù)clazz就是當(dāng)前測試類的class。除了調(diào)用父類構(gòu)造器,在此處還加了一步Pafa3TestContext.initContext,初始化Ioc容器,以及保存一些測試時需要的上下文信息。
然后注意createTest這個方法,事實上JUnit會根據(jù)測試class生成對應(yīng)的實例。之前說過還實現(xiàn)了自動DI,那么很顯然這一步在生成instance之后做再合適不過了,具體就是prepareAutoInject方法,至此自動DI已經(jīng)實現(xiàn),在測試類里@AutoInject private SomeController controller就可以直接獲取到bean了,當(dāng)然也提供了可以根據(jù)id獲取bean。
public class Pafa3Junit4ClassRunner extends BlockJUnit4ClassRunner {public Pafa3Junit4ClassRunner(Class<?> clazz) throws Exception {super(clazz);Pafa3TestContext.initContext(getTestClass().getJavaClass());}@Overrideprotected Object createTest() throws Exception {Object instance = super.createTest();prepareAutoInject(instance);return instance;}private void prepareAutoInject(Object instance) throws IllegalAccessException {TestClass testClass = getTestClass();List<FrameworkField> frameworkFields = testClass.getAnnotatedFields(AutoInject.class);for (FrameworkField frameworkField : frameworkFields) {Object bean;String beanName = frameworkField.getAnnotation(AutoInject.class).value();if (!"".equals(beanName)) {bean = Pafa3TestContext.getContext().getBean(beanName);} else {Class<?> beanType = frameworkField.getType();Map beansOfType = Pafa3TestContext.getContext().getBeansOfType(beanType, true, true);Iterator it = beansOfType.values().iterator();if (it.hasNext()) {bean = it.next();} else {throw new NoSuchBeanDefinitionException(beanType, "no bean type found");}}Field field = frameworkField.getField();field.setAccessible(true);field.set(instance, bean);}} } public class Pafa3TestContext {private static ApplicationContext context;private static String[] contextLocations;private static String[] sqlConfigLocations;private static Class<?> clazz;private Pafa3TestContext() {}public static void initContext(Class<?> clazz) {Pafa3TestContext.clazz = clazz;initConfigLocations();initContext();}public static ApplicationContext getContext() {return context;}public static String[] getContextLocations() {return contextLocations;}public static String[] getSqlConfigLocations() {return sqlConfigLocations;}private static void initConfigLocations() {ContextLocations annotation = clazz.getAnnotation(ContextLocations.class);if (annotation == null) {throw new IllegalStateException("test class should be annotated with ContextLocations");}sqlConfigLocations = annotation.sqlMap();String[] locations = annotation.context();int len = locations.length;// 業(yè)務(wù)定制的,為了少寫倆,直接先寫死吧contextLocations = Arrays.copyOf(locations, len + 2); contextLocations[len] = "classpath:biz-context.xml";contextLocations[len + 1] = "classpath:common-context.xml";}private static void initContext() {if (context == null) {synchronized (Pafa3TestContext.class) {if (context == null) {context = new ClassPathXmlApplicationContext(getContextLocations());}}}} } @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) @Inherited public @interface ContextLocations {String[] context();String[] sqlMap() default {};}@Retention(RetentionPolicy.RUNTIME) @Target(ElementType.FIELD) @Inherited public @interface AutoInject {String value() default ""; }MockMvc直接對接口發(fā)起請求
原來對controller的測試是要先獲取這個controller的bean,然后調(diào)用接口實際對應(yīng)的方法。這里其實復(fù)雜了,因為bean都是同一個類型的,獲取哪一個并沒有區(qū)別。如果有給定接口,實際已經(jīng)得到了實際要調(diào)用的方法,這個對應(yīng)關(guān)系,也是定義在一個MethodNameResolver類型的bean里的,顯然可以從我們的Pafa3TestContext里獲取到(因為這時候已經(jīng)初始化好了)。
public class MockMvcResult {private ModelAndView modelAndView;private String content;public MockMvcResult(ModelAndView modelAndView, String content) {this.modelAndView = modelAndView;this.content = content;}public Object getModel() {return modelAndView == null ? null : modelAndView.getModel();}public Object getView() {return modelAndView == null ? null : modelAndView.getView();}public String getContentAsString() {return content;} }public interface MockMvc {MockMvcResult request() throws Exception; }public class StandaloneMockMvc implements MockMvc {private final ApplicationContext context = Pafa3TestContext.getContext();private final String url;private final MockHttpServletRequest request;private final MockHttpServletResponse response;public StandaloneMockMvc(StandaloneMockMvcBuilder builder) {this.url = builder.getUrl();this.request = builder.getRequest();this.response = builder.getResponse();}@Overridepublic MockMvcResult request() throws Exception {Map beanMap = context.getBeansOfType(MethodNameResolver.class, true, true);if (beanMap == null || beanMap.isEmpty()) {throw new NoSuchBeanDefinitionException(MethodNameResolver.class, "ensure add the web context file");}String methodName = null;Iterator it = beanMap.values().iterator();while (it.hasNext() && methodName == null) {MethodNameResolver resolver = (MethodNameResolver) it.next();try {methodName = resolver.getHandlerMethodName(request);} catch (NoSuchRequestHandlingMethodException ignored) {}}if (methodName == null) {throw new NoSuchRequestHandlingMethodException(request);}Object controller = context.getBean(url);return dispatchRequest(methodName, controller);}private MockMvcResult dispatchRequest(String methodName, Object controller) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {Method handleMethod = controller.getClass().getDeclaredMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);Object result = handleMethod.invoke(controller, request, response);if (result == null) {return new MockMvcResult(null, response.getContentAsString());}if (ModelAndView.class.isAssignableFrom(result.getClass())) {return new MockMvcResult((ModelAndView) result, null);}return null;} }public class StandaloneMockMvcBuilder {private static final String SESSION_USER = "userinformation";private final String url;private final String method;private final MockHttpServletRequest request;private final MockHttpServletResponse response;public StandaloneMockMvcBuilder(String url) {this("GET", url);}public StandaloneMockMvcBuilder(String method, String url) {this.url = url;this.method = method;this.request = new MockHttpServletRequest(null, this.method, this.url);this.response = new MockHttpServletResponse();}public StandaloneMockMvcBuilder addParameter(String name, String value) {request.addParameter(name, value);return this;}public String getUrl() {return url;}public String getMethod() {return method;}public MockHttpServletRequest getRequest() {return request;}public MockHttpServletResponse getResponse() {return response;}public StandaloneMockMvcBuilder withUser(String uid) {UserInformationVO user = new UserInformationVO();user.setUID(uid);return withUser(user);}public StandaloneMockMvcBuilder withUser(UserInformationVO user) {request.getSession().setAttribute(SESSION_USER, user);return this;}public StandaloneMockMvc build() {return new StandaloneMockMvc(this);} }至此,我們可以直接構(gòu)造對應(yīng)的URL以及相關(guān)參數(shù),使用MockMvc發(fā)起請求等待結(jié)果了。
橋接ibatis的bean
以上兩點完成后,還差一個連接數(shù)據(jù)庫的bean。項目中使用的是ibatis,讀取的sqlmap是定義在一個sqlmap-config.xml里,該配置包含所有的sqlmap(按功能模塊分的),然后由SqlMapClientFactoryBean來讀取sqlmap-config.xml。由于配置都集中管理在ContextLocations注解里了,所以這里也需要重新實現(xiàn),用了一個小聰明,直接根據(jù)配置的sqlMapConfig生成一個XML內(nèi)容交給SqlMapClientFactoryBean去讀取。
public class SimpleSqlMapClientFactoryBean extends SqlMapClientFactoryBean {@Overridepublic void afterPropertiesSet() throws IOException {Resource configLocation = getSqlConfigResource();super.setConfigLocation(configLocation);super.afterPropertiesSet();}private Resource getSqlConfigResource() {String[] configLocations = Pafa3TestContext.getSqlConfigLocations();if (configLocations == null || configLocations.length == 0) {return new ClassPathResource("sqlmap-config.xml");}return builtXMLResource(configLocations);}private Resource builtXMLResource(String[] configLocations) {final String xmlAsString = buildSqlMapConfigContent(configLocations);return new AbstractResource() {@Overridepublic InputStream getInputStream() throws IOException {return new ByteArrayInputStream(xmlAsString.getBytes("UTF-8"));}@Overridepublic String getDescription() {return "XML built as string: " + xmlAsString;}};}private String buildSqlMapConfigContent(String[] configLocations) {Document document = DocumentHelper.createDocument();document.setXMLEncoding("UTF-8");document.addDocType("sqlMapConfig", "-//iBATIS.com//DTD SQL Map Config 2.0//EN", "http://www.ibatis.com/dtd/sql-map-config-2.dtd");Element sqlMapConfig = document.addElement("sqlMapConfig");Element setting = sqlMapConfig.addElement("settings");setting.addAttribute("cacheModelsEnabled", "true");setting.addAttribute("enhancementEnabled", "false");setting.addAttribute("lazyLoadingEnabled", "false");setting.addAttribute("maxRequests", "3000");setting.addAttribute("maxSessions", "3000");setting.addAttribute("maxTransactions", "3000");setting.addAttribute("useStatementNamespaces", "true");for (String location : configLocations) {Element sqlMap = sqlMapConfig.addElement("sqlMap");sqlMap.addAttribute("resource", location);}return document.asXML();} }Web到App的路由
項目是分層部署的,分為了Web(DMZ區(qū))和App(內(nèi)網(wǎng))兩層,前者就是controller所在,然后遠程調(diào)用App層的Action(通過EJB)。在本地單元測試,顯然不會去構(gòu)造一個EJB容器環(huán)境,而是直接通過本地同一個JVM調(diào)用即可(項目中調(diào)用的bean的名字是寫死的),于是實現(xiàn)一個本地的ApplicationController。
public class AppControllerFactoryBean implements FactoryBean {private ApplicationController proxy;@Overridepublic Object getObject() throws Exception {if (proxy == null) {proxy = getProxy();}return proxy;}@Overridepublic Class getObjectType() {return proxy != null ? proxy.getClass() : ApplicationController.class;}@Overridepublic boolean isSingleton() {return true;}private ApplicationController getProxy() {return (ApplicationController) Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(),new Class[]{ApplicationController.class}, new LocalProxyAppControllerInvocationHandler());} }public class LocalProxyAppControllerInvocationHandler implements InvocationHandler {@Overridepublic Object invoke(Object proxy, Method method, Object[] args) throws Throwable {String methodName = method.getName();Class<?>[] parameterTypes = method.getParameterTypes();if (method.getDeclaringClass() == Object.class) {throw new UnsupportedOperationException("unsupported method: " + method);}if ("toString".equals(methodName) && parameterTypes.length == 0) {return "proxy of ApplicationController";}if ("hashCode".equals(methodName) && parameterTypes.length == 0) {return 1;}if ("equals".equals(methodName) && parameterTypes.length == 1) {return Boolean.FALSE;}if (args.length != 1 || !(args[0] instanceof ServiceRequest)) {throw new IllegalArgumentException("arguments length not 1 or not type of ServiceRequest");}return invokeLocal((ServiceRequest) args[0]);}private Object invokeLocal(ServiceRequest request) throws BusinessServiceException {String beanName = request.getRequestedServiceID();Action action = (Action) Pafa3TestContext.getContext().getBean(beanName);return action.perform(request);} }寫完之后發(fā)現(xiàn),似乎不用動態(tài)代理,直接實現(xiàn)ApplicationController就行了= =||。不過鑒于都寫出來了,暫時先用著吧。主要是提醒看代碼的同志,toString, equals, hashCode三個方法,在動態(tài)代理時也是會被代理的。
后記
大功告成,現(xiàn)在寫單元測試的效率比之前提高的簡直不要太多。終于不用東配置一下西添加一下了(而且有兩個還是重復(fù)的),對團隊的提升自我感覺還是比較多的。但是有啥借鑒的么?我覺得沒啥,都是被老項目老框架逼出來的輪子,畢竟新框架直接上Spring的test即可,功能強大好用。順便吐槽一下公司:老項目難升級情有可原,但是2017年新啟動的項目,還有必要繼續(xù)jdk1.6 + weblogic + spring3.1嗎?
轉(zhuǎn)載于:https://www.cnblogs.com/dirac/p/8846905.html
總結(jié)
以上是生活随笔為你收集整理的基于JUnit4扩展老项目的UT框架且自动DI的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: goroutine中使用recover,
- 下一篇: html开始菜单,metro风格的htm