2015-09-28 35 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不能被使用。