如何让Spock在类似于Mockito的Reactor中匹配?
How to get Spock to match in Reactor similar to Mockito?
我正在尝试测试 class,如果提供了 null
值,它将调用另一个 Web 服务来获取数据。我的 JUnit + Mockito 测试效果很好,但我无法让我的 Spock 测试在 then:
块中匹配。 Spock 测试时返回的错误是:
Suppressed: java.lang.NullPointerException: Cannot invoke method map() on null object
这是因为我模拟的方法不匹配,因此 returns 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 处理模拟的方式与 mockito 不同,请查看 combining mocking and stubbing。此外,所有交互都必须在 then
块之前完成,因为 Spock 会首先验证它们。使用 reactor,实际上是 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"))
}
}
我正在尝试测试 class,如果提供了 null
值,它将调用另一个 Web 服务来获取数据。我的 JUnit + Mockito 测试效果很好,但我无法让我的 Spock 测试在 then:
块中匹配。 Spock 测试时返回的错误是:
Suppressed: java.lang.NullPointerException: Cannot invoke method map() on null object
这是因为我模拟的方法不匹配,因此 returns 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 处理模拟的方式与 mockito 不同,请查看 combining mocking and stubbing。此外,所有交互都必须在 then
块之前完成,因为 Spock 会首先验证它们。使用 reactor,实际上是 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"))
}
}