Showing posts with label Grails. Show all posts
Showing posts with label Grails. Show all posts

Tuesday, June 10, 2014

Grails - dataBind in Service Layer

Here is quick tip how you can use dataBind grails command outside of grails controller.
import org.codehaus.groovy.grails.web.metaclass.BindDynamicMethod

class DataBinder {

    private static BindDynamicMethod bindDynamicMethod = new BindDynamicMethod()

    /**
     * make the controller bindData method statically available, e.g. for service layer use
     * implemented as closure to allow static import emulating controller layer bindData usage 1:1
     */
    static Closure bindData = { Object[] args ->
        bindDynamicMethod.invoke(args ? args[0] : null, BindDynamicMethod.METHOD_SIGNATURE, args)
    }
}

//usage
class TestBind {
    public void intermediateNotification(Map params) {
        Test test = new Test()
        DataBinder.bindData(test, params)
        test.save()    
    }
}

Thursday, March 29, 2012

Testing controllers in Grails

In other to unit test controller in Grails you need to extend ControllerUnitTestCase. Also your test class needs to be in same package as the controller class unsed test and the name of your test class needs to be "Tests".

That's it. Now let's see code example.

Simple controller:
class SimpleController {

    def dateService

    def firstAction = {
        render "first"
    }

    def secondAction = {
        render "second"
    }

    def thirdAction = {
        render dateService.currentDateFormated()
    }
}

Service:
class DateService {

    boolean transactional = true

    public String currentDateFormated() {
        return (new Date()).format("MM-dd-yyyy")
    }
}

Unit test for controller.
class SimpleControllerTests extends ControllerUnitTestCase {
    
    protected void setUp() {
        super.setUp()
    }

    protected void tearDown() {
        super.tearDown()
    }

    public void testFirstAction() {
        controller.firstAction()
        assertEquals("first", controller.response.contentAsString)
    }

    public void testSecondAction() {
        controller.secondAction()
        assertEquals("second", controller.response.contentAsString)
    }

    public void testThird() {
        DateService dateService = new DateService()
        controller.dateService = dateService
        controller.thirdAction()
        assertEquals(dateService.currentDateFormated(), controller.response.contentAsString)
    }
}

If your controller references a service you have to explicitly initialise the service from your test like we did testThird() test in SimpleControllerTests tests.

Tuesday, September 27, 2011

Upload files with Grails

I found several examples of uploading files with Grails but neither one works for me. So here is snippet of code that is actually working (at lease for me). Maybe someone will find it useful.

Controller method look like this:
// inputTagName shoud be "myFile"
public MultipartFile readUploadFile(HttpServletRequest request, 
String inputTagName) {
  return request.getFile(inputTagName)  
}

Here is GSP page snippet: