李民11 2014-04-11 12:26 采纳率: 0%
浏览 2159

一个Spring框架的测试问题,总是调不通,求指教。

package com.chh;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.testng.AssertJUnit;
import org.testng.annotations.Test;
@Test
@ContextConfiguration("/beans.xml")
public class NewTest {

@Autowired
private ServiceImpl service;
@Test
public void getMessage(){
    String msg = service.getMessage("abc");
    AssertJUnit.assertEquals("abc", msg);

}

}

上面是我的测试代码,beans.xml放在src目录下。 service变量采用自动装配的方式引入。但是总是报错误。错误如下。
java.lang.NullPointerException
at com.chh.NewTest.getMessage(NewTest.java:15)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
说的是service引用时一个空指针,也就是说service对象在容器中就没有。
现在怀疑Spring容器就没有正常启动,也就是说@ContextConfiguration("/beans.xml") 就没有启动容器。
不过一直找不到关键点所在,请大神指教我错在哪了?

  • 写回答

1条回答 默认 最新

  • gointosusnet 2014-04-12 01:04
    关注

    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(locations = {"classpath:/applicationContext.xml"})
    public class CustomerServiceTest {
    @Autowired
    private CustomerService customerService;
    }

    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器