Spring注入后反射的空指针异常
1. 异常说明
在单元测试类中,要单独测一个类中的private
方法,所以想通过反射获得此方法,解除私有限定,然后进行调用。此类为Service
其中注入了许多的Mapper
,因此在通过调用此私有方法的时候报了空指针异常的错误,错误如下:
java.lang.reflect.InvocationTargetException 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 com.suixingpay.bap.service.impl.CheckAmsPmtAndPtsFlowServiceImplTest.testInvoke(CheckAmsPmtAndPtsFlowServiceImplTest.java:78) at com.suixingpay.bap.service.impl.CheckAmsPmtAndPtsFlowServiceImplTest.insertByList(CheckAmsPmtAndPtsFlowServiceImplTest.java:69) 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.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26) at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75) at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27) at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86) at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:252) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61) at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191) at org.junit.runner.JUnitCore.run(JUnitCore.java:137) 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)Caused by: java.lang.NullPointerException at com.suixingpay.bap.service.impl.CheckAmsPmtAndPtsFlowServiceImpl.succesExitReove(CheckAmsPmtAndPtsFlowServiceImpl.java:247) ... 36 more复制代码
注意最后面是由NullPointerException
引起的。通过Debug进去以后发现是与数据库交互的Mapper
没有注入进来。
单元测试代码如下:
@Autowired private CheckAmsPmtAndPtsFlowServiceImpl checkAmsPmtAndPtsFlowServiceI; @Test public void insertByList() throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException, NoSuchFieldException { Listlist = new ArrayList<>(); Class cls = Class.forName("service.impl.CheckAmsPmtAndPtsFlowServiceImpl"); Class[] cArg = new Class[5]; cArg[0] = List.class; cArg[1] = boolean.class; cArg[2] = String.class; cArg[3] = String.class; cArg[4] = String.class; Method succesExitReove = cls.getDeclaredMethod("succesExitReove",cArg); succesExitReove.setAccessible(true); succesExitReove.invoke(checkAmsPmtAndPtsFlowServiceI,list,true,"1","1","2"); }复制代码
此处有个知识点:在调用
getDeclaredMethod()
获得指定的方法时,第二个参数是方法的传参类型,包装类和其基本类型是不一样,例如下面方法为boolean
,如果此时在上面cArg[1] = Boolean.class;
写为其包装类型的话,那么就会报错找不到此方法的错误
被反射的类中方法succesExitReove ()
如下
@Autowired private TMemAcPmtDtlPoMapper tMemAcPmtDtlPoMapper; private void succesExitReove(Listresult, boolean isFen, String sourceType, String opId, String batchId) { Iterator iteDownPtsNoCodeSuccces = result.iterator(); while (iteDownPtsNoCodeSuccces.hasNext()) { int count; if (isFen) { count = tMemAcPmtDtlPoMapper.queryCheckFlow(bapCheckResult.getOutTransId()); } else { count = tMemAcPmtDtlPoMapper.queryCheckFlow(bapCheckResult.getTransId()); } bapCheckResult.setPmtCount(count); if (count > 0) { iteDownPtsNoCodeSuccces.remove(); } } }复制代码
此处在Debug时发现tMemAcPmtDtlPoMapper
为空。
2. 解决办法
在单元测试类中
@Autowired
要用的Mapper,然后通过反射获得上面方法要用到Mapper
的变量,为其赋值即可
单元测试代码如下
@Autowired private CheckAmsPmtAndPtsFlowServiceImpl checkAmsPmtAndPtsFlowServiceI; --在单元测试类中进行依赖注入Mapper @Autowired private TMemAcPmtDtlPoMapper tMemAcPmtDtlPoMapper; @Test public void insertByList() throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException, NoSuchFieldException { Listlist = new ArrayList<>(); Class cls = Class.forName("service.impl.CheckAmsPmtAndPtsFlowServiceImpl"); Class[] cArg = new Class[5]; cArg[0] = List.class; cArg[1] = boolean.class; cArg[2] = String.class; cArg[3] = String.class; cArg[4] = String.class; Field a1 = cls.getDeclaredField("tMemAcPmtDtlPoMapper"); Method succesExitReove = cls.getDeclaredMethod("succesExitReove",cArg); succesExitReove.setAccessible(true); --在此为反射的类进行赋值Mapper Field a1 = cls.getDeclaredField("tMemAcPmtDtlPoMapper"); a1.setAccessible(true); a1.set(checkAmsPmtAndPtsFlowServiceI,tMemAcPmtDtlPoMapper); succesExitReove.invoke(checkAmsPmtAndPtsFlowServiceI,list,true,"1","1","2"); }复制代码