/** * 获得当前测试类spring容器中名称为beanname的spring bean * * @param beanName * @return */ public static Object getBeanByName(String beanname) { BeanFactory factory = (BeanFactory) TestedObject.getSpringBeanFactory(); if (factory == null) { throw new RuntimeException( "can't find SpringApplicationContext for tested class:" + TestedObject.currTestedClazzName()); } else { Object bean = factory.getBean(beanname); return bean; } }
/** * 释放测试类的spring容器 * * @param springContext AbstractApplicationContext实例,这里定义为Object是方便其它模块脱离spring依赖 */ public static void closeSpringContext(Object beanFactory) { if (beanFactory == null) { return; } if (beanFactory instanceof JTesterBeanFactory) { ((JTesterBeanFactory) beanFactory).destroySingletons(); JTesterLogger.warn("close spring context for class:" + TestedObject.currTestedClazzName()); } else { String error = String.format( "there must be something error, the type[%s] object isn't a spring context.", beanFactory.getClass().getName()); throw new RuntimeException(error); } }