简体   繁体   English

Android-JUnit测试模型/控制器(仅使用Java导入),而无需部署Android仿真器

[英]Android - JUnit testing Models/Controllers (which only use java imports) without the Android Emulator deploy

I've got two different UnitTest Projects for my Android App Project. 我的Android App项目有两个不同的UnitTest项目。 One for the Model Classes (which only uses java imports), and one for the Activities (which obviously uses both java & android imports). 一种用于模型类(仅使用java导入),另一种用于“活动”(显然同时使用java和android导入)。

In Eclipse, when I use the option Run As -> Android JUnit Test on either of those two UnitTest Projects, I'm getting the following in my Console: 在Eclipse中,当我在这两个UnitTest项目中的任何一个上使用选项运行方式 -> Android JUnit Test时,我在控制台中得到以下内容:

[2014-05-23 11:02:23 - MyProject.test] Android Launch!
[2014-05-23 11:02:23 - MyProject.test] adb is running normally.
[2014-05-23 11:02:23 - MyProject.test] Performing android.test.InstrumentationTestRunner JUnit launch
[2014-05-23 11:02:23 - MyProject.test] Automatic Target Mode: using existing emulator 'emulator-5554' running compatible AVD 'MyEmulator'
[2014-05-23 11:02:27 - MyProject.test] Application already deployed. No need to reinstall.
[2014-05-23 11:02:27 - MyProject.test] Project dependency found, installing: MyProject
[2014-05-23 11:02:30 - MyProject] Application already deployed. No need to reinstall.
[2014-05-23 11:02:30 - MyProject.test] Launching instrumentation android.test.InstrumentationTestRunner on emulator-5554
[2014-05-23 11:03:03 - MyProject.test] Sending test information to Eclipse
[2014-05-23 11:03:40 - MyProject.test] Test run finished

Both UnitTest Projects work and give test-results, but since it's using the Emulator to test, it takes a bit of time before the results show up. 两个UnitTest项目都可以工作并给出测试结果,但是由于它是使用仿真器进行测试的,因此要花一些时间才能显示结果。

Now my question: Is it possible to run an Android UnitTest Project, which only uses java imports, as a JUnit , without having to use the Emulator? 现在我的问题是否可以运行一个仅使用Java导入作为JUnit的Android UnitTest项目,而不必使用模拟器? Which would result in much faster test-results. 这将导致更快的测试结果。

When I try the option Run As -> JUnit Test I'm getting the following error: 当我尝试运行方式 -> JUnit测试选项时,出现以下错误:

#
# A fatal error has been detected by the Java Runtime Environment:
#
#  Internal Error (classFileParser.cpp:3494), pid=388, tid=1680
#  Error: ShouldNotReachHere()
#
# JRE version: 6.0_31-b05
# Java VM: Java HotSpot(TM) 64-Bit Server VM (20.6-b01 mixed mode windows-amd64 compressed oops)
# An error report file with more information is saved as:
# C:\Users\...\MyProject.test\hs_err_pid388.log
#
# If you would like to submit a bug report, please visit:
#   http://java.sun.com/webapps/bugreport/crash.jsp
#

My Test-Classes used to be: 我的测试班过去是:

public class MyUnitTest extends android.test.AndroidTestCase
{
    public void aTest(){
        ...
        assertSomething(...);
        ...
    }

    ...
}

And I used to run them with Run As -> Android JUnit Test . 我以前使用Run As- > Android JUnit Test来运行它们。


I've changed it by the following steps: 我通过以下步骤对其进行了更改:

  1. First I've followed the instructions here. 首先,我按照这里的说明进行操作。
  2. Then in all my Test-Classes I've removed the extends android.test.AndroidTestCase . 然后在我所有的测试类中,我都删除了extends android.test.AndroidTestCase
  3. Added the following two imports: import junit.framework.Assert; 添加了以下两个导入: import junit.framework.Assert; and import org.junit.Test; import org.junit.Test; (WARNING: Make sure you use junit.framework.Assert instead of org.junit.Assert ). (警告:确保使用junit.framework.Assert而不是org.junit.Assert )。
  4. Added @Test before every Test-Method. 在每个测试方法之前添加了@Test
  5. Changed all assertSomething(...); 更改了所有assertSomething(...); to Assert.assertSomething(...); Assert.assertSomething(...); and all fail(...); fail(...); to Assert.fail(...); Assert.fail(...);
  6. Removed some Android-Logs I still had in some of my Model/Controller classes.. 删除了一些我在某些Model / Controller类中仍然拥有的Android日志。

Now my Test-Classes are: 现在我的测试班是:

import junit.framework.Assert;

import org.junit.Test;

public class MyUnitTest
{
    @Test
    public void aTest(){
        ...
        Assert.assertSomething(...);
        ...
    }

    ...
}

And now I'm able to run the Project by Run As -> JUnit Test . 现在,我可以通过Run As- > JUnit Test运行该项目。


UPDATE: 更新:

When you have this Project up and running, testing all UnitTests at the same time in Eclipse by right-clicking the Project -> Run as -> JUnit Test isn't a problem. 当您启动并运行该项目时,通过右键单击Project-> Run as- > JUnit Test来同时在Eclipse中测试所有UnitTest都不是问题。 But when you try to Test individual UnitTest-classes (like the ones you've just added), the same error as in my question might occur. 但是,当您尝试测试单个UnitTest类时(例如您刚刚添加的类),可能会发生与我的问题相同的错误。

These are the steps when you want to add a Java-only UnitTest-class in your Test-Project, which only uses Java libraries and enabling individual testing of the Test-classes without getting the error: 这些是您要在Test-Project中添加仅Java的UnitTest类时的步骤,该类仅使用Java库并启用Test类的单独测试而不会出现错误:

  1. Add a class (right-click a folder inside src ) -> New -> Class 添加一个类(右键单击src内的文件夹)-> 新建 ->
  2. Add the import junit.framework.Assert; 添加import junit.framework.Assert; and import org.junit.Test; import org.junit.Test; like mentioned above. 就像上面提到的。
  3. Create some UnitTest-methods with the synthax like above (so including the @Test and Assert.assertSomething(...); ) 用上面的语法创建一些UnitTest方法(因此包括@TestAssert.assertSomething(...);
  4. Right-click the new UnitTest -> Run as -> JUnitTest 右键单击新的UnitTest-> Run as- > JUnitTest
  5. Select Eclipse JUnit Launcher 选择Eclipse JUnit启动器
  6. Now the error will occur that is displayed at the bottom of my question-post 现在将出现在我的问题帖子底部的错误
  7. Right-click the Test-Project -> Properties -> Run/Debug Settings 右键单击测试项目-> 属性 -> 运行/调试设置
  8. Select the new UnitTest that gives the error -> Edit... 选择出现错误的新UnitTest-> Edit ...
  9. Go to the Classpath tab -> Under Bootstrap Entries remove Google APIs [Android 4.4.2] . 转到Classpath选项卡->在Bootstrap Entries下,删除Google API [Android 4.4.2] (This might be different in your case, but you should remove the Android API or Android Google API .) (这可能与您的情况有所不同,但是您应该删除Android APIAndroid Google API 。)
  10. After you've removed it click Apply -> OK -> Apply again at the Run/Debug Settings -> OK 删除后,单击应用 -> 确定 ->在“ 运行/调试设置”中再次应用 -> 确定

Now you are successfully able to use Run As -> JUnit Test on this individual Test-class, instead of being forced to Test the entire Project every time. 现在,您可以成功地在此单独的Test-class上使用Run As- > JUnit Test ,而不必每次都被迫测试整个Project。


I hope this helps anyone trying to make JUnit Tests for an Android project. 我希望这对尝试为Android项目进行JUnit测试的人有所帮助。 PS: I also have a second Test-Project with the JUnit Tests that require Android libraries. PS:我还有第二个Test-Project,其中包含需要Android库的JUnit Tests。 Just make sure none of the classes you want to test uses Android Libraries (like Logcat-messages, Test-Toasts, or something like Patterns.WEB_URL.matcher(url).matches()) [ android.util.Patterns ] which validates a String URL) are being used inside your JUnit-only Test Project. 只要确保您要测试的类均未使用Android库即可(例如Logcat消息,Test-Toasts或Patterns.WEB_URL.matcher(url).matches()) [ android.util.Patterns ]即可验证字符串URL)正在纯JUnit测试项目中使用。

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

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