PowerMock + +的Mockito对Maven的地塞米松显示错误装载机Android应用 [英] PowerMock + Mockito + Maven on Android app showing Dex loader error

查看:320
本文介绍了PowerMock + +的Mockito对Maven的地塞米松显示错误装载机Android应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的单元测试(JUnit的4.12)使用PowerMock。

I'm trying to use PowerMock in my unit test (JUnit 4.12).

我已经有集成的Mockito没有任何问题。
这是一个Android应用程序。

I already have Mockito integrated without any issue. This is an Android application.

当我运行我的单元测试,没有问题,静态函数的嘲讽完美的作品。

When I run my unit test, no problem and the mocking of the static function works perfectly.

当我在Eclipse中的播放按钮即可运行我的连接在物理机上的应用程序,我收到此错误:

When I click on the "play" button in eclipse to run my app on the physical machine connected, I receive this error:

[2015-01-15 15:22:22 - Dex Loader] Unable to execute dex: Multiple dex files define Lorg/hamcrest/Description;
[2015-01-15 15:22:22 - CLAP] Conversion to Dalvik format failed: Unable to execute dex: Multiple dex files define Lorg/hamcrest/Description;

我已阅读,这意味着PowerMock不支持Delvik VM,但我不明白这是什么意思,我不能相信PowerMock团队或团队的Mockito没有找到一种方法,在Android工作环境!

I have read that this means that PowerMock doesn't support the Delvik VM but I don't understand what this means and I can't believe that the PowerMock team or Mockito Team did not find a way to work in the Android environment!

下面是我的依赖关系Maven的有关PowerMock和的Mockito

Here is my dependencies in Maven related to PowerMock and Mockito

        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-core</artifactId>
            <version>1.9.5</version>
            <scope>test</scope>
        </dependency>

       <dependency>
          <groupId>org.powermock</groupId>
          <artifactId>powermock-module-junit4</artifactId>
          <version>1.6.1</version>
          <scope>test</scope>
       </dependency>

       <dependency>
          <groupId>org.powermock</groupId>
          <artifactId>powermock-api-mockito</artifactId>
          <version>1.6.1</version>
          <scope>test</scope>
       </dependency>

任何人可以帮助我吗?

Can anybody help me?

修改

EDIT

我是pretty确保所有我需要做的是,当我运行我的应用程序的删除从我的依赖关系PowerMock真正(而不是测试),但我不知道如何做到这一点。我使用Eclipse,所以我需要一个解决方案,在这种环境中工作。我已经检查从Maven的配置文件和排除,但我不能看到如何实现这一目标。我很新的行家。任何帮助将大大AP preciated。

I'm pretty sure all I need to do is to remove the PowerMock from my dependencies when I run my application for real (not in test) but I don't know how to do this. I'm using Eclipse so I need a solution that will work in this environment. I have checked the Profile and Exclusion from Maven but I can't see how to achieve this. I'm very new to maven. Any help will be greatly appreciated.

此外,当我删除PowerMock依赖关系(以及使用它所有的单元测试),该项目现在能够没有问题我的设备上运行。

Also, when I remove the PowerMock dependencies (and all the unit test using it), the project now is able to run on my device without problem.

编辑2

EDIT 2

通过命令的帮助MVN依赖性:列表的意见建议,我也发现了这一点:

With the help of the command mvn dependency:list suggested in the comment, I have discovered this:


  • 的JUnit对org.hamcrest依赖:hamcrest核心:瓶子:1.3:测试

  • PowerMock也纷纷对hamcrest库的依赖性

看来,当它是在项目中使用的库的版本hamcrest问题只发生。我曾试图删除JUnit的依赖,只能使用powermock之一,并在推出的错误是一样的。因此,我不认为这是一个冲突的问题,但也许与hamcrest版本powermock未来的一个问题?而且我不知道为什么它的使用在发射,因为它是在测试的范围...

It seems the problem is only happening when it's the hamcrest version of the library that is used in the project. I have tried to remove JUnit dependency and only use the powermock one and the error at launch is the same. So I don't think it's a "collision" problem but maybe a problem with the hamcrest version coming with powermock??? And I wonder why it's using at launch since it's in the "test" scope...

编辑3
我创建了一个Android项目从与Maven从无到有,看它是否是我的主要应用或使用Maven的一个问题。这个问题似乎是在Maven的或PowerMock依赖。如果您想尝试,这里是完整的Java项目。有没有单元测试在这个项目中,我只希望我的Andr​​oid机上运行它。我仍然收到确切的相同的消息。

EDIT 3 I have created a Android project from scratch with Maven to see if it's a problem with my main application or with Maven. The problem seems to be in Maven OR in the PowerMock dependency. If you want to try, here is the complete java project. There is no unit test in this project, I only want to run it on my Android machine. I still receive the exact same message.

推荐答案

我终于解决了这一问题,但我不明白的一切。由于很多尤金·马丁诺夫的对他的帮助和<一个href=\"http://stackoverflow.com/questions/22702267/android-gradle-dexexception-multiple-dex-files-defined\">this帖子这点我在正确的方向。

I have finally fixed the problem but I don't understand everything. Thanks a lot to Eugen Martynov for his help and to this post which point me in the right direction.

这个问题似乎是,Maven的有是嵌套依赖重复.jar文件的一个大问题。通过使用级搜索,我发现hamcrest类名说明是在我的项目3倍!

The problem seems to be that Maven have a big problem with duplicated .jar files that are in nested dependencies. By using the class search, I discovered that the hamcrest class name "Description" was in my project 3 times!

因此​​,我在如何排除在Maven依赖一些研究和发现,你可以做这一切在Maven POM文件编辑器。您可以在依赖选项卡上的依赖关系点击或者删除。

So I did some research on how to exclude dependencies in Maven and discover that you can do all of this in the Maven Pom Editor. You can click on a dependencies in the dependencies tab and do "Remove".

问题的其余部分只是去掉重复的Hamcrest依赖关系,只有在我的项目之一。解决这个问题之后,第二个库有同样的问题obgenesis。我做了同样的方位,它终于奏效。

The rest of the problem was just to remove the duplicated Hamcrest dependencies to have only one in my project. After fixing this problem, a second library had the same problem "obgenesis". I did the same fix and it finally worked.

尤金·马丁诺夫在我没有使用Maven所有评论指给我看。也许这是真的,裸跟我来!至少现在,我知道Maven的多一点点,它做什么,它需要在我的项目做。

Eugen Martynov pointed out to me in the comment that I was not using Maven at all. Maybe that's true, bare with me! At least now, I know Maven a little bit more and it does what it needed to do in my project.

下面是最后的依赖code:

Here is the final dependencies code:

<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
    <exclusions>
        <exclusion>
            <artifactId>hamcrest-core</artifactId>
            <groupId>org.hamcrest</groupId>
        </exclusion>
    </exclusions>
</dependency>

<dependency>
    <groupId>org.mockito</groupId>
    <artifactId>mockito-core</artifactId>
    <version>1.10.8</version>
    <scope>test</scope>
    <exclusions>
        <exclusion>
            <artifactId>objenesis</artifactId>
            <groupId>org.objenesis</groupId>
        </exclusion>
    </exclusions>
</dependency>

<dependency>
    <groupId>org.powermock</groupId>
    <artifactId>powermock-module-junit4</artifactId>
    <version>1.6.1</version>
    <scope>test</scope>
    <optional>true</optional>
    <exclusions>
        <exclusion>
            <artifactId>junit</artifactId>
            <groupId>junit</groupId>
        </exclusion>
    </exclusions>
</dependency>

<dependency>
    <groupId>org.powermock</groupId>
    <artifactId>powermock-api-mockito</artifactId>
    <version>1.6.1</version>
    <scope>test</scope>
    <optional>true</optional>
    <exclusions>
        <exclusion>
            <artifactId>mockito-all</artifactId>
            <groupId>org.mockito</groupId>
        </exclusion>
    </exclusions>
</dependency>

这篇关于PowerMock + +的Mockito对Maven的地塞米松显示错误装载机Android应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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