android sdk源包含junit.extensions的源代码,但是这些类不在android.jar中,即使junit.framework和junit.runner在那里。我尝试创建自己的junit.extensions包并使用android sdk源中包含的源代码,但是我得到了一个ClassCastException(见下文)。 android.test.suitebuilder中的东西试图将套接字()方法的返回值强制转换为TestCase,即使套件返回Test接口。
我想使用junit.extensions中的TestSetup类,如下例所示(参见http://etutorials.org/Programming/Java+extreme+programming/Chapter+4.+JUnit/4.7+One-Time+Set+Up+and+Tear+Down/):
public SomeTestCase extends TestCase {
public static Test suite() {
TestSetup setup = new TestSetup(new TestSuite(SomeTestCase.class)) {
@Override
protected void setUp( ) throws Exception {
// do your one-time setup here!
}
@Override
protected void tearDown( ) throws Exception {
// do your one-time tear down here!
}
};
return setup;
}
public void someTestMethod() { }
}
但是,我收到以下错误:
05-21 08:10:14.152: I/TestRunner(1316): java.lang.RuntimeException: Exception during suite construction
05-21 08:10:14.152: I/TestRunner(1316): at android.test.suitebuilder.TestSuiteBuilder$FailedToCreateTests.testSuiteConstructionFailed(TestSuiteBuilder.java:238)
05-21 08:10:14.152: I/TestRunner(1316): at java.lang.reflect.Method.invokeNative(Native Method)
05-21 08:10:14.152: I/TestRunner(1316): at java.lang.reflect.Method.invoke(Method.java:511)
05-21 08:10:14.152: I/TestRunner(1316): at junit.framework.TestCase.runTest(TestCase.java:154)
05-21 08:10:14.152: I/TestRunner(1316): at junit.framework.TestCase.runBare(TestCase.java:127)
05-21 08:10:14.152: I/TestRunner(1316): at junit.framework.TestResult$1.protect(TestResult.java:106)
05-21 08:10:14.152: I/TestRunner(1316): at junit.framework.TestResult.runProtected(TestResult.java:124)
05-21 08:10:14.152: I/TestRunner(1316): at junit.framework.TestResult.run(TestResult.java:109)
05-21 08:10:14.152: I/TestRunner(1316): at junit.framework.TestCase.run(TestCase.java:118)
05-21 08:10:14.152: I/TestRunner(1316): at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:169)
05-21 08:10:14.152: I/TestRunner(1316): at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:154)
05-21 08:10:14.152: I/TestRunner(1316): at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:545)
05-21 08:10:14.152: I/TestRunner(1316): at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1551)
05-21 08:10:14.152: I/TestRunner(1316): Caused by: java.lang.ClassCastException: com.somebody.test.SomeTestCase$1 cannot be cast to junit.framework.TestCase
05-21 08:10:14.152: I/TestRunner(1316): at android.test.suitebuilder.TestSuiteBuilder.build(TestSuiteBuilder.java:188)
05-21 08:10:14.152: I/TestRunner(1316): at android.test.InstrumentationTestRunner.onCreate(InstrumentationTestRunner.java:379)
05-21 08:10:14.152: I/TestRunner(1316): at android.app.ActivityThread.handleBindApplication(ActivityThread.java:3920)
05-21 08:10:14.152: I/TestRunner(1316): at android.app.ActivityThread.access$1300(ActivityThread.java:123)
05-21 08:10:14.152: I/TestRunner(1316): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1185)
05-21 08:10:14.152: I/TestRunner(1316): at android.os.Handler.dispatchMessage(Handler.java:99)
05-21 08:10:14.152: I/TestRunner(1316): at android.os.Looper.loop(Looper.java:137)
05-21 08:10:14.152: I/TestRunner(1316): at android.app.ActivityThread.main(ActivityThread.java:4424)
05-21 08:10:14.152: I/TestRunner(1316): at java.lang.reflect.Method.invokeNative(Native Method)
05-21 08:10:14.152: I/TestRunner(1316): at java.lang.reflect.Method.invoke(Method.java:511)
05-21 08:10:14.152: I/TestRunner(1316): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
05-21 08:10:14.152: I/TestRunner(1316): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
05-21 08:10:14.152: I/TestRunner(1316): at dalvik.system.NativeStart.main(Native Method)
TestCase的suite()方法应该返回一个Test,但是android os中出现的东西试图将它转换为TestCase。
作为解决问题的一种尝试,我调整了TestDecorator类,使其子类化了TestCase而不是Test,但它仍然给出了相同的ClassCastException,这让我感到很困惑。
顺便说一下,似乎对包含junit的android.jar有一些疑问,所以这里是android.jar内容的部分列表:
$ jar tf android.jar | more
META-INF/
META-INF/MANIFEST.MF
assets/
assets/images/
assets/images/android-logo-mask.png
assets/images/android-logo-shine.png
assets/sounds/
assets/sounds/bootanim1.raw
assets/sounds/bootanim0.raw
assets/webkit/
assets/webkit/android-weberror.png
assets/webkit/togglePlugin.png
assets/webkit/nullPlugin.png
assets/webkit/youtube.html
assets/webkit/missingImage.png
assets/webkit/youtube.png
assets/webkit/textAreaResizeCorner.png
assets/webkit/play.png
junit/
junit/framework/
junit/framework/TestSuite.class
junit/framework/ComparisonFailure.class
junit/framework/Assert.class
junit/framework/TestListener.class
junit/framework/TestResult.class
junit/framework/Test.class
junit/framework/TestFailure.class
junit/framework/Protectable.class
junit/framework/TestCase.class
junit/framework/AssertionFailedError.class
junit/runner/
junit/runner/TestSuiteLoader.class
junit/runner/Version.class
junit/runner/BaseTestRunner.class
您还可以在https://developer.android.com/reference/packages.html的Android参考站点的包内容中看到它。如果您仍然怀疑,请浏览您的android.jar副本。
有用的建议吗?有没有人成功使用junit.extensions.TestSetup与android?