Saturday, 15 January 2011

Grails Unit Testing -


This is not a question, but maybe the request for comment about the general unit test in grails.

I am pushing my head against writing a unit test and leaving a lot, very easy to use cases, I always go into some difficulties, whatever I am getting, will be hiding at any time Is needed, such as grailsApplication, or any other framework object, starts with unit testing being different or you need to jump through so many hoops that it becomes counter-productive Later, on top of it, the move from 1.X to 2.x made all kinds of unit / integration test refactoring, making it easy for long periods of time, but still there was a failure during migration.

My answer ... Move all semi-complex tests into integration tests and do not look back. It works when everything rotates, it takes more time to run, but unit testing headache No more time than dealing with.

First, not the first, the case of use that tried to test the unit due to my disturbance, which creates a domain object, which is a dependency on grailsApplication.config, and said that The domain does something with the object that I tried the most I found (except what really works!), Nothing worked, I take the unit test code in the integration test, and the first part Pari in A. The unit test complained that 'config' can not be called empty objects or in that way, which means that there was no application for the Galli.

When integration works for testing then I do not really need to write unit test everything, all the time.

Use of Grails 2.2.0.

Agree for this

After the unit test TDD concept, There are building blocks written by AAP. The argument to be a unit test is to separate a module from the injection and instead of being dependent provided by the container, try to test with all dependencies provided by the test environment.

I can understand the pain point here. You will have to go through some boiler plate setup, but this is the main point in getting a unit test.

Integration test provides everything on your plate (Dependency injection on top) but it does not solve the purpose of testing. In different circumstances, the module I am in the same way is like you Grails 1.3.4 From Grails 2.2.0 to upgrade projects and are facing the same problem. But I took some time to understand the highly flexible fun mechanism provided by the latest version of Grails. for example;

@mock : You do not have to use the mockDomain , the annotation unit will take care of the fun of domain classes in test cases. This makes it more convenient for you to combat.

@TestFor : You can use # Gallery Artifacts (Controller, Services) for TRF, which you can use You will do some injection and you must not forget the power of

Mixes .

You can get more information about them.

Now to answer your problem you have to face while the unit was testing the service class. grailsApplication is easily available in case of unit testing, you do not need any code to get rid of duplicate or try to get it from the app context. Just use in the test category such as grailsApplication :

  // test without unit test some () {"Hello" = "grails" applying.config.foo. Bar.hello} //Config.groovy foo.bar.hello = Hello   

Is not it easy? Do not remember for deaf gauls epicling in the unit test class.

On the other hand, integration testing is a better way if you are specifically testing the service or a part of the service uses more than on the module.

We write pass pass test case to fail to pass the test makes me inconvenient and feel that I am doing something wrong. :)

No comments:

Post a Comment