我的设置:
- 图书馆项目:ActionBarSherlock
- 项目
- 测试项目
我的项目将库项目链接为库项目。它编译并运行良好。
现在我尝试使用普通的测试项目来测试我的应用程序。在 Eclipse 中运行测试非常完美。如果我尝试使用 ant 运行测试,测试项目甚至不会编译:
[javac] LoginActivityTest.java:9: cannot access com.actionbarsherlock.app.SherlockActivity
[javac] class file for com.actionbarsherlock.app.SherlockActivity not found
[javac] public class LoginActivityTest extends ActivityInstrumentationTestCase2<LoginActivity> {
[javac] ^
[javac] LoginActivityTest.java:25: cannot find symbol
通过 eclipse 构建工作完美,测试运行也很完美。
如果我将库项目链接到我的测试项目,它会用 ant 编译,但测试会失败。
[exec] Error in testSuiteConstructionFailed:
[exec] java.lang.RuntimeException: Exception during suite construction
[exec] at android.test.suitebuilder.TestSuiteBuilder$FailedToCreateTests.testSuiteConstructionFailed(TestSuiteBuilder.java:238)
[exec] at java.lang.reflect.Method.invokeNative(Native Method)
[exec] at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:169)
[exec] at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:154)
[exec] at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:537)
[exec] at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1551)
[exec] Caused by: java.lang.reflect.InvocationTargetException
[exec] at java.lang.reflect.Constructor.constructNative(Native Method)
[exec] at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
[exec] at android.test.suitebuilder.TestMethod.instantiateTest(TestMethod.java:87)
[exec] at android.test.suitebuilder.TestMethod.createTest(TestMethod.java:73)
[exec] at android.test.suitebuilder.TestSuiteBuilder.addTest(TestSuiteBuilder.java:262)
[exec] at android.test.suitebuilder.TestSuiteBuilder.build(TestSuiteBuilder.java:184)
[exec] at android.test.InstrumentationTestRunner.onCreate(InstrumentationTestRunner.java:371)
[exec] at com.zutubi.android.junitreport.JUnitReportTestRunner.onCreate(JUnitReportTestRunner.java:90)
[exec] at android.app.ActivityThread.handleBindApplication(ActivityThread.java:3891)
[exec] at android.app.ActivityThread.access$1300(ActivityThread.java:122)
[exec] at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1184)
[exec] at android.os.Handler.dispatchMessage(Handler.java:99)
[exec] at android.os.Looper.loop(Looper.java:137)
[exec] at android.app.ActivityThread.main(ActivityThread.java:4340)
[exec] at java.lang.reflect.Method.invokeNative(Native Method)
[exec] at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
[exec] at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
[exec] at dalvik.system.NativeStart.main(Native Method)
[exec] Caused by: java.lang.NoClassDefFoundError: com.myproject.android.app.activities.LoginActivity
[exec] at com.myproject.android.app.test.LoginActivityTest.<init>(LoginActivityTest.java:18)
[exec] ... 19 more
我的测试课:
public class LoginActivityTest extends ActivityInstrumentationTestCase2<LoginActivity> {
private LoginActivity mActivity;
private EditText mTextUserName;
private EditText mTextUserPassword;
public LoginActivityTest() {
// the super call is line 18 (see stack trace above)
super("com.myproject.android.app.activities", LoginActivity.class);
}
@Override
protected void setUp() throws Exception {
super.setUp();
mActivity = getActivity();
mTextUserName = (EditText) mActivity.findViewById(com.myproject.android.app.R.id.login_activity_username);
mTextUserPassword = (EditText) mActivity.findViewById(com.myproject.android.app.R.id.login_activity_password);
}
public void testPreConditions() {
assertTrue("Activity is null!", mActivity != null);
}
public void testLogin() throws Throwable {
mActivity.runOnUiThread(new Runnable() {
public void run() {
mTextUserName.setText("username");
mTextUserPassword.setText("password");
}
});
sendKeys(KeyEvent.KEYCODE_ENTER);
}
}
一些想法如何解决这个问题?
更新:看起来 ant 构建/测试仍然是一团糟。根据这篇关于测试库项目的博客文章,列出的 7 个问题中的大部分将在下一个 ADT 版本 (ADT r20) 中修复。