无法运行android测试套件:线程"main"中存在异常; java.lang.ClassNotFoundException [英] Can't run android test suite: Exception in thread "main" java.lang.ClassNotFoundException
问题描述
我有2个需要在测试套件上运行的测试类,一个是RestaurantModelTest
,另一个是CartModelTest.class
:
I have 2 test classes that I need to run on a test suite one is RestaurantModelTest
the other is CartModelTest.class
:
@Config(sdk = 16, manifest = "src/main/AndroidManifest.xml")
@RunWith(RobolectricTestRunner.class)
public class RestaurantModelTest {
//setUp code here...
@Test
public void testFindByLocation() throws Exception {
//test code here
}
}
因此,我在线上关注了一些教程,他们指定使用我要测试的2个类来创建一个类似这样的TestSuite类:
So I followed some tutorials online and they specify to create a TestSuite class like this with my 2 classes to be tested:
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
@RunWith(Suite.class)
@Suite.SuiteClasses({
RestaurantModelTest.class,
CartModelTest.class
})
public class ModelsTestSuite {
}
然后我必须创建一个TestSuiteRunner类:
And then I must create a TestSuiteRunner class:
import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;
public class TestSuiteRunner {
public static void main(String[] args){
Result result = JUnitCore.runClasses(ModelsTestSuite.class);
for (Failure failure : result.getFailures()){
System.out.println(failure.toString());
}
System.out.println(result.wasSuccessful());
}
}
我正在使用android studio,当我讨厌右键单击>运行TestSuiteRunner ... main()时,我会得到Exception in thread "main" java.lang.ClassNotFoundException:
I'm using android studio and when I hate right-click > Run TestSuiteRunner...main() I get Exception in thread "main" java.lang.ClassNotFoundException:
任何帮助都非常感谢!
推荐答案
运行套件中的所有测试所需要做的就是-
All you need to do to run all the tests in suite - is to:
- 右键单击
ModelsTestSuite
- 点击运行ModelsTestSuite"
就是这样,套件中的所有测试都将执行.您实际上并不需要TestSuiteRunner
类:
That's it, all tests in the suite will be executed. You don't really need TestSuiteRunner
class for that:
注意!在解决此问题时,我发现了另一个奇怪的问题:除非我禁用了即时运行(首选项->构建,执行,部署->即时运行->在Android Studio 2.0中启用即时运行-取消选中),我的所有Robolectric测试均失败,并显示java.lang.ClassNotFoundException
. jFYI.
NB! While solving this problem, I found another bizarre one: unless I disabled instant run (Preferences -> Build, Execution, Deployment -> Instant Run -> Enable Instant Run - uncheck) in Android Studio 2.0, all my Robolectric tests were failing with java.lang.ClassNotFoundException
. jFYI.
得到java.lang.ClassNotFoundException
的原因是普通的Java项目和Android项目不兼容.您可以在此处了解更多信息:可以使用Android Studio来运行标准的Java项目?
The reason you get java.lang.ClassNotFoundException
is incompatibility of plain java projects and Android projects. You can read more here: Can Android Studio be used to run standard Java projects?
因此,从理论上讲,要使TestSuiteRunner
处于这种形式,您可以将其移动到单独的Java库模块,并将Android应用程序的模块链接为该Java库的依赖项.不幸的是,您会收到以下警告:
So, theoretically, to have TestSuiteRunner
in this form, you could move it to separate Java library module and link Android app's module as a dependency for this Java lib. Unfortunately, you'd get this warning:
警告:忽略模块"app"对模块"lib"的依赖性. Java模块不能依赖Android模块
Warning:Ignoring dependency of module 'app' on module 'lib'. Java modules cannot depend on Android modules
即它不会以这种方式工作.
i.e. it's not gonna work this way.
我希望这会有所帮助.
这篇关于无法运行android测试套件:线程"main"中存在异常; java.lang.ClassNotFoundException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!