Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/342.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.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_Junit - Fatal编程技术网

Java 使用空字段自动连接的Spring单元测试对象

Java 使用空字段自动连接的Spring单元测试对象,java,spring,junit,Java,Spring,Junit,我正试图在SpringServlet中为rest服务创建单元测试。但当控制器对象由@autowire创建时,其所有@autowired字段都为空 我的测试类如下所示,使用SpringJUnit运行程序和上下文配置集 @ContextConfiguration(locations = "ExampleRestControllerTest-context.xml") @RunWith(SpringJUnit4ClassRunner.class) public class ExampleRestCon

我正试图在SpringServlet中为rest服务创建单元测试。但当控制器对象由@autowire创建时,其所有@autowired字段都为空

我的测试类如下所示,使用SpringJUnit运行程序和上下文配置集

@ContextConfiguration(locations = "ExampleRestControllerTest-context.xml")
@RunWith(SpringJUnit4ClassRunner.class)
public class ExampleRestControllerTest {

    @Autowired
    private BaseService mockExampleService;

    @Autowired
    private ExampleRestController testExampleRestController;
ExampleRestControllerTest-context.xml设置要模拟的服务,并将模拟对象注入控制器

<context:annotation-config/>

<import resource="classpath*:example-servlet.xml"/>

<bean id="mockExampleService" class="org.easymock.EasyMock" factory-method="createMock">
    <constructor-arg index="0" value="za.co.myexample.example.services.BaseService"/>
</bean>
<bean id="testExampleRestController" class="za.co.myexample.example.rest.controller.ExampleRestController">
    <property name="exampleService" ref="mockExampleService"/>
</bean>
(及其实现类)

当我在Weblogic服务器中运行正确的代码时,字段会正确填充。更重要的是,如果我直接在测试类中添加这些字段,这些字段也会正确地@autowired。因此,我可以在测试类中@autowire这些对象,然后将它们直接设置到我的控制器中。但这不是正确的做法


所以问题是,当自动连线对象直接在同一测试类中自动连线时,或者如果我在Weblogic服务器上正常运行代码,为什么我的测试类中自动连线对象的自动连线字段为空。

在您的测试类中,@autowired对象由于上下文配置而为空,所以将其更改为:

@ContextConfiguration(locations = "ExampleRestControllerTest-context.xml")

ExampleRestControllerTest中

所以问题是,为什么自动连线的字段是自动连线的 当我的测试类直接自动连接这些对象时,该类中的对象为null 在同一测试类中,或者如果我在Weblogic上正常运行代码 服务器


在自动连接对象的构造函数中,它们必须为null。您可以尝试在autowired对象中创建
@PostConstruct
方法,在该方法中,autowired对象必须不为null

,将ContextConfiguration更改为仍然具有完全相同的效果。示例RestController通过
@Autowire
创建,但其字段由
@Autowired-protected-RESTCodeMapper-RESTCodeMapper创建@自连线受保护的BaseService示例服务@自连线保护的Jaxb2Marshaller Jaxb2Marshaller
BaseRestController使用默认构造函数和服务的setter,因此可以将其注入。此问题是由于bean初始化顺序不正确造成的。
examplerestcontrollertestcontext.xml中定义的bean在
exampleservlet.xml中的bean之前初始化。尝试添加
SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(此)调用autowired Bean之前,在
ExampleRestControllerTest
的内部,如您所建议,我尝试添加
SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(此)
为了
ExampleRestControllerTest
,我还尝试了
SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(testExampleRestController)并尝试
SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(此)在控制器本身内。我已尝试删除导入并将bean移动到context.xml,以便在控制器之前初始化bean。所有这些事情仍然有完全相同的结果;控制程序的字段中仍然有空值。我同意,如果spring没有正确实例化bean,那么这应该是有效的。但是我认为使用
processInjectionBasedOnCurrentContext()
具有相同的效果,并且遇到了相同的问题,因为Spring出于某种原因将bean实例化为null。或者至少我是这么想的是,这真的很奇怪,因为我有非常相似的场景。我发现你的和我的配置还有一个不同之处。我不相信它能解决问题,但你可以试试<代码>
标记在配置文件中丢失。答案也许能更好地解释它
@Controller
public abstract class BaseRestController {

    private static Logger LOGGER = Logger.getLogger(BaseRestController.class);
    protected final String HEADERS = "Content-Type=application/json,application/xml";
    @Autowired
    protected RESTCodeMapper restCodeMapper;

    @Autowired
    protected BaseService exampleService;

    @Autowired
    protected Jaxb2Marshaller jaxb2Marshaller;
@Controller
@RequestMapping("/example")
public class ExampleRestController extends BaseRestController {
@ContextConfiguration(locations = "ExampleRestControllerTest-context.xml")
@ContextConfiguration(locations = "classpath:/ExampleRestControllerTest-context.xml")