Android 应用程序上的 PowerMock + Mockito + Maven 显示 Dex 加载程序错误
PowerMock + Mockito + Maven on Android app showing Dex loader error
我正在尝试在我的单元测试 (JUnit 4.12) 中使用 PowerMock。
我已经毫无问题地集成了 Mockito。
这是一个 Android 应用程序。
当我 运行 我的单元测试时,没问题,静态函数的模拟工作完美。
当我在 eclipse 中单击 "play" 按钮以 运行 我的应用程序连接到物理机器上时,我收到此错误:
[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 环境!
这是我在 Maven 中与 PowerMock 和 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>
有人可以帮我吗?
编辑
我很确定当我 运行 我的应用程序是真实的(不是在测试中)时,我需要做的就是从我的依赖项中删除 PowerMock,但我不知道该怎么做。我正在使用 Eclipse,所以我需要一个可以在这个环境中工作的解决方案。我已经检查了 Maven 的 Profile 和 Exclusion,但我看不到如何实现这一点。我对 Maven 很陌生。任何帮助将不胜感激。
此外,当我删除 PowerMock 依赖项(以及所有使用它的单元测试)时,该项目现在能够 运行 在我的设备上没有问题。
编辑 2
在评论中建议的命令 mvn dependency:list
的帮助下,我发现了这个:
- JUnit 依赖于 org.hamcrest:hamcrest-core:jar:1.3:test
- PowerMock 也依赖于 hamcrest 库
似乎问题只发生在项目中使用的库的 hamcrest 版本时。我试图删除 JUnit 依赖项,只使用 powermock 一个,启动时的错误是一样的。所以我不认为这是一个 "collision" 问题,但可能是 powermock 附带的 hamcrest 版本的问题???我想知道为什么它在启动时使用,因为它在 "test" 范围内...
编辑 3
我用 Maven 从头开始创建了一个 Android 项目,看看它是我的主应用程序还是 Maven 有问题。问题似乎出在 Maven 或 PowerMock 依赖项中。如果你想尝试,here is the complete java project。这个项目没有单元测试,我只想在我的Android机器上运行它。我仍然收到完全相同的消息。
我终于解决了这个问题,但我还是不太明白。非常感谢 Eugen Martynov for his help and to this post 为我指明了正确的方向。
问题似乎是 Maven 对嵌套依赖项中的重复 .jar 文件存在很大问题。通过使用 class 搜索,我发现 hamcrest class 名称 "Description" 在我的项目中出现了 3 次!
所以我研究了如何在 Maven 中排除依赖项并发现您可以在 Maven Pom 编辑器中完成所有这些操作。您可以单击依赖项选项卡中的依赖项并执行 "Remove".
剩下的问题只是删除重复的 Hamcrest 依赖项,使我的项目中只有一个。解决这个问题后,第二个图书馆也有同样的问题"obgenesis"。我做了同样的修复,终于成功了。
Eugen Martynov 在评论中向我指出我根本没有使用 Maven。也许这是真的,请耐心等待!至少现在,我对 Maven 的了解多了一点,它在我的项目中做了它需要做的事情。
这里是最终的依赖代码:
<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>
我正在尝试在我的单元测试 (JUnit 4.12) 中使用 PowerMock。
我已经毫无问题地集成了 Mockito。 这是一个 Android 应用程序。
当我 运行 我的单元测试时,没问题,静态函数的模拟工作完美。
当我在 eclipse 中单击 "play" 按钮以 运行 我的应用程序连接到物理机器上时,我收到此错误:
[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 环境!
这是我在 Maven 中与 PowerMock 和 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>
有人可以帮我吗?
编辑
我很确定当我 运行 我的应用程序是真实的(不是在测试中)时,我需要做的就是从我的依赖项中删除 PowerMock,但我不知道该怎么做。我正在使用 Eclipse,所以我需要一个可以在这个环境中工作的解决方案。我已经检查了 Maven 的 Profile 和 Exclusion,但我看不到如何实现这一点。我对 Maven 很陌生。任何帮助将不胜感激。
此外,当我删除 PowerMock 依赖项(以及所有使用它的单元测试)时,该项目现在能够 运行 在我的设备上没有问题。
编辑 2
在评论中建议的命令 mvn dependency:list
的帮助下,我发现了这个:
- JUnit 依赖于 org.hamcrest:hamcrest-core:jar:1.3:test
- PowerMock 也依赖于 hamcrest 库
似乎问题只发生在项目中使用的库的 hamcrest 版本时。我试图删除 JUnit 依赖项,只使用 powermock 一个,启动时的错误是一样的。所以我不认为这是一个 "collision" 问题,但可能是 powermock 附带的 hamcrest 版本的问题???我想知道为什么它在启动时使用,因为它在 "test" 范围内...
编辑 3 我用 Maven 从头开始创建了一个 Android 项目,看看它是我的主应用程序还是 Maven 有问题。问题似乎出在 Maven 或 PowerMock 依赖项中。如果你想尝试,here is the complete java project。这个项目没有单元测试,我只想在我的Android机器上运行它。我仍然收到完全相同的消息。
我终于解决了这个问题,但我还是不太明白。非常感谢 Eugen Martynov for his help and to this post 为我指明了正确的方向。
问题似乎是 Maven 对嵌套依赖项中的重复 .jar 文件存在很大问题。通过使用 class 搜索,我发现 hamcrest class 名称 "Description" 在我的项目中出现了 3 次!
所以我研究了如何在 Maven 中排除依赖项并发现您可以在 Maven Pom 编辑器中完成所有这些操作。您可以单击依赖项选项卡中的依赖项并执行 "Remove".
剩下的问题只是删除重复的 Hamcrest 依赖项,使我的项目中只有一个。解决这个问题后,第二个图书馆也有同样的问题"obgenesis"。我做了同样的修复,终于成功了。
Eugen Martynov 在评论中向我指出我根本没有使用 Maven。也许这是真的,请耐心等待!至少现在,我对 Maven 的了解多了一点,它在我的项目中做了它需要做的事情。
这里是最终的依赖代码:
<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>