0

If I generate a contoller using the command "create-controller" Grails generates the contoller and a unit test (using the template /src/templates/scaffolding/Test.groovy).

After changing bits of the template I now want to generate the unit tests anew but Grails won't generate them. I tried:

  • "create-unit-test" but this will use the template /src/templates/testing/UnitTest.groovy and "create-integration-test" will use the template /src/templates/testing/Generic.groovy

So how can I generate unit test for controller using the Test.groovy template?

Jörg Rech
  • 1,339
  • 2
  • 13
  • 25

1 Answers1

1

When creating artifacts or using scaffolding, Grails uses templates to create the domain classes, controllers, views, etc. The default templates are part of the Grails distribution for out of the box behaviour, but can be customized for project specific needs.

Copies the the templates used by Grails during code generation to your project directory:

$ grails install-templates

src/templates/
├── artifacts
│   ├── Tests.groovy
│   ├── ...
├── scaffolding
│   ├── Test.groovy
│   ├── ...
└── war
    └── web.xml

The artifacts directory contains the templates used by the create-* commands.
The scaffolding directory contains templates used by the generate-* commands.
The war directory contains the web.xml template used to generate the deployment descriptor.

So you need to edit:

  • src/templates/artifacts/Tests.groovy file, when execute grails create-unit-test or grails create-integration-test commands.

  • src/templates/scaffolding/Test.groovy when execute grails generate-all or grails generate-controller commands.

In your case regenerate your controller test and overwrite only your test file:

$ grails generate-controller com.arturoherrero.Post
| Generating controller for domain class com.arturoherrero.Post
> File /grails-app/controllers/com/arturoherrero/PostController.groovy already exists. Overwrite?[y,n,a] n
> File /test/unit/com/arturoherrero/PostControllerTests.groovy already exists. Overwrite?[y,n,a] y
| Finished generation for domain class com.arturoherrero.Post
Arturo Herrero
  • 12,772
  • 11
  • 42
  • 73