YoYo Honey Singh
YoYo Honey Singh

Reputation: 464

how to mock util classes in grails service unit testing

class PropertyDetailsServiceSpec extends Specification {
void "test one"(){
    when:
    Map pdData = service.buildViewData()
    then:
    pdData != null
}
}

class PropertyDetailsService{
    buildViewData(){
      UtilClass obj=new UtilClass();
      obj.utilFunc();
    }
}

i want to mock utilFunc method how do i do that? grails version: 3.0.10

Upvotes: -1

Views: 266

Answers (1)

Jeff Scott Brown
Jeff Scott Brown

Reputation: 27255

The scenario described could be improved by allowing the helper to be injected into the service, which is generally a better idea. However, to address the question as asked, you can get there using runtime metaprogramming. The specifics my depend on factors not expressed in the question but you can do something like this in the test...

UtilClass.metaClass.utilFunc = { ->
    // put your test behavior here...
}

Upvotes: 2

Related Questions