Skip to content
update documentation authored by Stefan Borufka's avatar Stefan Borufka
......@@ -337,13 +337,19 @@ There might exist a better solution that does not require spy and that's the sol
The preferred way is to use the Spock framework for mocking and stubbing.
You should never mix it with the Grails Mocking Framework because this may lead to unintended side effects.
The same goes for ```MetaClass```. If you really have to use it, also use the ```@ConfineMetaClassChanges``` annotation.
The same goes for ```MetaClass```. If you really have to use it, also use the ``````TestCase.removeMetaClass``` in the cleanup section.
For the JUnit tests, Map coercion is currently the most effective approach to create fake objects for testing OTP components in unit tests. If they are too limited you should chose the most appropriate approach. These would be (in order of preference):
For the JUnit tests, Map coercion is currently the most effective approach to create fake objects for testing OTP components in unit tests.
If they are too limited you should choose the most appropriate approach. These would be (in order of preference):
1. Spock
1. Mock
1. GroovyMock
1. Spy
1. GroovySpy
1. Map Coercion. It's usually sufficient and the simplest method.
1. MetaClass (with ```@DirtiesRuntime``` annotation). Because of the side effects, it should be avoided.
1. Inheritance.
1. MetaClass (with manual cleanup via ```TestCase.removeMetaClass``` in the cleanup section. Because of the side effects, it should be avoided.
#### Avoid GroovyTestCase ####
......@@ -352,14 +358,12 @@ This will cause problems.
Test that do not run in the Grails context (that means tests for classes that live in ```src/```) should not extend it either as it will generate a JUnit 3 test case.
This does not allow the use of ```TemporaryFolder```, for example. It also has other drawbacks. If you find such a test, consider re-writing it in Spock.
#### Using thrown() or shouldFail ####
#### Using thrown() ####
In Spock, you should use ```thrown()``` if you expect an exception to be thrown.
The method also returns the exception for further inspection.
The ```shouldFail()``` method is injected in tests for Grails components automatically.
Code that lives outside of the Grails framework can extend ```GroovyTestCase```, as this is only a problem in Grails tests.
In Spock tests, ```thrown()``` should be used.
There exist also ```notThrown()``` to indicate, that a test shouldn't throw an exception, and you haven't anything to check.
#### Testing custom domain constraints ####
......@@ -368,12 +372,20 @@ The tests should not call ```save()```.
They should call ```validate()``` instead and check if the errors property is set and contains the expected values.
The procedure is described with examples in the [Grails manual](https://grails.github.io/grails-doc/latest/guide/testing.html#unitTestingDomains) (subsection "testing constraints").
To simplify the check, there exist two help methods:
- TestCase.assertValidateError: check, that exact the constraint defined failed. Other failed constraints are not valid.
- TestCase.assertAtLeastExpectedValidateError: check, that at least the provided constrain fail. More failed constraints are valid.
#### Temporary test files ####
Test files should be created using ```TemporaryFolder```.
It provides a unique base directory and takes care of clean-up. See the [JUnit documentation](https://github.com/junit-team/junit/wiki/Rules#temporaryfolder-rule) for more information.
```
@Rule
TemporaryFolder temporaryFolder
```
> **Note:** This does not work with JUnit 3 tests, that is tests extending GroovyTestCase or using a mixin. Consider re-writing those.
### Recommended Reading/Watching ###
......
......