I have a EncouragementService.groovy
with following method
class EncouragementService {
def stripePaymentService
def encourageUsers(List&l
Service class can be optimized as below:
class EncouragementService {
def encourageUsers(List users){
if(users){ //Groovy truth takes care of all the checks
for(user in users){
//logic
}
}
}
}
Spock Unit Test:
Spock takes testing to whole another level, where you can test the behavior (adheres to BDD). The test class would look like:
import spock.lang.*
@TestFor(EncouragementService)
@Mock(User) //If you are accessing User domain object.
class EncouragementServiceSpec extends Specification{
//def encouragementService //DO NOT NEED: mocked in @TestFor annotation
void "test Encourage Users are properly handled"() {
given: "List of Users"
List users = createUsers()
when: "service is called"
//"service" represents the grails service you are testing for
service.encourageUsers(users)
then: "Expect something to happen"
//Assertion goes here
}
private def createUsers(){
return users //List
}
}