当前位置: 首页 > 知识库问答 >
问题:

模拟返回void返回类型的存根

傅啸
2023-03-14

我有下面的课,我试图测试。我遇到问题的方法是ShowsCollerView,因为我试图存根/模拟行为,然后在测试中验证行为。

class CustomScrollerView @JvmOverloads constructor(
        context: Context,
        attributeSet: AttributeSet? = null,
        styleAttributes: Int = 0)
    : ConstraintLayout(context, attributeSet, styleAttributes) {

    private var fragment: ConstraintLayout by Delegates.notNull()
    private var layoutResEnding: Int = 0
    private val transition = ChangeBounds()
    private val constraintSet = ConstraintSet()
    private var isShowing = false

    init {
        View.inflate(context, R.layout.overview_scroller_view, this)
        transition.interpolator = AccelerateInterpolator()
        transition.duration = 300
    }

    fun <L: ConstraintLayout> setView(view: L) {
        fragment = view
    }

    fun setLayoutResourceFinish(@LayoutRes id: Int) {
        layoutResEnding = id
    }

    fun showScrollerView() {
        constraintSet.clone(context, layoutResEnding)
        TransitionManager.beginDelayedTransition(fragment, transition)
        constraintSet.applyTo(fragment)
        isShowing = true
    }

    fun isScrollViewShowing() = isShowing
}
class CustomScrollerViewTest: RobolectricTest() {
    @Mock
    lateinit var constraintSet: ConstraintSet
    @Mock
    lateinit var constraintLayout: ConstraintLayout

    private var customScrollerView: CustomScrollerView by Delegates.notNull()

    @Before
    fun setup() {
        customScrollerView = CustomScrollerView(RuntimeEnvironment.application.baseContext)
    }

    @Test
    fun `test that CustomScrollerView is not null`() {
        assertThat(customScrollerView).isNotNull()
    }

    @Test
    fun `test that the scrollerView is shown`() {
        doNothing().`when`(constraintSet.clone(RuntimeEnvironment.application.baseContext, R.layout.fragment)) /* Error here */
        doNothing().`when`(constraintSet).applyTo(constraintLayout)

        customScrollerView.setLayoutResourceFinish(R.layout.fragment)
        customScrollerView.setView(constraintLayout)
        customScrollerView.showScrollerView()

        assertThat(customScrollerView.isScrollViewShowing()).isEqualTo(true)
        verify(constraintSet).applyTo(constraintLayout)
        verify(constraintSet).clone(RuntimeEnvironment.application.baseContext, R.layout.fragment)
    }
}
doNothing().when(constraintSet.clone(RuntimeEnvironment.application.baseContext, R.layout.fragment))

共有1个答案

逑景铄
2023-03-14

您得到错误的行应该是:

doNothing().`when`(constraintSet).clone(RuntimeEnvironment.application.baseContext, R.layout.fragment)

就像这里javadoc中的示例一样:

List list = new LinkedList();
List spy = spy(list);

//let's make clear() do nothing
doNothing().when(spy).clear();

spy.add("one");

//clear() does nothing, so the list still contains "one"
spy.clear();
 类似资料:
  • 我正在使用Junit 5和mockito进行一些单元测试。 要被模拟的方法调用如下。它返回一个Mono并接受两个String参数。 我嘲笑它如下 这会产生一个空指针,如下所示

  • 我有一个关于显示void类型的返回值的问题。因此trimLines是一个void类型的方法。我不明白如何显示void类型的方法,因为该方法没有返回任何东西,所以没有任何东西可以显示。由于采用字符串类型,这将导致错误。

  • 我得到了这个错误:这个表达式的类型是'void',所以它的值不能被使用。尝试检查是否使用了正确的API;可能会有一个函数或调用返回您意想不到的void。还要检查类型参数和变量,它们也可能是空的。 代码: null null 我不明白这是什么。我是新手。这是我的第一个应用程序。有人能帮我一下吗。

  • 我在尝试单元测试函数调用时遇到了一个问题。尽管调用已被存根,但由于无效方法调用而失败。 请在下面找到我的代码的简化快照。我正在使用do答案()存根来模拟空方法(基于StackOverflow上的早期答案)。 我甚至尝试了其他选项的和存根,但当调用存根方法时,它们也会在相同的NPE中失败:(。 如果有人能提出解决方案/解决方法,我将不胜感激。非常感谢。 考试班 正在测试的实现类,来自该类的存根方法调

  • 我想创建一个任务来运行串行命令。此时,我不需要从正在进行工作的方法返回任何内容。这可能会在以后发生变化,但我现在很好奇这是如何发生的。 这就是我所拥有的。我想为任务使用单独的方法,而不是创建匿名操作。我尝试返回void,结果是“void不能显式转换为任务”。我也试过了<代码>任务 在此过程中,我使用了一个线程来完成这项任务,但这次我想使用任务。 预计到达时间: 最后,这是我的完整解决方案

  • 我想写一个函数返回一个数组,其所有子数组的长度必须为2。例如,返回将是。 我定义: (1); 和 我觉得(1)太复杂了。有更简单的吗?