在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
#    ::1             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選項中選擇工程,將會看到下面這個界面:

image

在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測試框架

image

2、記得在“Order and Export”選項卡中添加JUnit 4的依賴(如下圖)。

image

 

問題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: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單元測試的四部曲


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM