Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

junit单元测试报错 GenericWebApplicationContext has not been refreshed yet #273

Open
yanming1 opened this issue Oct 14, 2021 · 3 comments

Comments

@yanming1
Copy link

Issue Description

Type: bug report

junit单元测试报错

Caused by: java.lang.IllegalStateException: org.springframework.web.context.support.GenericWebApplicationContext@3fb1549b has not been refreshed yet
at org.springframework.context.support.AbstractApplicationContext.assertBeanFactoryActive(AbstractApplicationContext.java:1141)
at org.springframework.context.support.AbstractApplicationContext.getBeanNamesForType(AbstractApplicationContext.java:1293)
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:1391)
at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.parseCustomElement(BeanDefinitionParserDelegate.java:1371)
at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.parseBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:179)
at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.doRegisterBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:149)
at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.registerBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:96)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.registerBeanDefinitions(XmlBeanDefinitionReader.java:511)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:391)
... 37 more

Describe what you expected to happen

How to reproduce it (as minimally and precisely as possible)

1.spring.xml 中 import nacos配置

     <import resource="nacos-config.xml"/>
  1. nacos-config.xml配置如下
    <beans xmlns="http://www.springframework.org/schema/beans" xmlns:nacos="http://nacos.io/schema/nacos"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:schemaLocation="http://nacos.io/schema/nacos http://nacos.io/schema/nacos.xsd
   http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
 <!--nacos配置,这里是使用配置文件的方式,这只是其中的一种方式-->
<!--开启注解-->
 <nacos:annotation-driven></nacos:annotation-driven>
 <!--指定nacos配置地址-->
 <nacos:global-properties server-addr="x.x.x.x:8848"/>
 <!--指定dataId,group-id, 是否是自动刷新-->
 <nacos:property-source data-id="projectName" group-id="DEFAULT_GROUP" auto-refreshed="true"/>
3. 启动单元测试 @RunWith(SpringRunner.class) @SpringJUnitWebConfig @ContextConfiguration(locations = {"classpath:spring.xml" }) @activeprofiles("dev") public class XTest{ @Autowired private XService xService;
@Test
public void testCreate(){
    xService.doBusiness();
}

}
随便自动注入个服务,然后test方法中调用自动注入服务方法即可

Tell us your environment

spring版本5.3.6
nacos-spring-context版本1.1.0

Anything else we need to know?

对比了一下spring事务注解解析的代码发现不同之处在于
NacosBeanUtils中
public static void registerInfrastructureBeanIfAbsent(BeanDefinitionRegistry registry,
String beanName, Class beanClass, Object... constructorArgs) { if (!isBeanDefinitionPresent(registry, beanName, beanClass) && !registry.containsBeanDefinition(beanName)) { registerInfrastructureBean(registry, beanName, beanClass, constructorArgs); } } 如上方法在判断是否已注册过bean的时候多了isBeanDefinitionPresent(registry, beanName, beanClass)的判断调用, 而其实现最终会调用AbstractApplicationContext的方法 public String[] getBeanNamesForType(@nullable Class type, boolean includeNonSingletons, boolean allowEagerInit) {
assertBeanFactoryActive();
return getBeanFactory().getBeanNamesForType(type, includeNonSingletons, allowEagerInit);
}
此方法中assertBeanFactoryActive()抛出异常,ApplicationContext此时还未执行refresh方法并未激活

@ChubChen
Copy link

我也遇到这个问题。 请问最后是怎么解决的

1 similar comment
@wushaogen
Copy link

我也遇到这个问题。 请问最后是怎么解决的

@gingerjiang
Copy link

gingerjiang commented May 12, 2024

感觉是应为nacos和spring注解初始化顺序不同,造成的错误,未找到原因,只能用底层的办法先解决问题,先这么测试吧
`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);
}`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants