We are having a problem running integration unit tests in eclipse when using RoboGuice 2.0 on our application.
When we try to run the unit test class on the emulator (as an Android JUnit Test) we get the following exception, so the test doesn’t even run:
junit.framework.AssertionFailedError: Exception in constructor: testAddNote (java.lang.NoClassDefFoundError: br.org.certi.android.pgpsip.client.measure.activity.MeasureActivity
at br.org.certi.android.pgpsip.client.test.MeasureActivityTest.<init>(MeasureActivityTest.java:14)
at java.lang.reflect.Constructor.constructNative(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:415)
at junit.runner.BaseTestRunner.getTest(BaseTestRunner.java:103)
at android.test.AndroidTestRunner.getTest(AndroidTestRunner.java:127)
at android.test.AndroidTestRunner.setTestClassName(AndroidTestRunner.java:55)
at android.test.suitebuilder.TestSuiteBuilder.addTestClassByName(TestSuiteBuilder.java:81)
at android.test.InstrumentationTestRunner.parseTestClass(InstrumentationTestRunner.java:418)
at android.test.InstrumentationTestRunner.parseTestClasses(InstrumentationTestRunner.java:399)
at android.test.InstrumentationTestRunner.onCreate(InstrumentationTestRunner.java:364)
...
Here is the relevant part of the test class, I hope its enough:
public class MeasureActivityTest extends ActivityInstrumentationTestCase2<MeasureActivity> {
public MeasureActivityTest() {
super(MeasureActivity.class);
}
Using maven (mvn clean install) the test runs perfectly.
One important thing is that , if the activity class has no dependency on RoboGuice, it works fine on eclipse too.
The relevant part of the pom.xml:
<dependency>
<groupId>org.roboguice</groupId>
<artifactId>roboguice</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>com.google.inject.extensions</groupId>
<artifactId>guice-multibindings</artifactId>
<version>3.0</version>
<exclusions>
<exclusion>
<groupId>com.google.inject</groupId>
<artifactId>guice</artifactId>
</exclusion>
</exclusions>
</dependency>
We noted that when we copy the roboguice, guice and inject dependencies to the libs folder of the application (and mark the dependencies as provided on the pom.xml), the tests run beautifully on eclipse. So it seems to be a problem of the android unit test running in ADT not being able to identify the maven dependencies.
It seems very similar to this problem:
Android/RoboGuice/Maven: ClassNotFoundException in Eclipse, but not from Maven command line, but when running the unit test and not the actual application.
Any ideas on how to work around this problem would be greatly appreciated.
We seem to have found the problem.
We are using two projects on eclipse, actually three: one parent, one for the application and one for the tests.
The NoClassDefFoundError problem came from RoboGuice’s classes being loaded from the wrong dependency when running the test from inside eclipse.
We noticed a message like: “… had used a different Lroboguice/util/RoboContext; during pre-verification …”, a little above the error on the LogCat view.
What seems to have solved the issue was to remove the (unneeded) dependency from the test
project and kept it only on the actual application project and now everything works fine both with maven and with eclipse.
This seems to indicate that the dex generation with eclipse was handling the RoboGuice jars more than once, generating different references when running the application to be tested. I don’t know how to solve this if the dependencies were actually needed.
Thought I would answer this here in hopes it helps others …