Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android test class fails to compile in eclipse with "bound mismatch" error

I'm writing testcases for an android app extending ActivityInstrumentationTestCase2. The test class looks like this:

public class SolutionEntryActivityTest extends ActivityInstrumentationTestCase2<SolutionEntryActivity> {

    public SolutionEntryActivityTest() {

        super(SolutionEntryActivity.class);
    }
}

In eclipse, this code fails to compile with the error: Bound mismatch: The type SolutionEntryActivity is not a valid substitute for the bounded parameter <T extends Activity> of the type ActivityInstrumentationTestCase2<T>

But SolutionEntryActivity really is an android.app.Activity, the type hierarchy is like this:

Activity
    OrmLiteBaseActivity<H> (abstract)
        KabowieActivity
            PracticeActivity (abstract)
                SolutionEntryActivity

I found two eclipse bugs from 2004 and 2005 which seem to deal with a similar problem but these should be long fixed.

I'm using Eclipse Helios with Android 2.2 and Sun Java 1.6.

Any idea what's going on?

like image 600
ferdystschenko Avatar asked Feb 18 '11 07:02

ferdystschenko


1 Answers

The problem was that when you create a test project with the android eclipse project, references from the project-to-be-tested to external libraries are not resolved automatically. The class OrmLiteBaseActivity (see type hierarchy in the originial question) comes from an external jar included in the build path of the project under test. That external jar needs to be explicitely included in the build path of the test project as well. Not sure if this could be considered a bug and if so, where. But I would expect that all libraries used by the original project should also be available to the test project. Anyway, thanks to those who read the question.

EDIT Correction: adding the same library to the buildpath twice (once to the one of the project under test, and once to the testproject) does not work because JUnit does not understand that both are really the same library. Compilation will succeed but LogCat will give an error message saying that the superclasses of the SolutionEntryActivity are from different sources. The solution here is to export external libs from the original project (Configure buildpath -> Order and export -> select libs to be exported) instead of adding them to the buildpath of the test project directly.

like image 86
ferdystschenko Avatar answered Sep 29 '22 21:09

ferdystschenko