可以jmockit和robolectric共存? [英] can jmockit and robolectric coexist?

查看:774
本文介绍了可以jmockit和robolectric共存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现使用Robolectric替换的android.jar废止方法同时还采用jMockit来模拟一个Android类(片段,在我的情况)一个单元测试。不过,我似乎无法得到它的工作。如果我注释测试类 @RunWith(RobolectricTestRunner.class),我得到:

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没有正确初始化;检查在类路径jmockit.jar precedes的junit.jar(如果使用JUnit;如果没有,查看文档)

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)

如果我用 @RunWith(JMockit.class)或没有 @RunWith ,我得到存根!例外情况。

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

目前,我的类路径中有东西在下面的顺序:robolectric,jmockit,JUnit中,机器人。

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

人在那里能够得到jmockit和robolectric发挥得很好?

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

推荐答案

这应该是可能的。我没有测试过这一点,但你可以创建自己的测试运行。

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

看看的源JMockit和Robolectric测试运行:

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

  • <一个href="https://github.com/pivotal/robolectric/blob/master/src/main/java/com/xtremelabs/robolectric/RobolectricTestRunner.java"相对=nofollow> Robolectric
  • <一个href="https://$c$c.google.com/p/jmockit/source/browse/trunk/main/src/mockit/integration/junit4/JMockit.java"相对=nofollow> JMockit
  • Robolectric
  • JMockit

在两个Robolectric一个是要复杂得多,所以我们不希望重复这个功能。该JMockit测试运行是相当简单的。它应努力扩大RobolectricTestRunner和包括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);
   }
}

这篇关于可以jmockit和robolectric共存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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