Verify Spock mock with specified timeout - unit-testing

In Mockito there is option to verify if mock method has been called, and specify timeout for this verification (VerificationWithTimeout), for example:
verify(mock, timeout(200).atLeastOnce()).baz();
It there any equivalent to such functionality in Spock?

I was trying to use PollingConditions to satisfy a similar scenario (which wasn't helpful), but instead found satisfaction in Spock's BlockingVariables. To verify that SomeService.method() is invoked at least once in function ClassBeingTested.method() within a given timeout period:
def "verify an interaction happened at least once within 200ms"(){
given:
def result = new BlockingVariable<Boolean>(0.2) // 200ms
SomeService someServiceMock = Mock()
someServiceMock.method() >> {
result.set(true)
}
ClassBeingTested clazz = new ClassBeingTested(someService: someServiceMock)
when:
clazz.someMethod()
then:
result.get()
}
When the result is set, the blocking condition will be satisfied and result.get() would have to return true for the condition to pass. If it fails to be set within 200ms, the test will fail with a timeout exception.

There is no equivalent in Spock. (PollingConditions can only be used for conditions, not for interactions.) The closest you can get is to add a sleep() statement in the then block:
when:
...
then:
sleep(200)
(1.._) * mock.baz()

Using PollingConditions and a boolean variable, the following example evaluates a function until it satisfies an interaction.
def "test the config watcher to reload games if a new customer directory is added"() {
given:
def conditions = new PollingConditions(initialDelay: 1, timeout: 60, factor: 1.25)
def mockGameConfigLoader = Mock(GameConfigLoader)
def gameConfigWatcher= new GameConfigWatcher(mockGameConfigLoader)
boolean gamesReloaded = false
when:
conditions.eventually {
gameConfigWatcher.listenEvents()
assert gamesReloaded
}
then:
1 * mockGameConfigLoader.loadAllGameConfigs() >> {
gamesReloaded = true
}
0 * _
}

This doesn't do exactly what the question asked, but I found it a bit cleaner that using a variable. If you have other conditions to asynchronously test in addition to the interaction, then you can declare the interactions at mock creation time and then use PollingConditions to test the other conditions. PollingConditions will either fail the test or block until the conditions pass, so that by the time the interaction is tested, the method should have been called:
#MicronautTest
class KernelManagerTest extends Specification {
#Inject
KernelManager kernelManager
//create conditions
PollingConditions conditions = new PollingConditions(timeout: 10, initialDelay: 1)
class Exits {
static createKernel (String[] args) {
System.exit(args[0].toInteger())
}
}
def "handles a system exit from within kernel"() {
given:
// set custom kernel
kernelManager.kernelClass = Exits
// create custom logger
kernelManager.log = Mock(Logger) {
1 * warn("Kernel exited unexpectedly.", _ as UnexpectedExitException)
}
when:
// create new kernel (exit 1)
kernelManager.startNewKernel("1")
then:
conditions.eventually {
kernelManager.kernelInstances.size() == 0
kernelManager.kernelThreads.size() == 0
}
}
}

Related

Spock interaction verification ignoring call to Mock method

The following Spock test is failing to not counting the call to the Mock method:
def setup() {
mojo = new PactCreateVersionTagMojo()
mojo.pactBrokerUrl = 'http://broker:1234'
mojo.pacticipant = 'test'
mojo.pacticipantVersion = '1234'
mojo.tag = 'testTag'
}
def 'calls pact broker client with mandatory arguments'() {
given:
mojo.brokerClient = Mock(PactBrokerClient)
when:
mojo.execute()
then:
notThrown(MojoExecutionException)
1 * mojo.brokerClient.createVersionTag(
'test', '1234', 'testTag')
}
You can find it here.
The SUT code, removing the argument validation code, is:
class PactCreateVersionTagMojo : PactBaseMojo() {
override fun execute() {
...
createVersionTag()
}
private fun createVersionTag() =
brokerClient!!.createVersionTag(pacticipant!!, pacticipantVersion.orEmpty(), tag.orEmpty())
You can find it here.
The error is as follows:
I have a very similar example on the same project that passes just fine:
def 'passes optional parameters to the pact broker client'() {
given:
mojo.latest = 'true'
mojo.to = 'prod'
mojo.brokerClient = Mock(PactBrokerClient)
when:
mojo.execute()
then:
notThrown(MojoExecutionException)
1 * mojo.brokerClient.canIDeploy('test', '1234',
new Latest.UseLatest(true), 'prod') >> new CanIDeployResult(true, '', '')
}
override fun execute() {
...
val result = brokerClient!!.canIDeploy(pacticipant!!, pacticipantVersion.orEmpty(), latest, to)
}
You can find the test above here and the SUT here.
I have investigated the call that happens during the test, and it seems as expected.
Additionally, I try to create the verification with wildcard argument constraints, but it still didn't work.
It seems to me that I have misconfigured my test, but I can't spot the difference between the test that passes and my failing test.
Your fun createVersionTag(..) looks like this:
fun createVersionTag(
pacticipant: String,
pacticipantVersion: String,
tag: String) {
}
I do not speak Kotlin, but I think you ought to open the method because otherwise it is final, which means it cannot be overridden by a subclass and thus not be mocked or stubbed by conventional means. This is also the difference to open fun canIDeploy(..).

Unit testing retries with project reactor

I have a piece of code that goes something like this:
somePublisher
.subscribeOn(...)
.flatMap { x -> someFunctionThatReturnsMono(x) }
.retry(3)
.subscribe()
So far I have managed to unit test happy paths, like whether the code inside map {...} is getting called, using the tools from reactor-test.
Now I want to test errors and retries. How can I test to make sure someFunctionThatReturnsMono(x) is getting called at most 4 times when consecutive errors occur?
Simple way is to mock function and count its calls:
def 'Test retry'(){
setup:
someBean = Mock(SomeBean)
def testMono = Mono.just(X)
.flatMap { x -> someBean.someFunctionThatReturnsMono(x) }
.retry(3)
.subscribeOn(Schedulers.elastic())
when:
StepVerifier.create(testMono).verifyError(IllegalArgumentException)
then:
4 * someBean.someFunctionThatReturnsMono(_ as X) >> Mono.error(new IllegalArgumentException())
}
Another one is to mock funtion to return PublisherProbe and count probe subscriptions:
def 'Test retry'() {
when:
PublisherProbe probe = PublisherProbe.of(Mono.error(new IllegalArgumentException()))
someBean = Mock(SomeBean) {
someFunctionThatReturnsMono(_) >> probe
}
def testMono = Mono.just(X)
.flatMap { x -> someBean.someFunctionThatReturnsMono(x) }
.retry(3)
.subscribeOn(Schedulers.elastic())
then:
StepVerifier.create(testMono).verifyError(IllegalArgumentException)
probe.subscribeCount() == 4
}
This is how I do it for one of my use-cases:
def "should retry failed action given number of times"() {
given:
def invocationCounter = new AtomicInteger(0)
when:
Mono<Void> mono = myService.doSth()
.doOnError { invocationCounter.incrementAndGet() }
then:
StepVerifier.create(mono)
.verifyErrorMatches { it == expectedError }
and:
invocationCounter.get() == 1 + retryNum
}

Unit testing a GORM call with a closure

I have a Grails service that does a where query like this:
List<Car> search(Long makeId = null) {
Car.where {
join("make")
if(makeId) {
make.id == makeId
}
}.findAll()
}
I'm trying to unit test it with Spock like this:
def setup() {
GroovyMock(Car, global: true)
}
void "test search"() {
when:
service.search()
then:
1 * Car.where {}
}
However, I can't seem to find a way to test the content of the closure.
I can get the test to pass by verifying 1 * Car.where(_), but how can I make assertions on the content of the closure, i.e. that join was called and that the make.id constraint is specified only when needed?
You could set the closure's delegate to be an Mock of DetachedCriteria to make assertions on it. DetachedCriteria is the main class used in gorm to build the query.
Example:
given: 'Mocking DetachedCriteria'
DetachedCriteria detachedCriteriaMock = Mock(DetachedCriteria)
and: 'Just to avoid nullPointerException when findAll() call happens on service'
1 * detachedCriteriaMock.iterator() >> [].listIterator()
when:
service.search(1L)
then:
// Capture the argument
1 * Car.where(_) >> { args ->
args[0].delegate = detachedCriteriaMock
args[0].call()
return detachedCriteriaMock
}
// Join is a method on detached criteria
1 * detachedCriteriaMock.join('make')
// Make is an association, so detachedCriteria uses the methodMissing to find the property.
// In this case, we call the closure setting the delegate to the mock
1 * detachedCriteriaMock.methodMissing('make', _) >> { args ->
// args[1] is the list of arguments.
// args[1][0] is the closure itself passed to detachedCriteria
args[1][0].delegate = detachedCriteriaMock
args[1][0].call()
}
// If id is passed, it must compare (eq method) with value 1
1 * detachedCriteriaMock.eq('id', 1L)

Spock Testing and mocking or stubing database interaction

I have a private controller method that is used by multiple actions to retrieve an object from the database. I can not for the life of me correctly mock/stub the call to the database. The Controller method is:
private Order getSalesOrder(){
def order = Order.get(params.id)
if(!order){
flash.message = (code: 'default.not.found.message', args: [message(code: 'order.label', default: 'Order'), params.id])
redirect action: "list"
return
}
return order
}
The Test method I have at this point is:
def "test getSalesOrder returns Sales Order"(){
given:
params.id >> 3002L
criteriaSetup()
Order testOrder = salesOrders[2]
Order.metaClass.static.get() >> testOrder
when:
def order = controller.getSalesOrder()
then:
1 * Order.get(3002) >> testOrder
//1 * Order.get() >> salesOrders[2]
order == testOrder
}
My results are either a message about too few invocations or I just get a null value back. Both of which cause the test to fail.
I have tried variations of this by using examples found from various blogs or tutorials such as:
Order.metaClass.static.get() >> testOrder
Order.metaClass.methods.get = { return testOrder }
1 * Order.get(3002) >> testOrder
1 * Order.get(params.id) >> testOrder
I'm not sure where to go from here, since I'm new to spock tests, Mocking and Stubing in general. And due to the fact that we are writing test for legacy code it is even more confusing.
You could simply use #Mock([Order]) on top of your test class declaration
I think your failure case of too few invocations is the correct one. I don't think you can check 1 * Order.get(3002). This call will always return 0 invocations, and you would be checking for 1.
You would either have to mock this method with a counter in it to do the count check.

Groovy Spock File test

I'm about to start learning spock and I'm trying some basic stuff.
I want to check File's: exist() and getText() functionality
So I wrote the following test:
class MyTestSpec extends Specification {
def "test file"() {
given:
def mockFile = Mock(File,constructorArgs :["./doesNotExist.txt"])
mockFile.exists() >> true
mockFile.getText() >> "sampleText"
when:
def res = ""
if(mockFile.exists()) {
res = mockFile.getText()
}
then:
"sampleText" == res
1 * mockFile.exists()
1 * mockFile.getText()
}
}
This fails on:
Too few invocations for:
1 * mockFile.getText() (0 invocations)
Unmatched invocations (ordered by similarity):
None
When I comment the 'verifications' in 'then' block, I get:
java.lang.NullPointerException at
java.io.FileInputStream.(FileInputStream.java:138) at
groovy.util.CharsetToolkit.(CharsetToolkit.java:69) at
MyTestSpec.Test Existing Resource(MyTestSpec.groovy:83)
So my question is: How Exactly I can organize my test? Why does it assume that getText should not be called?
I'm using groovy 2.4 and spock 1.0
The solution will be:
#Grab('org.spockframework:spock-core:0.7-groovy-2.0')
#Grab('cglib:cglib:3.1')
#Grab('org.ow2.asm:asm-all:5.0.3')
import spock.lang.*
class MyTestSpec extends Specification {
def "test file"() {
given:
def mockFile = GroovyMock(File, constructorArgs :["./doesNotExist.txt"])
when:
def res = ""
if(mockFile.exists()) {
res = mockFile.getText()
}
then:
"sampleText" == res
1 * mockFile.exists() >> true
1 * mockFile.getText() >> "sampleText"
}
}
On of the problems is creating a mock. Because of a dynamic nature of groovy some functionality - e.g. getText() method for File class - is added at runtime. It requires mocks to be constructed in a different way. Have a look at spock mock implementation enum and extract:
An implementation specifically targeting Groovy callers. Supports mocking of dynamic methods, constructors, static methods, and "magic" mocking of all objects of a particular type.
The second problem is defining mock behavior and verifying the interactions. When you both mock and stub it must happen in the same interaction (here, in then block), here is he relevant part of the docs.