combine #TestFor and #Integration Annotation grails 3 - grails

I migrate a large Grails 1.3.7 project to 3.1.6.
Tests for Controllers are integration tests. This works fine because the tests inherit from ControllerSpec. Now i´m should be able to do something like this:
#Integration
#TestFor(SampleController)
class SampleControllerIntSpec extends Specification {
Because the TestFor Annotation allows usage of model/view/.. fields like in Unit tests.
Is there a way to do something like this?
Thanks in advance.

No the TestFor annotations are exclusively for unit tests, Integration tests are full functional tests in Grails 3 where you should use a client like Geb to send requests to the server and assert responses.

Related

Integration tests vs Unit tests

I know this has been asked a lot of times, but this example one test confuses me. The test example here:
Testing Routes In ASP.NET MVC
Is this a unit or integration test? On the page it specifies it is a unit test but as I understand it, an integration test are tests that uses real dependencies. So is using the GlobalApplication.RegisterRoutes considered a dependency? So is this an integration test? I'm a bit confused to the extent of what a dependency is.
This is a unit test of a particular functionality of your application: the routes that you have defined.
So is using the GlobalApplication.RegisterRoutes considered a
dependency?
No, that's the subject under test - it's what you are testing. A dependency would be something that this subject depends on in order to work. It is this dependency that can be either mocked (in a unit test) or just using the actual object (in an integration test). For example if your routes were dependent on some database lookup operation then, if you don't mock this db call, you would be writing an integration test.

controllers integration test grails3: services are not getting injected

I am upgrading a grails app from 2.4.3 to 3.0.8.
There are a lot of integration tests which are using grails.util.GrailsWebUtil.bindMockWebRequest(grailsApplication.mainContext) following by controller.method call. But I discovered that grails.util.GrailsWebUtil doesn't contain bindMockWebRequest method anymore, seems like it has been replaced with grails.util.GrailsWebMockUtil, alright, but all services declared in controller are not getting injected into the class. I could use grails.test.spock.IntegrationSpec but this class has also been removed from grails3. There is a suggestion to use functional tests for integration test of controllers, but this solution doesn't work for me, I'm not itching to implement all these tests as functional, or inject dependencies manually into controller instances, how can I fix it?
Use grails.test.mixin.integration.Integration annotation instead of grails.test.spock.IntegrationSpec class extension.
And move the integration tests to src/integration-test/groovy.
I'm upgrading from Grails 2.5.1 to 3.2.4. One of my integration tests could not find the service I injected with def xxxService. I included the service in the #Mock list. XxxServiceIntegrationSpec extends Specification.
I know this does not quite relate to testing controllers, but maybe it helps?
We had dozones of controller integration in our old grails 2.x app. When we migrated to grails 3, we wanted to keep it, can not throw them and write functional tests from scratch.
Here I have blogged about it Integration testing controllers with Grails 3
The basic steps are
- setup mock request and response
- set current controller name
- rest mock request and response at the end of the test
See the above blog post for a complete example.
Hope it helps.
For anyone still looking for a solution to this (I had the same problem), I found this example really useful:
https://github.com/albertodeavila/testingGrails3

Where is IntegrationSpec in Grails 3

In middle of upgrading to Grails 3.0.1. All good except for integration testing which worked well in 2.4.4.
I've noticed grails.test.spock.IntegrationSpec is not there in org.grails:grails-test:3.0.1 any more.
Tried extending spock.lang.Specification and running via Gradle integrationTest task however it didn't seems to inject Spring resources. Also tried #Integration getting same error, additionally complained by GGTS:
General error during canonicalization: Provider "data" not installed java.nio.file.FileSystemNotFoundException: Provider "data" not installed at java.nio.file.Paths.get(Paths.java:147) at
org.grails.io.support.MainClassFinder.searchMainClass(MainClassFinder.groovy:37) at
org.grails.compiler.injection.test.IntegrationTestMixinTransformation.visit(IntegrationTestMixinTransformation.groovy:82) at
org.codehaus.groovy.transform.ASTTransformationVisitor.visitClass(ASTTransformationVisitor.java:150) at org.codehaus.groovy.transform.ASTTransformationVisitor
...
So I'm wondering if IntegrationSpec still exists in 3.0. How should I get it work?
Alright, figured out #Integration should be applied and to resolve the compilation error I had to specify #Integration(applicationClass = Application.class) because somehow the IDE couldn't find Application class. Of course the test case should extend Specification.
#Autowired to be used for objects that need to be injected into your test classes. Can't use #Autowired in conjunction with #Shared, which you can do in 2.x.

Why Grails unit, integration and functional test phases do not have their own environment?

Unit test don't need a database.
Integration and functional tests can have different fixtures and bootstrap data.
It would be also better to split functional tests on application itself and Selenium testing robot.
So, is there any reason to keep all tests phases in one environment?
I guess that's just a convention, since:
The setup of the unit tests configure a memory database to let you use GORM methods.
Your database will be initialized only when running integration tests.
Functional tests are treated as extension and depending on your project they're not mandated (for example: plugin projects that does not rely on UI).
Nothing stops you to define custom environments and run specific commands to them. You can also create Spring Beans and configure database access according to your env, using the Environment class.
if(Environment.current == Environment.DEVELOPMENT) {
...
}

Are ASP.NET MVC route tests considered Unit Tests or Integration Tests?

So i'm going to add some tests to my project to test my ASP.NET MVC routes. Are they a unit test or an integration test?
I feel like they are a unit test, where integration tests are against db's, 3rd party services (eg. twitter, file upload, etc) .. something I would normally mock out in a unit test.
The reason I'm asking is that I was looking at Ayende's Raccon Blog and noticed they have their route tests listed as Integration Tests.
I would consider them to be unit tests. Your just testing that an endpoint reaches a particular controller action. I think that's a pretty isolated set of functionality.

Resources