nacos-group / nacos-spring-project

Nacos ECO Project for Spring Framework
https://nacos.io/
Apache License 2.0
754 stars 309 forks source link

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

Open wushaogen opened 8 months ago

wushaogen commented 8 months ago

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"/>

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">

3. 启动单元测试 https://github.com/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