Let's do it, with small test.
First my service class GreetService:
class GreetService {
def provider
def sayHello(String name){
provider.sayHello(name)
}
}
class DevProvider {
def sayHello(String name){
println "DevProvider: hello ${name}"
}
}
class TestProvider {
def sayHello(String name){
println "TestProvider: hello ${name}"
}
}
import grails.util.*
beans = {
switch(GrailsUtil.environment) {
case "test":
provider(TestProvider)
break
case "development":
provider(DevProvider)
break
}
}
class GreetServiceTests extends GroovyTestCase {
def greetService
void testGreet() {
greetService.sayHello("World!!!!!!")
}
}
// output: TestProvider: hello World!!!!!!
def service = ctx.getBean("greetService")
service.sayHello("world")
// output: DevProvider: hello world
No comments:
Post a Comment