2015-09-28 42 views
2

我想知道是否有辦法避免嘲笑某些類。我正在研究與通知有關的更大的單元測試。我遇到了多個問題,我主要可以解決這個問題。使用真實實現(類)的單元測試

現在我被困在一些類如PendingIntentNotification.Builder甚至可能Notification。我正在使用compat庫,所以我不能注入我的代碼來模擬一切。我仍然不知道如何嘲笑建設者模式。

您能否給我一個參考,我可以如何避免Android Studio注入每次調用都返回null的虛擬對象?我想列舉一些課程。我的意思是我知道有些課程很容易模擬,如IntentSharedPreferences

+0

'@ Spy'就是這樣做的。不過,我不確定這是不是你正在尋找的。你能否提供一個你想測試的示例類,以及你想運行哪些字段的具體實現。 – Magnilex

回答

2

我找到了一個叫unmock的精美圖書館,它正是我所需要的。這裏是我的示例配置,我需要爲我的測試:

unMock { 
    // URI to download the android-all.jar from. e.g. https://oss.sonatype.org/content/groups/public/org/robolectric/android-all/ 
    downloadFrom 'https://oss.sonatype.org/content/groups/public/org/robolectric/android-all/4.3_r2-robolectric-0/android-all-4.3_r2-robolectric-0.jar' 

    keep 'android.os.Bundle' 
    keepStartingWith 'android.content.Intent' 
    keepStartingWith 'android.content.ComponentName' 
    keep 'android.app.Notification' 
    keepStartingWith 'android.app.Notification$' 
    keep 'android.net.Uri' 
    keepStartingWith 'android.widget.RemoteViews' 
    keep 'android.util.SparseIntArray' 
    keep 'android.util.SparseArray' 
    keep 'com.android.internal.util.ArrayUtils' 
    keep 'com.android.internal.util.GrowingArrayUtils' 
    keep 'libcore.util.EmptyArray' 

    keepStartingWith "libcore." 
    keepStartingWith "com.android.internal.R" 
    keepStartingWith "com.android.internal.util." 
    keepAndRename "java.nio.charset.Charsets" to "xjava.nio.charset.Charsets" 
} 

,因爲它需要一些本地實現,但是我可以嘲笑的相關部分,所以我可以單元測試他們反正PendingIntent s不能被使用。