当前位置:   article > 正文

PowerMockito使用中一个需要注意的点_powermockito 空指针

powermockito 空指针

问题现象

最近在使用PowerMockito进行单元测试时,遇到了一个问题,代码如下:
原方法签名

ContactInfo getInfoById(long id);
  • 1

测试用例中

PowerMockito.when(mapper.getInfoById(any())).thenReturn(new ContactInfo());
  • 1

这是要mock一个方法返回一个ContactInfo对象,非常简单,但是在运行测试用例的时候,出现了如下错误:

java.lang.NullPointerException
(和业务相关的已经隐藏,报错的就是上面一行)
	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:59)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56)
	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.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
	at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
	at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

mapper对象的代理已经生成,但空指针表示有地方获取为空了。

排查结果

经过大量排查,发现需要mock的函数参数为long类型,使用any()做mock的时候会爆出空指针异常,需要替换为基本类型的anyLong()

原因

any()返回的是一个null值,而anyLong()返回的是0。
在mock方法时,传入为原始对象要执行拆箱操作,any()返回了null引发空指针。
在Oracle的官方文档里可以看到自动拆箱的两种触发情况描述如下:[1]

The Java compiler applies unboxing when an object of a wrapper class is:

  1. Passed as a parameter to a method that expects a value of the corresponding primitive type.
  2. Assigned to a variable of the corresponding primitive type.

1.作为参数传递给一个期望获得原始类型的方法
2.对一个原始类型的对象赋值的时候
本例中应该是属于第一种,null在调用longValue()时出现空指针异常

引用
[1]https://docs.oracle.com/javase/tutorial/java/data/autoboxing.html

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号