简单的非API的Andr​​oid的JUnit在Eclipse测试与Android,Maven的插件?插件、简单、测试、Andr

2023-09-05 00:28:02 作者:ancestor(小祖宗)

我运行,每天在Eclipse非Android JUnit测试。今天,我想测试我的一些Android的库类的。哦,痛苦。

I run non-Android JUnit tests from within Eclipse every day. Today I wanted to test some of my Android library classes. Oh, the pain.

我在使用Android系统的maven-plugin的一个Android库项目。我在的src / main / java下的src /测试/ java的我的(新)单元测试的源文件。我POM具有适当的JUnit的依赖和Android,Maven的插件引用。

I have an Android library project using android-maven-plugin. I have source files in src/main/java and my (new) unit test in src/test/java. My POM has the appropriate JUnit dependencies and android-maven-plugin references.

有时候,我创建一个文件一个Android 乌里实例。有时候,我有一个现有的Java URI 实例,我已经从文件然后我转换为乌里。因为我相信没有Java和Android的处理文件和URI(不要让我开始对java如何轧液中的URI UNC路径,或Java如何打破等于()合同的URI),我想创建一个简单的单元测试,以创建一个临时文件,从两个不同的方法创建尤里斯,并确保他们出来平等的。

Sometimes I create an Android Uri instance from a File. Sometimes I have an existing Java URI instance that I've created from a File which I then convert to a Uri. Since I trust neither Java nor Android with files and URIs (don't get me started on how Java mangles UNC paths in URIs, or how Java breaks the equals() contract in URIs), I wanted to create a simple unit test to create a temp file, create Uris from two different approaches, and make sure they come out equal.

所以,我做一点JUnit单元测试就像我已经习惯了,并尝试使用按Ctrl + F11 在Eclipse中运行它。 Eclipse询问我,如果这是一个Android的JUnit测试或JUnit测试。好了,Android的,很明显。所以,我选择了第一个选项,并得到:

So I make a little JUnit unit test like I'm used to, and try to run it in Eclipse using Ctrl+F11. Eclipse asks me if this is an "Android JUnit Test" or a "JUnit Test". Well, Android, obviously. So I choose the first option and get:

[2013-03-23 21:37:10 - mylib] ------------------------------
[2013-03-23 21:37:10 - mylib] Android Launch!
[2013-03-23 21:37:10 - mylib] adb is running normally.
[2013-03-23 21:37:10 - mylib] Could not find mylib.apk!

嗯...这是不是很成功。所以我删除运行配置,并尝试只是JUnit测试。现在,我得到一个不同的对话,要我选择我的preferred发射器,无论是Android的JUnit测试启动器或Ecl​​ipse的JUnit测试启动器。没关系,这是我选择;我得到的:

Hmmm... that wasn't very successful. So I delete the run configuration and try just "JUnit Test". Now I get a different dialog, asking me to select my preferred launcher, either "Android JUnit Test Launcher" or "Eclipse JUnit Test Launcher". It doesn't matter which I choose; I get:

Class not found com.example.MyUnitTest
java.lang.ClassNotFoundException: com.example.MyUnitTest
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.loadClass(RemoteTestRunner.java:693)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.loadClasses(RemoteTestRunner.java:429)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:452)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

我读过了Android,Maven的插件,我可以在Eclipse本地运行单元测试,如果他们只是使用类在Android罐子,但不作任何API调用,这是我在做什么在这里。所以,我怎么拉说了吗?

I've read that with the android-maven-plugin I can run unit tests locally in Eclipse if they just use classes in the Android jar but don't make any API calls, which is what I'm doing here. So how do I pull that off?

推荐答案

一个解决方法是单击运行方式...工具栏中的按钮,然后选择运行配置...。如果您选择创建的JUnit的启动并进入其类路径选项卡中可以斌/班文件夹中添加的JUnit的启动类路径。这应该现在运行。

A workaround is to click the "Run As..." button in the toolbar and then select "Run Configurations...". If you select the JUnit launcher you created and go to its "Classpath" tab you can add the bin/classes folder to the JUnit launcher classpath. This should now run.