如何使用PowerMock在Android的项目? [英] How to use PowerMock in Android projects?

查看:145
本文介绍了如何使用PowerMock在Android的项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个新的Andr​​oid测试项目。我下载powermock-mockito-junit-1-1.5.zip从<一个href="https://$c$c.google.com/p/powermock/downloads/list">https://$c$c.google.com/p/powermock/downloads/list.我添加了所有的库测试项目的文件夹。测试类是一个非常简单的对象:

I created a new Android test project. I downloaded powermock-mockito-junit-1-1.5.zip from https://code.google.com/p/powermock/downloads/list. I added all of the libraries to the test project's libs folder. The test class is a very simple object:

package com.test.test;

import org.junit.runner.RunWith;
import org.powermock.modules.junit4.PowerMockRunner;

import android.util.Log;

@RunWith(PowerMockRunner.class)
public class TestTestAndroid {

    public void testRuns() {
        Log.e("test", "Test case is called");
    }
}

于是,我试着从Eclipse中运行该项目,或进行命令行项目。我得到了同样的错误:

Then, I try running the project from Eclipse, or making the project from the command line. I get the same error:

转换到的Dalvik格式失败:无法执行DEX:多DEX文件定义Lorg / hamcrest /说明;

事实证明,无论是的junit-4.8.2.jar 的Mockito-全1.9.5.jar 定义 org.hamcrest.Description 。我必须包含的Mockito罐子,原因很明显 - 我需要的Mockito。不同版本的JUnit是由机器人提供的,但它是一个老版本,不包括 @RunWith 注释。

As it turns out, both junit-4.8.2.jar and mockito-all-1.9.5.jar define org.hamcrest.Description. I must include the Mockito jar for obvious reasons - I need Mockito. A different version of JUnit is provided by Android, but it is an old version that does not include the @RunWith annotation.

有人能回答如何使用powermock和的Mockito在Android项目,没有冲突 org.hamcrest.Description 问题?

Can someone answer how to use powermock and mockito in an Android project, without the conflicting org.hamcrest.Description problem?

推荐答案

对不起,您不能在Dalvik虚拟机使用PowerMock。

Sorry, you can't use PowerMock in the Dalvik VM.

PowerMock的工作原理是在一个定制的ClassLoader,它使用了Javassist 运行测试修改类的字节code。这工作好于正常的JVM,而是Dalvik的字节code和类格式是不同的,所以这种方法是行不通的。 PowerMock需要被重写,以使用 Dexmaker 的,代替了Javassist - 这将是决定性的不平凡的,并我看不出在PowerMock 问题列表这样的事。

PowerMock works by running your test under a custom ClassLoader which uses Javassist to modify the bytecode of your classes. This works okay on a normal JVM, but on Dalvik the bytecode and class format are different, so this approach doesn't work. PowerMock would need to be rewritten to use Dexmaker instead of Javassist - this would be decidedly non-trivial, and I don't see anything like this on the PowerMock issues list.

这篇关于如何使用PowerMock在Android的项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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