【问题标题】:Application context is coming as null while running spring integration test with @ContextConfiguration使用 @ContextConfiguration 运行 spring 集成测试时,应用程序上下文为 null
【发布时间】:2015-11-19 16:03:04
【问题描述】:

我正在尝试使用 spring 编写集成测试。下面是测试类

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath*:**/context*.xml"})
public class MyFirstTestClass {

      @AutoWired
      private ApplicationContext applicationContext;

      @Test
      public void testApplicationContext {
        applicationContext.getName();
      }    
}

当我从 maven 运行测试时,我得到了 NullPointerException,因为 applicationContextnull。我无法理解原因。同样在日志中我也没有看到任何错误。我尝试将配置 <context:annotation-config/> 放入其中一个应用程序上下文文件中。我仍然遇到同样的错误。

当我使用 classpath*:**/context*.xml 手动创建上下文时,它可以工作。但我相信当我使用上下文配置时,它不会加载应用程序上下文文件。我也没有收到任何错误。 请推荐


好的,我设法加载了应用程序上下文。但现在我得到了NoSuchBeanDefinition 异常。

豆文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
                           http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="messageService"
          class="com.mycompany.app.MessageService">
    </bean>    
</beans>                 

测试类

package com.mycompany.app;
import org.junit.runner.RunWith;
import static org.junit.Assert.*;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import junit.framework.TestSuite;
@ContextConfiguration(locations={"classpath*:*/AppTest.context.xml"})
@RunWith(SpringJUnit4ClassRunner.class)
public class AppTest 
{
    @Autowired
    private ApplicationContext applicationContext;


    public AppTest(  )
    {

    }

    @Test
    public void testApp(){

        assertNotNull(applicationContext);
        for(String str : applicationContext.getBeanDefinitionNames()){
            System.out.println("Bean feifniaio count"+str); 
        }
        applicationContext.getBean("messageService");
    }
}

执行测试后的日志

信息:令人耳目一新 org.springframework.context.support.GenericApplicationContext@196751b: 启动日期 [Fri Nov 20 00:00:11 IST 2015];上下文层次的根 豆角 countorg.springframework.context.annotation.internalConfigurationAnnotationProcessor 豆角 countorg.springframework.context.annotation.internalAutowiredAnnotationProcessor 豆角 countorg.springframework.context.annotation.internalRequiredAnnotationProcessor 豆角 countorg.springframework.context.annotation.internalCommonAnnotationProcessor 豆角 countorg.springframework.context.event.internalEventListenerProcessor 豆角 countorg.springframework.context.event.internalEventListenerFactory 豆角 countorg.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor 豆角 countorg.springframework.context.annotation.ConfigurationClassPostProcessor.enhancedConfigurationProcessor

我看不到消息服务 bean。可能是什么问题?

【问题讨论】:

  • 你的context*.xml的位置在哪里?
  • 它在类路径中。当我运行构建时,它会将 context*.xml 文件复制到 test-classes 目录中,在 Maven 构建中我也可以在类路径中看到这个目录。
  • context*.xml 文件在哪里?而且,您如何运行测试?
  • 我正在使用 maven build 运行我的测试。在编译过程中,context*.xml 被复制到 test-classes 目录中。
  • 看看here。您可能使用的 Ant 样式模式并不完全正确。

标签: java spring maven testing


【解决方案1】:

最后我发现了这个问题。我的上下文文件有问题。我在 test 的资源文件夹中放置了一个空的上下文文件,该文件被 maven 复制到目标文件夹中。并且实际的上下文文件没有被复制到目标中。由于上下文文件为空,上下文被初始化,但由于它没有 bean 定义,所以它抛出 NoSuchBeanException。现在我应该强调 ContextConfiguration 没有抛出任何错误能够找到 location 属性中指定的上下文文件。我认为理想情况下应该这样做。我尝试放置一些在位置属性中不存在并且没有引发错误的虚拟文件路径。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-25
    • 1970-01-01
    • 1970-01-01
    • 2013-01-18
    相关资源
    最近更新 更多