Scala中的PowerMock:getPackage时的java.lang.NullPointerException

时间:2015-10-28 11:15:59

标签: scala testing junit mockito powermock

这段代码只是一个类的getPackage和getName(还没有使用任何模拟技术),但它失败了。

之前有人看到过这个问题吗?

代码:

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]   ...

1 个答案:

答案 0 :(得分:0)

我想我找到了答案,但我不明白为什么会有效?

答案:在sbt内部,首先应该设置fork:= true"然后一切都会正常工作。

问题可能与进程与线程问题有关。