gpt4 book ai didi

android - 无法使用 Realm + PowerMockito 在 Android Studio 中运行 Robolectric 测试

转载 作者:太空狗 更新时间:2023-10-29 15:50:51 28 4
gpt4 key购买 nike

我使用 Realm、PowerMockito 和 Robolectric 为我的应用程序编写测试。如果我使用 ./gradlew test,测试运行良好,但如果我使用 Android Studio 中的配置运行。它将显示如下错误。

com.thoughtworks.xstream.converters.ConversionException:无法构造 org.powermock.modules.junit4.rule.PowerMockStatement$1,因为它没有无参数构造函数:无法构造 org.powermock.modules.junit4 .rule.PowerMockStatement$1 因为它没有无参数构造函数

有人能解决这个问题吗?这是我用 Kotlin 编写的测试。

@RunWith(RobolectricGradleTestRunner::class)
@Config(application = TestApplication::class, constants = BuildConfig::class, sdk = intArrayOf(21))
@PowerMockIgnore("org.mockito.*", "org.robolectric.*", "android.*")
@SuppressStaticInitializationFor("io.realm.internal.Util")
@PrepareForTest(RealmCore::class, RealmLog::class, Realm::class, RealmResults::class, RealmQuery::class)
class RealmMiddlewareTest {

data class TestState(var item: List<Item> = listOf())

lateinit var realmMock: Realm

lateinit var mockRealmResults: RealmResults<Item>

val mockResults = arrayListOf(
Item().apply {
title = "item#1"
},
Item().apply {
title = "item#2"
},
Item().apply {
title = "item#3"
}
)

@get:Rule
val rule = PowerMockRule()

@Before
fun setUp() {
PowerMockito.mockStatic(RealmCore::class.java)
PowerMockito.mockStatic(RealmLog::class.java)
PowerMockito.mockStatic(Realm::class.java)
PowerMockito.mockStatic(RealmResults::class.java)
Realm.init(RuntimeEnvironment.application)
// Create the mock
realmMock = PowerMockito.mock(Realm::class.java)

// TODO: Better solution would be just mock the RealmConfiguration.Builder class. But it seems there is some
// problems for powermock to mock it (static inner class). We just mock the RealmCore.loadLibrary(Context) which
// will be called by RealmConfiguration.Builder's constructor.
PowerMockito.doNothing().`when`(RealmCore::class.java)
RealmCore.loadLibrary(Matchers.any(Context::class.java))
`when`(Realm.getDefaultInstance()).thenReturn(realmMock)

mockRealmResults = PowerMockito.mock(RealmResults::class.java) as RealmResults<Item>

val mockQuery = PowerMockito.mock(RealmQuery::class.java)
`when`(realmMock.where(Item::class.java)).thenReturn(mockQuery as RealmQuery<Item>)
`when`(mockQuery.findFirst()).thenReturn(mockResults[0])
`when`(mockQuery.findAll()).thenReturn(mockRealmResults)

`when`(mockRealmResults.iterator()).thenReturn(mockResults.iterator())
`when`(mockRealmResults.size).thenReturn(mockResults.size)


}

@Test
fun realm_transaction_action_successfully_committed() {
val testReducer = ReducerFn<TestState> { state, action ->
if (action is RealmTransactionAction.Committed) {
if (action.payloadType == Item::class.java.canonicalName) {
assertThat(action.payload as Item, sameInstance(mockResults[0]))
}
}
state
}
val store = SimpleStore(TestState(), testReducer).applyMiddleware(RealmMiddleware())

val transaction: (Realm) -> Item = {
it.copyToRealm(mockRealmResults[0])
}

val action = RealmTransactionAction.create(transaction = transaction)
store.dispatch(action)

verify(realmMock, times(1)).executeTransaction(Mockito.any())
verify(realmMock, times(1)).close()
}

@RealmClass
open class Item() : RealmObject() {
open var title: String = ""
}
}

最佳答案

testCompile 'org.powermock:powermock-module-junit4:1.6.5'
testCompile 'org.powermock:powermock-module-junit4-rule:1.6.5'
testCompile 'org.powermock:powermock-api-mockito:1.6.5'
testCompile 'org.powermock:powermock-classloading-xstream:1.6.5'

关于android - 无法使用 Realm + PowerMockito 在 Android Studio 中运行 Robolectric 测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40540893/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com