在Android中进行单元测试遇到的问题

问题一、Cannot connect to VM  socket closed

在使用JUnit进行测试的时候,遇到这个问题。网上的解释是:使用Eclipse对Java代码进行调试,不管是远程JVM仍是本地JVM都会进行Socket通信.发生这样的错误是因为这些软件会修改winsock,还会监听和占用一些端口,Socket通信不上形成的。html

我经过cmd →ping localhost ,发现localhost指向::1,这是由于个人系统是win7 ,它支持IPv6的缘由。而Eclipse须要localhost指向127.0.0.1。因而就修改hosts文件(C:\Windows\System32\drivers\etc\hosts)。发现hosts中有两行被注释掉了(#后的东西表明被注释掉了)。java

# localhost name resolution is handled within DNS itself.
#    127.0.0.1       localhost
#    ::1             localhost

而后,去掉127.0.0.1前的#号,就能够了。若是没有127.0.0.1   localhost这行,则本身手动添加上去。这样咱们就将localhost重定向为127.0.0.1了。android

这个问题也多是本地的配置文件被修改,或防火墙开着的缘由。若是本地文件被修改,那么在cmd命令行里面输入netsh winsock reset命令就能够解决。windows

问题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中的位置在注释中。app

根据本身的程序,在AndroidManifest.xml文件中配置上面的信息。若是上面的信息配置正确,鼠标右键单击工程,选择Run As\Run configurations,在Android JUnit Test选项中选择工程,将会看到下面这个界面:框架

image

在Instrumentation runner后的列表框选项中,咱们看到android.test.InstrmentationTestRunner,并处于当前选择状态。若是这个没 有选择框中没有任何选项,就说明AndroidManifest.xml配置有问题。socket

问题三、Test run failed: Instrumentation run failed due to 'java.lang.ClassNotFoundException'

一、首先确保你已经引入了JUnit测试框架,添加的办法是:右键点你的项目→选中“Build Path”→选中“Configure Build Path…”→在Libraries选项卡中点击“Add Library”(以下图)→ 添加JUnit4测试框架jsp

image

二、记得在“Order and Export”选项卡中添加JUnit 4的依赖(以下图)。单元测试

image

 

问题四、在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:136), pid=5560, tid=5584
#  fatal error: Invalid layout of preloaded class
#
# JRE version:  (7.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的启动依赖便可。

image

 

 

 

参考连接

 如何进行Android单元测试

Android上的单元测试

在Android上实现Junit单元测试的四部曲

相关文章
相关标签/搜索