在Android中进行单元测试遇到的问题
问题1、Cannot connect to VM socket closed
在使用JUnit进行测试的时候,遇到这个问题。网上的解释是:使用Eclipse对Java代码进行调试,无论是远程JVM还是本地JVM都会进行Socket通讯.发生这样的错误是由于这些软件会修改winsock,还会监听和占用一些端口,Socket通讯不上造成的。
我通过cmd →ping localhost ,发现localhost指向::1,这是因为我的系统是win7 ,它支持IPv6的原因。而Eclipse需要localhost指向127.0.0.1。于是就修改hosts文件(C:\Windows\System32\drivers\etc\hosts)。发现hosts中有两行被注释掉了(#后的东西代表被注释掉了)。
# localhost name resolution is handled within DNS itself.
# 127.0.0.1 localhost
# :: localhost
然后,去掉127.0.0.1前的#号,就可以了。如果没有127.0.0.1 localhost这行,则自己手动添加上去。这样我们就将localhost重定向为127.0.0.1了。
这个问题也可能是本地的配置文件被修改,或防火墙开着的原因。如果本地文件被修改,那么在cmd命令行里面输入netsh winsock reset命令就可以解决。
问题2:在Android中进行单元测试,需要在项目中的Manifest.xml文件中添加一些必要的配置。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.bang.test"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<application android:icon="@drawable/icon" android:label="@string/app_name"> <!-- 在本应用中导入需要使用的包,放在application里面activity外面 -->
<uses-library android:name="android.test.runner" />
<activity android:name=".JunitTestActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity> </application>
<uses-permission android:name="android.permission.RUN_INSTRUMENTATION" />
<!-- 记住这个一要放在application外面,不然会出现配置错误 信息 -->
<instrumentation android:name="android.test.InstrumentationTestRunner"
android:targetPackage
="com.bang.test" android:label="Tests for My App" />
</manifest>
必须要添加的配置,已经在上面的示例配置文件中用灰色背景标出来了,配置需要放置Manifest中的位置在注释中。
根据自己的程序,在AndroidManifest.xml文件中配置上面的信息。如果上面的信息配置正确,鼠标右键单击工程,选择Run As\Run configurations,在Android JUnit Test选项中选择工程,将会看到下面这个界面:
在Instrumentation runner后的列表框选项中,我们看到android.test.InstrmentationTestRunner,并处于当前选择状态。如果这个没 有选择框中没有任何选项,就说明AndroidManifest.xml配置有问题。
问题3、Test run failed: Instrumentation run failed due to 'java.lang.ClassNotFoundException'
1、首先确保你已经引入了JUnit测试框架,添加的办法是:右键点你的项目→选中“Build Path”→选中“Configure Build Path…”→在Libraries选项卡中点击“Add Library”(如下图)→ 添加JUnit4测试框架
2、记得在“Order and Export”选项卡中添加JUnit 4的依赖(如下图)。
问题4、在Android项目中的测试类点击"run as JUnit test"出错
控制台中会有一段错误提示
Invalid layout of java.lang.String at value
#
# A fatal error has been detected by the Java Runtime Environment:
#
# Internal Error (javaClasses.cpp:), pid=, tid=
# fatal error: Invalid layout of preloaded class
#
# JRE version: (.0_40-b43) (build )
# Java VM: Java HotSpot(TM) Client VM (24.0-b56 mixed mode windows-x86 )
# Failed to write core dump. Minidumps are not enabled by default on client versions of Windows
#
# An error report file with more information is saved as:
# D:\我的文档\workspace\solarTest\hs_err_pid5560.log
#
# If you would like to submit a bug report, please visit:
# http://bugreport.sun.com/bugreport/crash.jsp
#
后面发现是debug configuration的问题。我的测试项目的BootStrap Entries,默认是Android2.3.3。只要去掉这个东西就行了。只要右键点你的项目→选中“Debug As”→ 选择“Debug Configurations”→然后按下图操作,去掉对Android2.3.3的启动依赖即可。
参考链接
在Android中进行单元测试遇到的问题的更多相关文章
- Android中的单元测试
2015年5月19日 23:10 在Android中,已经内置了Junit所以不需要在导包.只要继承AndroidTestCase类就可以了. 首先需要修改AndroidManifes ...
- 在android中进行单元测试的步骤
若不知道怎么配上面两个参数
- Android中使用HTTP服务
在Android中,除了使用java.net包下的API访问HTTP服务之外,我们还可以换一种途径去完成工作.Android SDK附带了Apache的HttpClient API.Apache Ht ...
- 在Android Studio中进行单元测试和UI测试
本篇教程翻译自Google I/O 2015中关于测试的codelab,掌握科学上网的同学请点击这里阅读:Unit and UI Testing in Android Studio.能力有限,如有翻译 ...
- Android中如何使用JUnit进行单元测试 eclipse
Android中如何使用JUnit进行单元测试 在我们日常开发android app的时候,需要不断地进行测试,所以使用JUnit测试框架显得格外重要,学会JUnit可以加快应用的开发周期. Andr ...
- Android中后台的劳动者“服务”
前言 作为四大组件之一的Service,想必不少开发者都是了解的,那具体熟悉吗?是不是对Service中的每个知识点是否了解,它与Activity的关系又是什么样的,我们所理解的后台服务跟Servic ...
- Android笔记——Android中数据的存储方式(三)
Android系统集成了一个轻量级的数据库:SQLite,所以Android对数据库的支持很好,每个应用都可以方便的使用它.SQLite作为一个嵌入式的数据库引擎,专门适用于资源有限的设备上适量数据存 ...
- Android系列----JUnit单元测试的使用
[声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/ ...
- Android中的单位及测试相关概念
android中的单位: in 英寸 pt 点距 px 像素 dp(dip) 密度无关的像素单位,自适应device屏幕的比例,通常涉及长宽高时采用 sp 与范围无关的像素单位,通常在设置字体大小时 ...
随机推荐
- CSS3常用选择器(二)
本文继续介绍css3新增的选择器. 1.选择器 first-child.last-child.nth-child 和 nth-last-child 利用这几个选择器能够针对一个父元素中的第一个子元素. ...
- phpstorm
9XVKERIY9F-eyJsaWNlbnNlSWQiOiI5WFZLRVJJWTlGIiwibGljZW5zZWVOYW1lIjoiYXNoZXIgY2hlbiIsImFzc2lnbmVlTmFtZ ...
- C++ 中int,char,string,CString类型转换
1. c++中string到int的转换 1) 在C标准库里面,使用atoi: #include <cstdlib> #include <string> std::stri ...
- Python UTF8 读取文件
严禁伸手党:
- ORACLE 分析函数整理汇总
1. 聚合分析函数 SUM MIN MAX AVG COUNT 这类聚合类分析函数可以在窗口中分组
- 分布式Hbase-0.98.4在Hadoop-2.2.0集群上的部署
fesh个人实践,欢迎经验交流!本文Blog地址:http://www.cnblogs.com/fesh/p/3898991.html Hbase 是Apache Hadoop的数据库,能够对大数据提 ...
- RedHat下编译安装Boost
1.解压boost_1_54_0.tar.gz 2.进入目录后,运行 ./bootstrap.sh ,会生成一个 bjam 的可执行程序 3.运行 ./bjam release install 进行编 ...
- java1.7集合源码阅读: Stack
Stack类也是List接口的一种实现,也是一个有着非常长历史的实现,从jdk1.0开始就有了这个实现. Stack是一种基于后进先出队列的实现(last-in-first-out (LIFO)),实 ...
- CCKJ 笔试
面向对象三个特性: 面向对象技术是目前流行的系统设计开发技术,它包括面向对象分析和面向对象程序设计.面向对象程序设计技术的提出,主要是为了解决传统程序设计方法--结构化程序设计所不能解决的代码重用问题 ...
- Boo who
function boo(bool) { // What is the new fad diet for ghost developers? The Boolean. //return bool; r ...