标签:通过 测试 mode ble for 注入 注解 exec 解决办法
java.lang.NullPointerException
at com.example.new_oa.identity.service.impl.IdentityServiceImpl.getAllDepts(IdentityServiceImpl.java:51)
at com.example.new_oa.NewOaApplicationTests.initDb(NewOaApplicationTests.java:31)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.springframework.test.context.junit4.statements.RunBeforeTestExecutionCallbacks.evaluate(RunBeforeTestExecutionCallbacks.java:74)
at org.springframework.test.context.junit4.statements.RunAfterTestExecutionCallbacks.evaluate(RunAfterTestExecutionCallbacks.java:84)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
笔者在进行开发测试的时候,代码中是通过注解将业务层对象放入spring容器的,而在测试时,时通过手动new的业务层对象,去调用的方法,但是在方法的内部却出现了空指针的情况,这是怎么回事呢?后来发现,在我的业务层方法中,是依赖了持久层对象的,而持久层对象又是通过注解引入的,前面由于业务层对象是通过手动new出来的,所以该对象中并没有把持久层对象注入进去,所以出现了空指针的情况。所以将手动的new的对象,修改为从容器中获取就好了。
标签:通过 测试 mode ble for 注入 注解 exec 解决办法
原文地址:https://blog.51cto.com/14423206/2415452