gpt4 book ai didi

java - 如何模拟/ stub jOOQ UpdatableRecordImpl store() 方法?

转载 作者:搜寻专家 更新时间:2023-11-01 03:31:34 24 4
gpt4 key购买 nike

我已经设法 stub update():

@Test
public void updateStubbed() {
UpdatableRecordTest updatableRecordTest = spy(new UpdatableRecordTest());
doReturn(1).when(updatableRecordTest).update();
}

但是,当我尝试 stub store() 时:

@Test
public void storeStubbed() {
UpdatableRecordTest updatableRecordTest = spy(new UpdatableRecordTest());
doReturn(1).when(updatableRecordTest).store();
}

我得到错误:

org.mockito.exceptions.misusing.WrongTypeOfReturnValue:
Integer cannot be returned by fieldsRow() fieldsRow() should return Row


If you're unsure why you're getting above error read on. Due to the nature of the syntax above problem might occur because:

  1. This exception might occur in wrongly written multi-threaded tests. Please refer to Mockito FAQ on limitations of concurrency testing.
  2. A spy is stubbed using when(spy.foo()).then() syntax. It is safer to stub spies -
    • with doReturn|Throw() family of methods. More in javadocs for Mockito.spy() method.

如何 stub store()方法?

最佳答案

您不能使用 Mockito 模拟 store() 方法,因为它是一个 final 方法,并且 Mockito 不支持模拟 final 方法。您可以做的是:

  • 对于单元测试,使用 PowerMock(它提供了一种模拟 final 方法的方法,但对 Spring 不友好。只是提一下,以防你使用 Spring),
  • 对于功能测试,创建嵌入式数据库架构,并配置您的 JOOQ 以在测试环境中使用它。

很遗憾,我没有更好的答案。

关于java - 如何模拟/ stub jOOQ UpdatableRecordImpl store() 方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51521457/

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