Reputation: 3423
I've got a service, which is using sendJMSMessage method which is provided by jms-grails plugin. I want to write some unit tests, but I'm not sure how to "mock" this method, so it just does nothing at all. Any tips?
Upvotes: 3
Views: 607
Reputation: 3243
You can metaClass the method to have it return whatever you want.
@Test
void pluginCode() {
def myService = new MyService()
def wasCalled = false
myService.metaClass.sendJMSMessage = {String message ->
//I like to have an assert in here to test what's being passed in so I can ensure wiring is correct
wasCalled = true
null //this is what the method will now return
}
def results = myService.myServiceMethodThatCallsPlugin()
assert wasCalled
}
I like to have a wasCalled
flag when I'm returning null
from a metaClassed method because I don't particularly like asserting that the response is null
because it doesn't really assure that you're wired up correctly. If you're returning something kind of unique though you can do without the wasCalled
flag.
In the above example I used 1 String parameter but you can metaClass
out any number/type of parameters to match what actually happens.
Upvotes: 2