Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/401.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 测试中加载了错误的spring应用程序上下文_Java_Spring_Unit Testing_Testing - Fatal编程技术网

Java 测试中加载了错误的spring应用程序上下文

Java 测试中加载了错误的spring应用程序上下文,java,spring,unit-testing,testing,Java,Spring,Unit Testing,Testing,我有我的Spring应用程序,其中应用程序上下文被正确加载并工作。但是,当我尝试在测试中加载它时,加载的应用程序上下文不包含配置文件中声明的任何bean @Test public void testInsertMapitPoint() { ApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "classpath:*/application-ds-context.xml","c

我有我的Spring应用程序,其中应用程序上下文被正确加载并工作。但是,当我尝试在测试中加载它时,加载的应用程序上下文不包含配置文件中声明的任何bean

@Test
public void testInsertMapitPoint() {
    ApplicationContext context = new ClassPathXmlApplicationContext(new String[] {     "classpath:*/application-ds-context.xml","classpath:*/application-dao-context.xml" });
    System.out.println("Bean Names:");
    for (String beanName : context.getBeanDefinitionNames()) {
        System.out.println(beanName);
    }
}
显示的测试不会在上下文中显示任何bean。有什么帮助吗?

有这样的模式

classpath:*/application-ds-context.xml
       // ^ this guy
Spring将加载与该模式匹配的任何文件。任何意味着它可能找不到/加载任何。在本例中,它将查找任何一级包中名为
application ds context.xml
的文件

例如,它会发现

/classpath-root
    /first
        application-ds-context.xml
但它找不到

/classpath-root
    application-ds-context.xml


如果您看到的是这种行为,那么您的类路径不包含这样的文件。查看您是如何构建测试应用程序的。

Aha。我想他的意思是
classpath*:/application ds context.xml
@chrylis看看
pathmatchingrourcepatternsolver
。在这种情况下,
*
在你的提议和OP的提议中的用法基本相同。@chrylis抱歉,不,不完全一样。我想说的是,它们都是通配符。但是是的,
classpath*:/application ds context.xml
会在根目录下查找它,而不是在一个包级别下查找。@chrylis但是,如果没有找到任何东西,它不会抱怨。完全理解,但我怀疑这是它找不到任何bean的原因。
/classpath-root
    /first
        /second
            application-ds-context.xml