简体   繁体   English

在Android模拟器或设备上运行Java SE库的JUnit测试

[英]Run JUnit Tests of Java SE library on Android emulator or device

I have a simple library which compiles and runs on Java SE and builds with Maven. 我有一个简单的库,可以在Java SE上编译和运行,并使用Maven构建。 The test classes are JUnit test cases (currently 4.x, no problem to downgrade to 3.x). 测试类是JUnit测试用例(目前是4.x,降级到3.x没问题)。

The library can be used in Android, but has no dependencies on android.* . 该库可以在Android中使用,但不依赖于android.* It is absolutely necessary to run the JUnit tests on an emulator or Android device, since there are some subtle differences in the JDK (Java SE and Android/Harmony). 在模拟器或Android设备上运行JUnit测试是绝对必要的,因为JDK(Java SE和Android / Harmony)存在一些细微差别。

I don't want to copy the test cases to an Hello-Testing Android project, but reuse the existing classes. 我不想将测试用例复制到Hello-Testing Android项目,而是重用现有的类。

Is there a way to achieve this? 有没有办法实现这个目标?

To execute the tests on the android emulator you need to create a Test Project (this can only use JUnit 3), that is depending on a normal Android Project. 要在android模拟器上执行测试,您需要创建一个测试项目(这只能使用JUnit 3),这取决于普通的Android项目。

I would suggest you add a Test-suit to your library tests containing calls to all tests of your library. 我建议您在库测试中添加一个测试套件,其中包含对库的所有测试的调用。 Create a simple Hello Word Android Project. 创建一个简单的Hello Word Android项目。 Create a Android Test Project for this Project. 为此项目创建Android测试项目。 Then you can call the new created test-suit from your Android Test Project and you do not need to copy any test cases. 然后,您可以从Android测试项目中调用新创建的测试套件,而无需复制任何测试用例。 All you do is building some Wrapper Projects. 您所做的只是构建一些Wrapper项目。

Probably this is not the optimum solution, but I managed to test the library running in Android (Simulator or real device). 可能这不是最佳解决方案,但我设法测试在Android(模拟器或真实设备)中运行的库。 You can find it on DavidWebb . 你可以在DavidWebb上找到它。

public class DavidWebbAndroidTests extends TestSuite {
    public static Test suite () {
        return new TestSuiteBuilder(DavidWebbAndroidTests.class)
            .includeAllPackagesUnderHere()
            .build();
    }
}

The caveat is that I have to extend all tests so that they are present on Android. 需要注意的是,我必须扩展所有测试,以便它们出现在Android上。

Hopefully someone will find a better solution, but at least it works. 希望有人会找到更好的解决方案,但至少它有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM