lunes, 25 de abril de 2011

bug.. mocking a domain class inherited gives problem in integration test

Well, i spend a lot of time searching for an issue..
This was happened to me:
I build a service, build an integration test case, and run it
When i made the test pass, i choice to run all my unit and integration test.

But this time, only that integration test failed.

Strange if i run all the integration test, Green light in junit
if i run all the unit and integration test, RED!

Wow, awesome, just was a save and find of an entity object, and that doesnt work.

After many debugging hours i find that the save method of my entity class in the integration test was calling a MockUtils.class. That is not good, i dont want a mock in mi integration test, i want a real implementation of the save method in mi inmemory database.

Well the problem was mocking a inherited domain class, i explained better here

I found a workaround, but i will wait for a fix.

miércoles, 13 de abril de 2011

Mocking the lock method

Just another issue with the grails unit test. I found, (like many others i guess jeje), that the static lock method is not mocked when i use mockDomain(Something).

There is a simple way to mock this:

// URLController.groovy
public class SomethingToTest extends GrailsUnitTestCase{
    public void test_lockMethod(){
        def strictControlUser = mockFor(User); 
        //And then simply do:
        strictControlUser.demand.static.lock(1..1){ Long param1 ->
        return User.get(param1); //In the unit test the get simply works 
    }
}

And done!
Now we are happy again

martes, 12 de abril de 2011

Mocking config parameters

A few days ago, I was writing my testing class for a controller, and i found that I didn't know how to mock a config parameter from Config.groovy.

Supose the following example:

You need to obtain the facebook url from the Config.groovy

environments {
    production {
        grails.facebookURL = "http://www.facebook.url"
    }
}


You have a controller groovy class:

// URLController.groovy
public class UrlController{
    public String getFacebookUrl(){
        return ConfigurationHolder.config.grails.facebookURL;
    }
}


And you want to build its respective unit test:

// URLControllerTests.groovy
public class UrlControllerTests extends GrailsUnitTestCase{
     def urlController;
     public void setUp(){
          super.setUp();
          urlController = new UrlController();
    }
     public void test_getFacebookURL(){
        //Mock configurationholder like this
        ConfigurationHolder.config = [ grails: [facebookURL: 'http://www.facebook.com'] ];
        assertEquals("http://www.facebook.com",urlController.getFacebookUrl());
    }
}


Voila!