问答详情
源自:4-1 使用集成测试Service逻辑

测试exposerSeckillUrl出现@Service 下@Autowired注入失败的 出现NullPointerException!!百度找不到办法

@Service
public class SeckillServiceImpl implements SeckillService {
   private Logger logger = LoggerFactory.getLogger(this.getClass());
   //注入Service依赖,
   @Autowired
   private SeckillDao seckillDao; //提示 Cannot autowired ,no beans of 'seckillDao'
   @Autowired
   private SuccessKilledDao successKilledDao; //提示 Cannot autowired ,no beans of 'successKilledDao'

java.lang.NullPointerException

at org.seckill.service.impl.SeckillServiceImpl.exportSeckillUrl(SeckillServiceImpl.java:53)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

at java.lang.reflect.Method.invoke(Method.java:606)

at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)

at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:201)

at com.sun.proxy.$Proxy19.exportSeckillUrl(Unknown Source)

at org.seckill.service.SeckillServiceTest.testexportSeckillUrl(SeckillServiceTest.java:43)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

at java.lang.reflect.Method.invoke(Method.java:606)

at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)

at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)

at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)

at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)

at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:73)

at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:82)

at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:73)

at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)

at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:224)

at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:83)

at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)

at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)

at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)

at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)

at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)

at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)

at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:68)

at org.junit.runners.ParentRunner.run(ParentRunner.java:309)

at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:163)

at org.junit.runner.JUnitCore.run(JUnitCore.java:160)

at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)

at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)

at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)

at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)


提问者:从入门到衫裤跑路 2018-11-22 14:31

个回答

  • qq_戎码一生_03590608
    2019-02-17 02:16:33

    都是泪,搞了4个小时了快,@Autowired注入失败   seckillDao successKilledDao 有红色的下划线

    我的解决办法是把idea换成2017的版本就没有红色下划线了,顺带还解决了2018版本idea创建接口测试实例不能选

    中并自动生成测试方法。

    NullPointerException 空指针异常  我本以为也是版本问题,不过重装后没有解决,然后我我报错是这一句

    if (nowTime.getTime() < startTime.getTime() || nowTime.getTime() > endTime.getTime()){

    然后改成 if(1 == 0)就通过了测试,所以我又改回来并把断点打在这一步然后debug发现开始时间没有值即

    startTime = null 然后就发现是seckill实体类seckillId写成 seckill 和 startTime 写成starTime !连累setget方法也

    错了 最后把这个实体类重写一遍写对就可以了,开始时间就有值了也就没有空指针异常引起的报错了测试也就通过

    了!所以空指针就debug跑一下应该就能发现是哪儿出错了,idea的debug好好用!如果我的解决方法可以帮到你

    麻烦点个赞!虽然我有点啰嗦


  • Y_XL
    2018-11-26 22:36:09

    spring-service.xml配置不对吧,或者测试类有问题