can jmockit and robolectric coexist?

那年仲夏 提交于 2019-12-13 11:49:27

问题


I'm trying to implement a unit test using Robolectric to replace the stubbed methods in android.jar while also using jMockit to mock an Android class (Fragment, in my case). However, I can't seem to get it to work. If I annotate the test class with @RunWith(RobolectricTestRunner.class), I get:

java.lang.IllegalStateException: JMockit wasn't properly initialized; check that jmockit.jar precedes junit.jar in the classpath (if using JUnit; if not, check the documentation)

If I use @RunWith(JMockit.class) or no @RunWith, I get "Stub!" exceptions.

At the moment, my classpath has things in the following order: robolectric, jmockit, junit, android.

Anybody out there been able to get jmockit and robolectric to play well together?


回答1:


This should be possible. I haven't tested this, but you can create your own test runner.

Take a look at the source for the JMockit and Robolectric test runners:

  • Robolectric
  • JMockit

Of the two the Robolectric one is much more complicated, so we don't want to duplicate that functionality. The JMockit test runner is fairly simple. It should work to extend the RobolectricTestRunner and include the JMockit functionality.

import mockit.internal.startup.*;
class MyTestRunner extends RobolectricTestRunner {

   static { Startup.initializeIfNeeded(); }

   /**
    * Constructs a new instance of the test runner.
    *
    * @throws InitializationError if the test class is malformed
    */
   public MyTestRunner(Class<?> testClass) throws InitializationError
   {
      super(testClass);
   }
}



回答2:


At version Version 1.8 (Apr 27, 2014) JMockit can work in conjunction with Robolectric.

JMockit now works fine with the Robolectric Android testing tool (tested with Robolectric 2.2 and 2.3).

http://jmockit.org/changes.html



来源:https://stackoverflow.com/questions/14780965/can-jmockit-and-robolectric-coexist

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!