grails testing « Intelligrape Groovy & Grails Blogs

Posts Tagged ‘ grails testing ’

My Top 9 Features from Grails 2.0

Posted by Vivek Krishna on January 18th, 2012

The groovy world is abuzz about the latest release from the Grails Stable, Grails 2.0, which packs a lot more punch than its predecessors, which by themselves were productivity enhancers and wonderful to develop our web applications with. The new version brings with itself a lot of changes compared to the previous releases and deserves to be truly called 2.0 instead of 1.4 or 1.5.

I would like to walk you through some of the coolest features from grails 2.0 that I have discovered in the limited time I have spent with the new version and fell in love with.

  1. The Improved Interactive Mode: I love working on the command line and the new interactive mode is a joy to work with. The auto-completions, combined with the quick response times(as a result of not having to load JVM for each command) is a sure shot productivity booster. That I can run normal commands from the grails shell by just prefixing them with a “!” is an added advantage.
  2. Dynamic Domain Class Reloading: One activity that consumed a lot of time with earlier versions of Grails was waiting for the servlet container to restart, once we made even a line’s change in the domain class or src/groovy file. Now, the reloading is dynamic which means that the changes take effect very quickly, this letting us focus on the development without the flow being interrupted by the restart process.
  3. HTML5 scaffolded screens: One of the complaints I had with earlier versions of grails was the not-so-good looking screens that were generated by scaffolding. For most of the admin screens, the scaffolded screens are sufficient and the new HTML5 compatible scaffolded screens are very easy on the eye and provide an excellent user experience. With the new screens however, I can simply change the logo and they make for sufficiently pleasing CRUD screens.
  4. Business Class Citizenship for Testing: Testing has been a first class citizen of the framework right from day 1, but the new testing framework has made life easy for people like me, for whom testing doesn’t come naturally. With scaffolded tests being generated, newbies will find it easier to learn aspects of testing the application. In addition to that, the capability to unit-test criteria queries adds an element which was missing from earlier versions.
  5. Link Generation/Page Renderer APIs: Earlier, generating html from gsp files from non-request bound threads was a pain, especially with mocking the web request. Now, with the PageRenderer API, generating a view in a job or a service is as simple as injecting a groovyPageRenderer bean and calling groovyPageRenderer.render() method just like from within a controller.
  6. GORM Finders with Groovy Collection find/findAll like syntax: If there was one feature I could keep from all the enhancements in 2.0, it would be this. This is also the feature which, I think would go a long way in making GORM queries easier. There couldn’t be a more expressive syntax!
  7. Public methods in controllers as actions which can take arguments: Earlier, it used to be a pain to type-cast the parameters into their respective types(when one felt that command objects are an overhead while working with at most 2-3 params). The new method like syntax lets us define actions as public methods in controllers, which automatically bind data to the method arguments, based on the params. If I have a command object as argument, it works just the way it worked earlier. We talk about thin controllers a lot. This also means that we’ll be more diligent while creating methods in controllers and even if we really must, they have to be private. This just made the controllers thinner!
  8. The New Improved Test Reports: The new test reports are very easy on the eye and even more easier while finding test failures. Though we don’t recommend having println statements in our test cases, the fact that I can see the system outputs on the same page as my test case is a winner.
  9. DB Console: Viewing the contents of the in-memory DB was a pain in the earlier versions. The new dbconsole, which can be accessed only in development mode is a clear winner.

Grails 2.0 comes with its set of wonderful features which makes development with it, a much better experience.

  • Share/Bookmark
Posted in Grails

Unit-Tests : Getting started with Service Unit Test

Posted by Imran Mir on June 14th, 2010

Hi all,

Here I am giving a brief introduction about unit testing of services in grails. I will explain it with the help of a simple example.

We have a domain class named: Item
Two services : UtilService and ItemService

Code of UtilService.groovy

// class UtilService code
 
class UtilService {
    boolean transactional = true
    Integer calculatePrice(Item item, Integer vat, Integer profit) {
        return item.price * vat * profit
    }
}

Code of ItemService.groovy

// ItemService class code
 
class ItemService {
    boolean transactional = true
    def utilService
    Integer calculateCost(Item item,Integer quantity) {
        Integer price = utilService.calculatePrice(item,5,8)
        Integer totalCost = price * quantity
        return totalCost
    }
}

Our test case:-

  void test_calculateCost() {
 // Step-1: Mocking the domain class
        def instance = [new Item()]
        mockDomain(Item, instance)
 
//Step-2: Mocking the service and its method used in the function that has to be tested
        def otherService = mockFor(UtilService)
        otherService.demand.calculatePrice(1) {l,m, n -> return 150}
 
// Step-3: creating the instance of our service
        def itemService = new ItemService()
        itemService.utilService = otherService.createMock()
 
// Step-4: Calling the method to be tested
        def amount = itemService.calculateCost(instance[0],10)
 
        assertEquals 1500, amount
    }

Hope it helps.
For more…. wait for the next blog on unit testing

Regards
Imran
imran@intelligrape.com

  • Share/Bookmark
Posted in Grails, Groovy