[英]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: 我通过以下步骤对其进行了更改:
extends android.test.AndroidTestCase
. extends android.test.AndroidTestCase
。 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
)。 @Test
before every Test-Method. @Test
。 assertSomething(...);
assertSomething(...);
to Assert.assertSomething(...);
Assert.assertSomething(...);
and all fail(...);
fail(...);
to Assert.fail(...);
Assert.fail(...);
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类的单独测试而不会出现错误:
import junit.framework.Assert;
import junit.framework.Assert;
and import org.junit.Test;
import org.junit.Test;
like mentioned above. @Test
and Assert.assertSomething(...);
) @Test
和Assert.assertSomething(...);
) Android API
or Android Google API
.) Android API
或Android Google API
。) 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.