Spring项目用JUnit调试时出现错误 Failed to load ApplicationContext 的解决方法(不一定适合所有人)

之前在Spring中加载配置文件**.xml文件的时候,我是写在一个main()方法里面

public class SoundMain {
    /* 基于XML的配置实现Bean自动化装配 */
    public static void  main(String[] args){
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("/sound.xml");
        //CompactDisc sgt = context.getBean(SgtPeppers.class);
        //sgt.play();
        CDPlayer cdPlayer = context.getBean("CdPlayer",CDPlayer.class);
        cdPlayer.play();
    }
}

但是我最近在 JUnit测试的时候,相同的代码在@Test注解的方法里面就报错了,代码如示

@RunWith(SpringJUnit4ClassRunner.class)
public class ExtraTest {
    @Test
    public void TestEncore(){
        ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("/spring-config.xml");
        ExtraClass ec = ctx.getBean("extraClass", ExtraClass.class);
        ec.out();
    }
}

**.xml的路劲是没有问题的,报的错是:Neither GenericXmlContextLoader nor AnnotationConfigContextLoader was able to load an ApplicationContext from [MergedContextConfiguration@81560cc testClass = ExtraTest, locations = '{}', classes = '{}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{}', contextCustomizers = set[[empty]], contextLoader = 'org.springframework.test.context.support.DelegatingSmartContextLoader', parent = [null]].

And java.lang.IllegalStateException: Failed to load ApplicationContext

最后我发现是因为我加了 @RunWith(SpringJUnit4ClassRunner.class)注解的缘故:这个注解会在测试开始的时候自动创建Spring的应用上下文,在用了这个注解的情况下,可以用@ContextConfiguration(locations="classpath:/spring-config.xml")

去掉@RunWith(...)注解的话,前面的代码也是可以运行的。

因为用new ClassPathXmlApplicationContext()它也会获取Spring的上下文环境,所以会有冲突。下面是用注解的形式的代码

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:/spring-config.xml")
public class ExtraTest {
    @Resource
    private ExtraClass ec;
    @Test
    public void TestEncore(){ ;
        ec.out();
    }
}

 

 

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
当maven打包测试不通过,报错信息为“Failed to load ApplicationContext,通常是因为应用程序上下文初始化失败导致的。这可能是由于以下原因之一引起的: 1.应用程序上下文文件(如applicationContext.xml)中的bean配置错误或无法加载。 2.测试类中的依赖项无法正确加载或配置。 为了解决这个问题,可以尝试以下几个步骤: 1.检查应用程序上下文文件(如applicationContext.xml)中的bean配置是否正确,并确保所有依赖项都已正确加载和配置。 2.检查测试类中的依赖项是否正确加载和配置,并确保它们与应用程序上下文文件中的bean配置相匹配。 3.尝试使用调试器来诊断问题,以确定哪个bean或依赖项导致了应用程序上下文初始化失败。 以下是一个可能的解决方案示例: ```xml <!-- applicationContext.xml --> <bean id="myBean" class="com.example.MyBean"> <property name="myProperty" value="myValue" /> </bean> ``` ```java // MyBean.java public class MyBean { private String myProperty; public void setMyProperty(String myProperty) { this.myProperty = myProperty; } public String getMyProperty() { return myProperty; } } // MyBeanTest.java @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = { "classpath:applicationContext.xml" }) public class MyBeanTest { @Autowired private MyBean myBean; @Test public void testMyBean() { assertNotNull(myBean); assertEquals("myValue", myBean.getMyProperty()); } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值