unit-test « Intelligrape Groovy & Grails Blogs

Posts Tagged ‘ unit-test ’

Grails productivity enhancer. The unsung hero ‘grails interactive mode’

Posted by Mohd Farid on November 30th, 2011

Of late, I have been thinking about the popularity of grails interactive mode amongst developers. I found that though most of us are aware of this mode but we don’t use it as often, as it should be.



Where should I use grails –interactive mode while developing in grails?
The answer may vary from person to person and the level of expertise. For me, I found it amazing while running my tests and creating artifacts.


I shall describe the advantages that I found with interactive mode while running Unit Tests. Let us take an example here:

I have a class called PersonUtil which needs to be unit tested. I write a PersonUtilTests class for testing its functionality. In order to run this particular unit test, I can use the following command:

grails test-app unit: PersonUtilTests

This takes approximately 20 secs on an average to run on my machine with a decent configuration.

Alternatively, We can run it in interactive mode. What we need to do is

 grails --interactive

This shall bring the grails infrastructure up and make it just ready to run commands. It takes around 5 seconds.

Welcome to Grails 1.3.7 - http://grails.org/
Licensed under Apache Standard License 2.0
Grails home is set to: /opt/grails

Base Directory: /home/farid/grailsApplications/survey-app/ErServices
--------------------------------------------------------
Interactive mode ready. Enter a Grails command or type "exit" to quit interactive mode (hit ENTER to run the last command):

Now, to test the app I can use the following command:

-------------------------------------------------------
Command TestApp completed in 2461ms
--------------------------------------------------------
Interactive mode ready. Enter a Grails command or type "exit" to quit interactive mode (hit ENTER to run the last command):
test-app unit: PersonUtilTests

It took 15 seconds to run this for first time.
For subsequent runs, it took 2-4 seconds. Which is a clear advantage of 16 seconds for every run. These 16 seconds are no less than gem when you are in your “flow-state”( in concentration mode).

While development, we make frequent changes to our code and therefore we need to run our unit tests multiple times. Every time I make a change and re run my unit test it takes 20 seconds through conventional grails test-app whereas it takes 3-4 seconds through interactive mode.

Earlier, I used to get frustrated while waiting for test cases to execute. This waiting time is one of the biggest factor that drives us from doing Test Driven Development.


Just in case you have not tried it. Please give it a shot. Believe me, it’s worth trying, you wont be disappointed!!!


Mohd Farid
farid@intelligrape.com

  • Share/Bookmark
Posted in Grails, Test, Unit Test

Tips for Agile Development with Unit Testing approach.

Posted by Salil on July 14th, 2010

This is more towards the best practices and my experiences for developing an application at a faster rate.

In my current project, we are using many different APIs (for external services). And usually it takes time to explore, implement and then stabilize the application. Also, it’s not over once you stabilize your APIs based code. Because there are possibilities to have unexpected results such as server side errors, broken request, broken response, etc. Also you never know when Service provider makes some changes and your code breaks.

Well, here are some tips which really helped me to achieve faster implementation and more stable application (esp. when working with third party APIs).

TRY TO MAKE YOUR CODE UNIT TESTABLE as much as possible.

1. Create Service layer for API based implementation. Keep this service layer independent from other application. So that you should run it without any other dependency.

2. Try to make small chunks of code (in form of methods) with least dependency on external things.
Avoid Domain/Database/Environment specific code. In other words, don’t put any kind of framework specific dependency. In some cases you can’t avoid. But try to avoid as much as possible. Because it could lead to integration testing and which again takes more time. Unit test is the fastest way to check your code stability.

3. In case you have some dependencies (and that can not be avoided) – Try to mock it up. In many cases it’s possible. If you are using Grails/Groovy kind of framework/language – you have an edge to take an advantage of meta-programming.

4. There is no specific way to do this. But it comes with practice. You can follow above tips to implement your Service Layer Code. And then create unit tests – invoke your API based service methods. And run it. Wow.. that’s it.

Advantages:
If there’s any kind of changes (from service provider), that can break your application’s functionality, test driven development will work as a “Life Saver”. It can tell you where’s the problem and why it happened. And you can make appropriate changes to stabilize it again.

Same thing implements when you develop a new feature. Unit Test Case helps you see the results in few seconds. Instead of waiting for 10-15 minutes (until application starts).

Isn’t it cool?

I really appreciate this approach. Hope others will also use it.

Thanks
Salil Kalia

  • Share/Bookmark
Posted in Grails, Groovy, Test, Unit Test

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