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 ActivityInstr开发者_如何学编程umentationTestCase2<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?
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.
精彩评论