Spring 4.3 dubbo 2.7 nacos-spring-context1.1.2 nacos1.4 junit 4.13 单元测试用例执行时报错:
感觉是应为nacos和spring注解初始化顺序不同,造成的错误,未找到原因,只能用底层的办法先解决问题
java.lang.IllegalStateException: Failed to load ApplicationContext...
....
Caused by: java.lang.IllegalStateException: org.springframework.context.support.GenericApplicationContext@40ef3420 has not been refreshed yet
at org.springframework.context.support.AbstractApplicationContext.assertBeanFactoryActive(AbstractApplicationContext.java:1069)
at org.springframework.context.support.AbstractApplicationContext.getBeanNamesForType(AbstractApplicationContext.java:1183)
at com.alibaba.spring.util.BeanUtils.getBeanNames(BeanUtils.java:153)
at com.alibaba.spring.util.BeanUtils.getBeanNames(BeanUtils.java:136)
at com.alibaba.nacos.spring.util.NacosBeanUtils.isBeanDefinitionPresent(NacosBeanUtils.java:207)
at com.alibaba.nacos.spring.util.NacosBeanUtils.registerInfrastructureBeanIfAbsent(NacosBeanUtils.java:174)
at com.alibaba.nacos.spring.util.NacosBeanUtils.registerNacosApplicationContextHolder(NacosBeanUtils.java:285)
at com.alibaba.nacos.spring.util.NacosBeanUtils.registerNacosCommonBeans(NacosBeanUtils.java:372)
at com.alibaba.nacos.spring.context.annotation.NacosBeanDefinitionRegistrar.registerNacosAnnotationBeans(NacosBeanDefinitionRegistrar.java:83)
at com.alibaba.nacos.spring.context.config.xml.NacosAnnotationDrivenBeanDefinitionParser.parse(NacosAnnotationDrivenBeanDefinitionParser.java:49)
at org.springframework.beans.factory.xml.NamespaceHandlerSupport.parse(NamespaceHandlerSupport.java:74)
at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.parseCustomElement(BeanDefinitionParserDelegate.java:1435)
at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.parseCustomElement(BeanDefinitionParserDelegate.java:1419)
at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.parseBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:172)
at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.doRegisterBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:142)
at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.registerBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:94)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.registerBeanDefinitions(XmlBeanDefinitionReader.java:508)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:392)
解决思路:使用ClassPathXmlApplicationContext初始化,在用
ClassPathXmlApplicationContext.getBean(xxxService.class);的方法获取接口,在进行测试
public static ApplicationContext context = null;
public static void context() {
print("*************************** BaseTest ***************************");
context = new ClassPathXmlApplicationContext("boot-context.xml");
}
private static OfflinebakService offlinebakService;
@BeforeClass
public static void init() {
context();
offlinebakService = context.getBean(OfflinebakService.class);
}