Simple non-API Android JUnit test in Eclipse with Android maven plugin?

I run tests other than Android JUnit from Eclipse every day. Today I wanted to test some of my Android library classes. Oh pain.

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

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

So, I do a little JUnit unit test, as I'm used to, and try to run it in Eclipse using Ctrl+F11 . Eclipse asks me if this is the "Android JUnit Test" or the "JUnit Test". Well, Android, obviously. Therefore, 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! 

Hmmm ... it was not very successful. Therefore, I delete the launch configuration and try simply "JUnit Test". Now I get another dialog asking me to choose my preferred launcher "Android JUnit Test Launcher" or "Eclipse JUnit Test Launcher". It doesn't matter what 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) 

I read that with the android-maven plugin, I can run unit tests locally in Eclipse if they just use classes in the Android bank but don't make any API calls, which I do here. So how can I take this off?

+4
source share
3 answers

The workaround is to click the "Run As ..." button on the toolbar, and then select "Run Configurations ...". If you select the JUnit launcher that you created and go to the "Classpath" tab, you can add the bin/classes folder to the JUnit launcher class path. That should work now.

+4
source

Do not use Run As - Android JUnit Test , as it only works for Android Test Project .

When using Run As - JUnit Test ClassNotFoundException occurs due to inconsistency between ADT and Eclipse built-in JUnit Test Runner refers to the project output folder. ADT generates all .class files in bin/classes , while the built-in test runer JUnit looks for .class files under target/classes . Your Android project in Eclipse never uses target/classes , so it stays empty, so you get a ClassNotFoundException .

AFAIK there is no way to change the Eclipse built-in JUnit Test Runner to use a different folder than the target / classes by default. Check out The answer to Ricardo's question to learn how to add bin/classes to the built-in class path of the JUnit Test Runner. Also note that you cannot change your default output folder of an Android project to anything other than ../bin/classes , as it will disrupt the ADT build process.

A dirty workaround (for ClassNotFoundException solution) manually copies everything under bin\classes to target\classes , note that you need to do this every time you change the source code.

This is not a problem when running mvn test from the command line or through Eclipse, since Maven uses target\classes and knows how to populate it correctly. note that with this approach you will not be able to use the JUnit window with a beautiful red / green error inside Eclipse.

+3
source

The junit android test should be located in a separate module as a setting in the Android Maven Plugin sample projects (for example, morseflash example). This is due to the configuration of the overloaded path and the need to build apk and deploy to the device / emulator to run the test. Junit tests for Android are NOT single tests in general, but rather an integration test (or in this case called measurement tests).

0
source

Source: https://habr.com/ru/post/949007/


All Articles