为啥嘲笑 DAO 时 mockito 崩溃?

Posted

技术标签:

【中文标题】为啥嘲笑 DAO 时 mockito 崩溃?【英文标题】:Why mockito crash when mocking the DAO?为什么嘲笑 DAO 时 mockito 崩溃? 【发布时间】:2019-07-31 16:36:53 【问题描述】:

我目前正在编写单元测试。但是当我尝试模拟 DAO 接口时,mockito 崩溃了。 下面是测试代码。它基本上只是试图模拟一个界面。

class RepoTest 
    @Test
    fun testDelete() 
        val dao = Mockito.mock(ExchangeRateItemMainPageDAO::class.java)
    

DAO接口代码如下:

@Dao
interface ExchangeRateItemMainPageDAO 
    @Insert
    fun insertExchangeRateItem(item: ExchangeRateItemMainPage)

    @Delete
    fun deleteExchangeRateItem(item:ExchangeRateItemMainPage)

测试失败时给出的消息如下:

java.lang.ExceptionInInitializerError
    at org.mockito.internal.exceptions.stacktrace.ConditionalStackTraceFilter.<init>(ConditionalStackTraceFilter.java:17)
    at org.mockito.exceptions.base.MockitoException.filterStackTrace(MockitoException.java:41)
    at org.mockito.exceptions.base.MockitoException.<init>(MockitoException.java:30)
    at org.mockito.exceptions.misusing.MockitoConfigurationException.<init>(MockitoConfigurationException.java:18)
    at org.mockito.internal.configuration.plugins.PluginLoader.loadImpl(PluginLoader.java:66)
    at org.mockito.internal.configuration.plugins.PluginLoader.loadPlugin(PluginLoader.java:24)
    at org.mockito.internal.configuration.plugins.PluginRegistry.<init>(PluginRegistry.java:12)
    at org.mockito.internal.configuration.plugins.Plugins.<clinit>(Plugins.java:11)
    at org.mockito.internal.util.MockUtil.<clinit>(MockUtil.java:24)
    at org.mockito.internal.MockitoCore.<init>(MockitoCore.java:44)
    at org.mockito.Mockito.<clinit>(Mockito.java:975)
    at com.lazyfire.curencyexchg.currencypawn.viewModel.RepoTest.testDelete(MainActivityViewModelTest.kt:28)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.rules.TestWatcher$1.evaluate(TestWatcher.java:55)
    at org.junit.rules.RunRules.evaluate(RunRules.java:20)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
    Caused by: java.lang.NullPointerException
    at org.mockito.internal.configuration.plugins.Plugins.getStackTraceCleanerProvider(Plugins.java:17)
    at org.mockito.internal.exceptions.stacktrace.StackTraceFilter.<clinit>(StackTraceFilter.java:21)
    ... 36 more

【问题讨论】:

我在堆栈跟踪中看到了一些 NullPointerException(从底部算起的第 3 行)。我会使用调试器来检查什么引用为空。这可能是解决问题的好线索。 你在代码中的某处使用静态初始化块吗? 【参考方案1】:

抱歉,缓存出了点问题。只需使缓存无效并重新启动,它就可以正常工作了。

【讨论】:

这应该是您问题中的评论或对其进行编辑

以上是关于为啥嘲笑 DAO 时 mockito 崩溃?的主要内容,如果未能解决你的问题,请参考以下文章

CoreData SIGABRT 同时保存主 MOC。为啥?

Android:在JUnit中传递上下文并使用共享首选项

Mockito + Dexmaker 测试在 Android < 4.4 上运行时崩溃

核心数据 MOC 保存暂停执行并且无法保存(无错误或崩溃)

当mockito类作为依赖项传递时,Mockito不会返回mockValue

如何使用 Mockito 测试我的 DAO 方法?