Android 使用真实实现的单元测试(类)
我想知道是否有办法避免嘲笑某些类。我正在做一个更大的与通知相关的单元测试。我遇到了多个问题,大部分问题我都能解决 现在我一直在使用一些类,比如Android 使用真实实现的单元测试(类),android,unit-testing,mockito,powermock,powermockito,Android,Unit Testing,Mockito,Powermock,Powermockito,我想知道是否有办法避免嘲笑某些类。我正在做一个更大的与通知相关的单元测试。我遇到了多个问题,大部分问题我都能解决 现在我一直在使用一些类,比如pendingent,Notification.Builder,甚至可能还有Notification。我使用的是compat库,因此我无法注入代码来模拟所有内容。我仍然不知道如何模拟构建器模式 你能给我一个如何避免Android Studio注入每次调用都返回null的伪对象的参考吗?我想列出一些课程的白名单。我的意思是,我知道有些类很容易模仿,比如Int
pendingent
,Notification.Builder
,甚至可能还有Notification
。我使用的是compat库,因此我无法注入代码来模拟所有内容。我仍然不知道如何模拟构建器模式
你能给我一个如何避免Android Studio注入每次调用都返回null的伪对象的参考吗?我想列出一些课程的白名单。我的意思是,我知道有些类很容易模仿,比如
Intent
或sharedreferences
,我发现了一个很棒的库,名为。以下是我测试所需的示例配置:
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"
}
pendingent
s无法使用,因为它需要一些本机实现,但是我可以模拟相关部分,以便对它们进行单元测试。@Spy
正是这样做的。然而,我不确定这是否正是你想要的。能否提供一个要测试的示例类,以及要为哪些字段运行具体实现。