Spock interaction based test does not fail - unit-testing

I have the following two Spock tests:
def "sends a valid response when no users exist"() {
setup:
def exchange = new HttpServerExchange(Mock(ServerConnection))
usersRepository.size() >> 0
when:
firstRunHandler.handleRequest(exchange)
then:
1*response.send(exchange, _)
}
def "does not send content when any users exist"() {
setup:
usersRepository.size() >> 1
when:
firstRunHandler.handleRequest(new HttpServerExchange(Mock(ServerConnection)))
then:
0*response.send(_, _)
}
The second one should definitely fail, since the interaction is stll there. But it always passes. I can not even make it fail with:
then:
assert false
0*response.send(_, _)
IntelliJ Idea still shows it as "green". But when I change the "then" to
then:
assert false
the test fails, so it is definitely being run and executed as a spock test.
I don't get much info from Spock, and I did not find out anything when debugging. What am I missing? What can I do to diagnose this problem?

Related

Spock - Pass test successfully after timeout

In Spock, you can set time out, after which the test fails.
#Timeout(value = 1500, unit = TimeUnit.MILLISECONDS)
def "this method should not run longer than 2 seconds"() {
given:
userFilter = buildUserFilter(); // criteria to search users.
exportDestination = buildDestination(); // where to export users.
expect: "should not take long"
userService.exportUsers(userDetails);
}
Now I need something the opposite: the test should pass after timeout.
def "should block and wait until user-microservice is up"() {
given:
userExportMicroserviceClient = mock()
userExportMicroserviceClient.getUsers(_) >> httpNotFound
expect: "should block the current thread because userMicroservice is down"
failHere()
}
I know, it is a bad thing to depend on the other service availability. This thing runs only during data migration and it is not related to daily tasks.
Again, I have a method that gives users from user microservice, and it blocks until user microservice is up. How do I test it?
It depends how did you implement the exportUsers() method.
Assume it pulls a microservice every second. If so, you can emulate the response of the microservice in sequence: down, down, up.
// The timeout includes 2 seconds of microservice down.
#Timeout(value = 3500, unit = TimeUnit.MILLISECONDS)
def "should block and wait until user-microservice is up"() {
given: "mocked microservice which is not available for first 2 calls and available on 3rd call"
userExportMicroserviceClient = mock()
userExportMicroserviceClient.getUsers(_) >>> [httpNotFound, httpNotFound, httpOk]
when: "calling the user getting should take long time"
def actualResult = userService.exportUsers(userDetails)
then:
def expectedResult = <prepare>
actualResult == expectedResult
}
Sure things, this is a pseudo-code and requires yours project specific corrections.

Unit test grails with domain objects using GORM functions

I have the following piece of code inside a service class named OrderService in Groovy on Grails. I want to make a unit test for this class. User and Order are domain classed. A user has many orders.
boolean testfun(long userId, lond orderId){
User user = User.findByUserId(userId)
if(user == null)return false
Order order = Order.findByUserAndId(user, orderId)
if(order == null)return false
return true
}
The unit test that I am trying to write is the following (using Spock):
#TestFor(OrderService)
#Mock([User, Order])
class OrderServiceSpec extends Specification{
def "test funtest"() {
User user = new User(2)
Order order = new Order()
order.metaClass.id = 3// I want to assign the id of the order in domain
order.save()
user.addToOrders(order)
user.save()
expect:
service.testfun(2,3) == true
}
}
However this test fails because the order is null. Can anyone help me?
Another question is: is this test a unit test? or should I write an integration test in grails?
It depends on what you're actually trying to test, but this can be a unit test—I'd just recommend modifying it a little bit to isolate only the service method that you're interested in testing. You're not looking to test the domain classes at all, so it's best to mock/stub the behavior that you need from them to test the service functionality.
A good way to do this is with Spock's support for interaction based testing via mock objects. Basically we specify that when the service's testfun() method is called, we expect User.findById() to be called once and Order.findByUserAndId() to be called once as well. Spock then allows us to stub out each method call so that we specify what we want the method to return. When we run the test, the stub will be used, not the actual GORM method.
Some complexity lies with stubbing out static methods (like GORM methods), but you can use a GroovySpy to get the job done.
Also, I'm assuming you meant to use User.findById() instead of User.findByUserId()?
Something along these lines should work for you:
def "test funtest"() {
setup:
// Global so that it replaces all instances/references of the
// mocked type for the duration of the feature method.
GroovySpy(User, global: true)
GroovySpy(Order, global: true)
when:
def result = service.testfun(2,3)
then:
// No need to return real objects, so use a mock
1 * User.findById(2) >> Mock(User)
1 * Order.findByUserAndId(_ as User, 3) >> Mock(Order)
result == true
when:
result = service.testfun(2,3)
then:
1 * User.findById(2) >> null
result == false
}
Note that we've isolated the service method. Any collaborating objects (User and Order) are only being interacted with via stubs and we can test the functionality of the service method without worrying about GORM at all.

Class.metaclass.static say can not get static on null

Was writing unit test cases and at one point I needed to do some meta programming to test a method as below.
void "test method:resolver"(){
setup:"mocked resolver"
ContextHolder.getMetaClass().static.getBean = {
Resolver resolver = Mock(Resolver)
resolver(_) >> {HttpServletRequest request1->
return 1;
}
}
and:"mocked getAppName"
CoreUtil.metaClass.static.getAppName = {
return "$apiName"
}
when:
UserGroupRole userGroupRole = service.resolve(username)
then:
userGroupRole != null
where:
apiName || username
"core-blog" || "test11"
}
Following are the scenarios that I have gone through for running test cases:
When running test case individually, It works perfectly.
When running test case as a whole Specification i.e. run the Specification class itself, It works perfectly
But when running the test cases as whole by
grails test-app :unit
It fails saying Class.metaclass.static say can not get static on null
Please help!
If you're making metaclass changes in your tests, you need to clean up those metaclass changes in a cleanup step at the end of each test. Otherwise you risk test pollution.
setup
metaclass work
when
then
cleanup:
revoke the metaclass changes here
where

How to reset a mock invocation counter using Scalatra, Specs2, Mockito

I never expected that I will need to ask a question on this site because everything is already answered normally but with Scalatra... I haven't find a lot of information so here it is:
I'm not experienced with all that so maybe I'm missing something but from what I understand, if I want to test the API that I develop on Scalatra, I need to start the server everytime I run the test suit, right ?
Second question, how can I reset the invocation counter on a method so I don't have to calculate how many times the method has been called since the beginning of the test suite ? Right now using this give me more than one because it counts the previous test.
there was one(*instance*).*method*(*parameter*)
I can still get around the problem by either counting or putting the test as first test for now but it's not a sustainable solution...
Other thing that I found:
Reset method on the mock... not found
http://docs.mockito.googlecode.com/hg/org/mockito/Mockito.html#17
Isolating the test in a class scope:
We need to add
val servlet = new Servlet(eventRepoMock)
addServlet(servlet, "/*")
and we can't repeat the addServlet at every initialization
https://etorreborre.github.io/specs2/guide/SPECS2-3.5/org.specs2.guide.Isolation.html
Last thing that I try is:
servlet.repo = mock[EventRepo]
but repo being a value, I can't change it like this.
Neither of these "solutions" feel very clean so I was wondering if someone had a genius idea that can solve that mess !?
Thank you in advance !
EDIT:
Thanks to Eric's comment the above question are solve(that was easy) but now I have problem because I'm testing the get/post which are asynchronous call so resetting the mock does not happen at the right time... Any suggestion ?
Here's a simplified version of the code:
class EventServiceSpec extends ScalatraSpec with Mockito with Before { def is = s2"""
Event Service
GET an existing event
must return status 200 $get_status200
must return the event with id = :id $get_rightEventElement
must call findById once on the event repository $get_findByIdOnRepo
"""
lazy val anEvent = Event(1, "Some Event"
lazy val eventsBaseUrl = "/events"
lazy val existingEventUrl = s"$eventsBaseUrl/${anEvent.id}"
lazy val eventRepoMock = mock[EventRepository]
lazy val servlet = new Servlet(eventRepoMock)
addServlet(servlet, "/*")
def before = {
eventRepoMock.findById(anEvent.id) returns Option(anEvent)
eventRepoMock.findById(unexistingId) returns None
eventRepoMock.save(anEvent) returns Option(anEvent)
}
def get_status200 = get(existingEventUrl){
status must_== 200
}
def get_findByIdOnRepo = get(existingEventUrl){
// TODO count considering previous test... need to find a cleaner way
there was three(eventRepoMock).findById(anEvent.id)
}
All org.mockito.Mockito functions can still be used in a specs2 specification and reset is one of them.
Now, since you are sharing the state of a mock object across several examples, you not only need to reset the mock state before each example but you also need to make your specification sequential:
class EventServiceSpec extends ScalatraSpec with Mockito
with BeforeAll with BeforeEach {
def is = sequential ^ s2"""
Event Service
GET an existing event
must return status 200 $get_status200
must return the event with id = :id $get_rightEventElement
must call findById once on the event repository $get_findByIdOnRepo
"""
lazy val anEvent = Event(1, "Some Event")
lazy val eventsBaseUrl = "/events"
lazy val existingEventUrl = s"$eventsBaseUrl/${anEvent.id}"
lazy val eventRepoMock = mock[EventRepository]
lazy val servlet = new Servlet(eventRepoMock)
def beforeAll = addServlet(servlet, "/*")
def before = {
reset(eventRepoMock)
eventRepoMock.findById(anEvent.id) returns Option(anEvent)
eventRepoMock.findById(unexistingId) returns None
eventRepoMock.save(anEvent) returns Option(anEvent)
}
def get_status200 = get(existingEventUrl){
status must_== 200
}
def get_findByIdOnRepo = get(existingEventUrl){
there was one(eventRepoMock).findById(anEvent.id)
}
}

Explicitly verifying a mocha expectation after it's been called

I am using Mocha::Hooks#mocha_verify in the after_scenario spinach hook which works fine for some cases. However, there are many times when I want to verify an expectation after a value has been updated on a piece of data. For example,
class MyModel < ActiveRecord::Base
after_commit :checked, if: -> (record) { record.previous_changes.key?('checked_at') && record.checked_at? }
def checked
Bus.publish_at(checked_at + 1.day, 'checked', id: id)
end
end
Right now I'm having to set the expectation before the "act" part of the test runs so I have to do something like:
Bus.expects(:publish_at).with(
instance_of(ActiveSupport::TimeWithZone),
'checked',
has_entries(id: #my_model.id)
).once
The test data in #my_model still has nil checked_at because the "act" part of the test hasn't run yet, but I would like to verify that the first parameter is correct. I don't see a way to do this but it would be nice to be able to verify an invocation after the "act" part of the test like:
Bus.verify(:publish_at).with(
#my_model.checked_at + 1.day,
'checked',
has_entries(id: #my_model.id)
).once