views:

81

answers:

3

Im developing with Grails. Since the framework will bootstrap data and a fully flushed out spring context, I find that I write a lot of integration tests for services. Let me rephrase that: I find I write no unit tests for services, only integration tests. Is this a bad idea? The only downside I see to it is that my tests take a bit longer to run.

I do use unit testing on controllers, as in a controller I am testing the various application flows, result types, redirect logic, etc. But the majority of tests I write are integration tests. This seems a break from tradition J2EE tests, where mostly unit tests are written.

edit- to be clear, Im not writing integration tests because the code is so complicated only integration tests will do. Im writing integration tests because it is easier to test everything together, since the framework gives you so much. I do mock certain things, like if a service collaborates with the acegi authenticationService, I mock that. I also mock anytime we interact with a webservice, because you have to in order to get a test that runs without special setup.

A: 

You may be able to use a mock framework to isolate different parts of your services for individual testing. Instead of relying on the massive Spring context, directly instantiate your service class and fill in the dependencies not directly relevant to the test with mocks. You may need a little refactoring to decouple your dependencies, but it will usually be for the best. This can lead to writing more and smaller tests instead of relying on a few large integration tests.

I am in a similar situation where many established tests are really integration tests, and it can be difficult, but not impossible, to change this going forward.

Matt
+1  A: 

In general, the important measure is the code coverage.

In can be advantageous to do integration tests if the global interface is more stable (less subject to change).

h3xStream
A: 

I clearly see a trend towards more functional tests and fewer unit tests, in particular for heavily-connected components low on logic. If I implement a complicated algorithm in a particular class, I will usually write unit tests for it, but if the complexity is due to integration with other components (which is very frequently the case), unit tests are not worth the hassle.

Gintautas Miliauskas