groovy - 如何让 Spock 在类似于 Mockito 的 Reactor 中匹配?
问题描述
我正在尝试测试一个类,如果null
提供了一个值,它将调用另一个 Web 服务来获取数据。我的 JUnit + Mockito 测试效果很好,但我无法让我的 Spock 测试在then:
块中匹配。Spock 测试期间返回的错误是:
Suppressed: java.lang.NullPointerException: Cannot invoke method map() on null object
这是因为我的模拟方法不匹配,因此返回null
.
Spock测试(不工作)
class MySpec extends Specfication {
def mockCollaboratingService = Mock(CollaboratingService)
def service = new MyService(collaboratingService: mockCollaboratingService)
def "should call another service if the provided start value equals null"() {
given:
def id = 123
def startDate = null
when: "the service is called"
def result = service.getTransactions(id, startDate)
then:
1 * mockCollaboratingService.getData(id) >> Mono.just(new SomeMockResponse(key: "123"))
StepVerifier
.create(result)
.consumeNextWith({
// assertions here
})
.verifyComplete()
}
}
JUnit + Mockito(工作)
class AnotherSpec {
def mockCollaboratingService = Mockito.mock(MockCollaboratingService)
def service = new MyService(collaboratingService: mockCollaboratingService)
@Test
@DisplayName("should call the statement service if the given start date is null")
void shouldCallStatementServiceIfStartDateEqualsNull() {
def id = 123
def startDate = null
// and some fake data returned from the api
Mockito.when(mockCollaboratingService.getData(id)).thenReturn(Mono.just(new SomeMockResponse(key: "123")))
//when
def result = service.getTransactions(id, null)
//then
StepVerifier
.create(result)
.consumeNextWith({
Mockito.verify(mockStatementService, Mockito.times(1)).getLastStatement(enterpriseToken)
assert it.transactionId == 123
})
.verifyComplete()
}
}
解决方案
Spock 处理 mocking 与 mockito 不同,看看结合 mocking 和 stubbing。此外,所有交互都必须在then
块之前完成,因为 Spock 会首先验证它们。使用反应器,实际上StepVerifier
是在执行代码。这条线def result = service.getTransactions(id, startDate)
只会创建一个冷的 Mono/Flux,但不会产生任何效果。
因此,您必须稍微重新排序测试。
class MySpec extends Specfication {
def mockCollaboratingService = Mock(CollaboratingService)
def service = new MyService(collaboratingService: mockCollaboratingService)
def "should call another service if the provided start value equals null"() {
given:
def id = 123
def startDate = null
when: "the service is called"
def result = service.getTransactions(id, startDate)
and: "code is executed"
StepVerifier
.create(result)
.consumeNextWith({
// no explicit mock assertions necessary
assert it.transactionId == 123
})
.verifyComplete()
then:
1 * mockCollaboratingService.getData(id) >> Mono.just(new SomeMockResponse(key: "123"))
}
}
推荐阅读
- java - Java中从头开始的广度优先搜索
- blockchain - 我们如何知道区块链中每个账户的金额?
- reactjs - ECONNREFUSED - 代理来自 React 的 API 请求
- visual-studio-code - VSCode 任务,npm 脚本的用户输入
- ijson - ijson.common.IncompleteJSONError:词法错误:json 文本中的无效字符
- python - 带有 Flask 的 SQLALchemy 中的静态模型实例(夹具)
- azure-pipelines - 从构建中排除分支(Azure Pipelines)
- php - PHP按数组键值排序
- mongodb - 对MongoDB查询的质疑
- sql - 如何使用 SQL Server 触发器将数据插入 table_2(Log)