Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - Junit - Tested project uses external Jar

I have some java code (compiles nicely for Android) in a library project (LIB) that I want to use in an another Android project (A).

As LIB's code is unlikely to change much, I opted for adding it as a jar to A. It works fine.

I have another project that is an Android instrumentation project, testing the first Android project (B).

So what we have now is A including LIB as an external jar, and B testing A.

The problem starts when I want to access from B code written in LIB. From what I see, unless I add LIB as an external jar to B, it refuses to compile (that is, it cannot access the code in the LIB jar that is included in A).

I am reluctant to add LIB as an external jar to B because: 1. It doesn't feel right, and 2. When running the tests it fails with:

java.lang.IllegalAccessError: Class ref in pre-verified class resolved to unexpected implementation

I found Mike's question, but his solution only covers compiling using an ant script, and I currently prefer to use the Eclipse IDE for this project.

Any idea how can I solve this issue?

like image 750
Amit Kotlovski Avatar asked Oct 12 '10 12:10

Amit Kotlovski


People also ask

Is JUnit an external library?

An external library would be something like, Junit, Spring, and other frameworks that are not native code to Java, and instead were created by a third party source.

Where should JUnit tests be stored?

Keep tests in the same location as the source code If the test source is kept in the same location as the tested classes, both test and class will compile during a build.

Which tests run on JVM in Android?

Local unit tests are located at module-name /src/test/java/ . These are tests that run on your machine's local Java Virtual Machine (JVM). Use these tests to minimize execution time when your tests have no Android framework dependencies or when you can create test doubles for the Android framework dependencies.

Which Java framework is used to write unit tests in android?

JUnit is a “Unit Testing” framework for Java Applications which is already included by default in android studio. It is an automation framework for Unit as well as UI Testing. It contains annotations such as @Test, @Before, @After, etc.


1 Answers

(In order to remove this question from the "Unanswered" filter...)

The solution is outlined here:

Android Testing: External libraries

The LIB should be exported from A to make it accessible from B.

like image 111
DreadPirateShawn Avatar answered Sep 16 '22 14:09

DreadPirateShawn