Scala中的PowerMock:getPackage时出现java.lang.NullPointerException [英] PowerMock in Scala: java.lang.NullPointerException when getPackage

查看:137
本文介绍了Scala中的PowerMock:getPackage时出现java.lang.NullPointerException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这段代码只是一个类的getPackage和getName(尚未使用任何模拟技术),但失败了.

This code just getPackage and getName of a class (not use any mock techniques yet), but it failed.

有人见过这个问题吗?

代码:

import mai.MyScala1
import org.junit.Test
import org.junit.runner.RunWith
import org.powermock.modules.junit4.PowerMockRunner
import org.scalatest.junit.JUnitSuite

@RunWith(classOf[PowerMockRunner])
class MyTest extends JUnitSuite {

  @Test def test1() {
    classOf[MyScala1].getPackage          // this one returns null
    classOf[MyScala1].getPackage.getName  // raise java.lang.NullPointerException    
  }
}

错误日志:

[info] - test1 *** FAILED ***
[info]   java.lang.NullPointerException:
[info]   at org.apache.tmp.MyTest.test1(MyTest.scala:15)
[info]   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[info]   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
[info]   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
[info]   at java.lang.reflect.Method.invoke(Method.java:497)
[info]   at org.junit.internal.runners.TestMethod.invoke(TestMethod.java:68)
[info]   at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:310)
[info]   at org.junit.internal.runners.MethodRoadie$2.run(MethodRoadie.java:89)
[info]   at org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:97)
[info]   at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.executeTest(PowerMockJUnit44RunnerDelegateImpl.java:294)
[info]   ...

推荐答案

我想找到了答案,但是我不明白为什么会这样?

I think I found the answer, but I don't understand why it works?

答案:在sbt中,我们应该首先设置fork:= true",然后一切正常.

Answer: Inside sbt, we should first "set fork := true", then everything will work fine.

该问题可能与进程与线程问题有关.

The problem might be related to process vs thread problem.

这篇关于Scala中的PowerMock:getPackage时出现java.lang.NullPointerException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆