How do I inject another service in a Grails UnitTest (Specification) using Spock?
class ServiceOne {
ServiceTwo serviceTwo
ModelOne getMethodOne(String name) {
// do somethings here
return serviceTwo.getMethodTwo(name)
}
}
class ServiceTwo {
ServiceTwo serviceTwo
ModelOne getMethodTwo(String name) {
// do somethings here
return ModelOne.get(name)
}
}
// Tests
import grails.test.mixin.Mock
import grails.test.mixin.TestFor
import spock.lang.Specification
/**
* Tests for ServiceOne
*/
@TestFor(ServiceOne)
@Mock(ModelOne)
class ServiceOneSpec extends Specification {
/** Inject serviceTwo in here
Otherwise the following exception is thrown
java.lang.NullPointerException: Cannot invoke method getMethodTwo() on null object
*/
void "test method one"() {
when:
ModelOne modelOne = service.getMethodOne(name)
then:
modelOne != null
}
}
This is how I solved my question - by creating an object of the Service (It is not singleton but it is fine since it is just for unit testing)
// Test Spec
import grails.test.mixin.Mock
import grails.test.mixin.TestFor
import spock.lang.Specification
/**
* Tests for ServiceOne
*/
@TestFor(ServiceOne)
@Mock(ModelOne)
class ServiceOneSpec extends Specification {
@Shared ModelOne modelOne
def setup() {
// the magic
service.serviceTwo = new ServiceTwo()
modelOne = service.serviceTwo.getMethodTwo("Name")
}
void "test method one"() {
when:
ModelOne modelOne = service.getMethodOne(name)
then:
modelOne != null
}
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments