Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.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
Spring 测试包中的Autowire java类-Junit_Spring_Junit_Autowired - Fatal编程技术网

Spring 测试包中的Autowire java类-Junit

Spring 测试包中的Autowire java类-Junit,spring,junit,autowired,Spring,Junit,Autowired,我想在运行时为我的junit测试用例创建测试数据。作为该活动的一部分,我必须选择编写一组数据装置和一个围绕它的实现,它将创建/删除/更新测试数据。这些将从每个Junit类@before和@After方法中调用-@before来设置测试数据,@After来分解测试数据 我在“test”包中编写了所有这些实现,并将主要类标记为@component和@autowired这些新类。但是,当我运行junit测试时,它无法创建这些新类的实例,因此autowire似乎无法工作 除了在test-config.x

我想在运行时为我的junit测试用例创建测试数据。作为该活动的一部分,我必须选择编写一组数据装置和一个围绕它的实现,它将创建/删除/更新测试数据。这些将从每个Junit类@before和@After方法中调用-@before来设置测试数据,@After来分解测试数据

我在“test”包中编写了所有这些实现,并将主要类标记为@component和@autowired这些新类。但是,当我运行junit测试时,它无法创建这些新类的实例,因此autowire似乎无法工作

除了在test-config.xml中添加以下配置之外,我不确定在autowire之前还要做什么

<context:component-scan base-package="test">
</context:component-scan>
//Myservice类的Junit测试:

//文件位置:src/test/java/com/abc/pqr

 package com.abc.pqr;
 @RunWith(SpringJUnit4ClassRunner.class)
 @ContextConfiguration(locations={"classpath:/META-INF/spring/test-Config.xml"})
 public class MyServiceTest {
     @Autowired  
     private ABC abc;
 }
//用于创建/删除/更新测试数据的数据装置

//文件位置:src/test/java/com/abc/pqr/datafixtures

 package com.abc.pqr.datafixtures
 @Component("abc") 
 public class ABC{
     public void create(){
     } 
     public void remove(){
     } 
     public void update(){
     }
 }

我认为问题是由于您没有在spring测试上下文中扫描正确的包。
test Config.xml
文件应如下所示:

<context:annotation-config />
<context:component-scan base-package="com.abc.pqr.datafixtures" /> 


希望这有帮助。

您是否正在将测试上下文加载到JUnit测试类(
@ContextConfiguration(locations={/pathToContext/test config.xml})
)?是的,我正在将测试上下文加载到Junit测试类中。您能告诉我您是如何实现这两个类的吗?请查找我更新的问题,并提供更多信息谢谢您的回答,因为我说过“MyServiceTest”和“ABC”是测试包中的类,而不是“main”代码中的类,因此基本包将查找“datafixtures”包位于src/main/java位置内,但它不在src/test/java中point@Vini据我所知,您不能告诉spring扫描源文件夹(test或main)。您只能指定要扫描的包。但通常情况下,您的
test Config.xml
上下文应该能够扫描主文件夹和测试源文件夹中的包。因此,将
base package
属性设置为
com.abc.pqr
应该可以解决您的问题。请告诉我这是否有效。
<context:annotation-config />
<context:component-scan base-package="com.abc.pqr.datafixtures" />