{"id":15166,"date":"2014-08-04T16:37:17","date_gmt":"2014-08-04T11:07:17","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=15166"},"modified":"2016-12-19T14:54:46","modified_gmt":"2016-12-19T09:24:46","slug":"grails-unit-test-filters-with-the-injected-service","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/grails-unit-test-filters-with-the-injected-service\/","title":{"rendered":"Grails Unit Test Filters with the injected Service"},"content":{"rendered":"<p>Recently I was writing unit tests for a filter in my project, which makes use of a service. <\/p>\n<p>Normally mocking using &#8216;metaclass&#8217; and &#8216;demand&#8217; doesn&#8217;t work for services in filter, because filters don&#8217;t let mock the service methods like we do in other classes or controller.<\/p>\n<p>Suppose we have a filter like:<\/p>\n<p>[java]<br \/>\nclass SomeFilters {<\/p>\n<p>    SomeService someService<\/p>\n<p>    def filters = {<br \/>\n        all(controller: &#8216;*&#8217;, action: &#8216;*&#8217;) {<br \/>\n            before = {<br \/>\n                if (params.checkSomeValue) {<br \/>\n                    someService.serviceMethod(params.checkSomeValue)<br \/>\n                    redirect(controller: &#8216;some&#8217;, action: &#8216;index&#8217;)<br \/>\n                }<br \/>\n                return true<br \/>\n            }<br \/>\n        }<br \/>\n    }<br \/>\n}<br \/>\n[\/java]<\/p>\n<p>and a Controller and the service like:<br \/>\n[java]<br \/>\nclass SomeController {<\/p>\n<p>    def index() {<br \/>\n        render &quot;this is index&quot;<br \/>\n    }<br \/>\n}<br \/>\n[\/java]<\/p>\n<p>[java]<br \/>\nimport grails.transaction.Transactional<\/p>\n<p>@Transactional<br \/>\nclass SomeService {<\/p>\n<p>    def serviceMethod(String check) {<br \/>\n        println &quot;this is the Service Method &quot;+check<br \/>\n    }<br \/>\n}<br \/>\n[\/java]<\/p>\n<p>To test the above mentioned filter using Grails spock we need to first mock the service. We can mock the service like:<br \/>\n[java]<br \/>\nclass MockedSomeService extends SomeService {<br \/>\n    @Override<br \/>\n    def serviceMethod(String check) {<br \/>\n        println &quot;this is the Service Method &quot; + check<br \/>\n    }<br \/>\n}<br \/>\n[\/java]<\/p>\n<p>all we need to do is to create a class which will extend the service class.<br \/>\nNow the question arises, how&#8217;ll we inject this mocked service class for our test cases.<br \/>\nHere&#8217;s answer to this question.<br \/>\n[java]<br \/>\ndefineBeans {<br \/>\n            someService(MockedSomeService)<br \/>\n        }<br \/>\n[\/java]<\/p>\n<p>this &#8216;defineBeans&#8217; closure helps us to inject the service with the new definition specified by the mocked class. Here, our service &#8216;someService&#8217; will use &#8216;MockedSomeService&#8217; as its definition, i.e. overriding the original definition &#8216;SomeService&#8217;.<\/p>\n<p>What&#8217;s next? This is it, now we can write rest of the test case as we do for normal filters.<br \/>\n[java]<br \/>\n@Mock([SomeFilters, SomeService])<br \/>\n@TestFor(SomeController)<br \/>\nclass SomeFiltersSpec extends Specification {<\/p>\n<p>    void &quot;test the filter&quot;() {<br \/>\n        setup:<br \/>\n        defineBeans {<br \/>\n            someService(MockedSomeService)<br \/>\n        }<br \/>\n        \/\/mock other methods and variables here<\/p>\n<p>        when:<br \/>\n        params.checkSomeValue = &#8216;checkSomeValue&#8217;<br \/>\n        withFilters(controller: &quot;*&quot;, action: &quot;*&quot;) {<br \/>\n            controller.index()<br \/>\n        }<\/p>\n<p>        then:<br \/>\n        assert response.status == 302<br \/>\n        assert response.redirectedUrl == &quot;\/some\/index&quot;<br \/>\n    }<br \/>\n}<\/p>\n<p>class MockedSomeService extends SomeService {<br \/>\n    @Override<br \/>\n    def serviceMethod(String check) {<br \/>\n        println &quot;this is the Service Method &quot; + check<br \/>\n    }<br \/>\n}<br \/>\n[\/java]<\/p>\n<p>Dont forget to add the important annotations, i.e.,<br \/>\n[java]<br \/>\n@Mock([SomeFilters, SomeService])<br \/>\n@TestFor(SomeController)<br \/>\n[\/java]<\/p>\n<p>I hope you wont find it too difficult to test the filters with services.<\/p>\n<p>You can also find the demo here.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Recently I was writing unit tests for a filter in my project, which makes use of a service. Normally mocking using &#8216;metaclass&#8217; and &#8216;demand&#8217; doesn&#8217;t work for services in filter, because filters don&#8217;t let mock the service methods like we do in other classes or controller. Suppose we have a filter like: [java] class SomeFilters [&hellip;]<\/p>\n","protected":false},"author":115,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":3},"categories":[7],"tags":[1497],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/15166"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/115"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=15166"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/15166\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=15166"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=15166"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=15166"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}