grails-core icon indicating copy to clipboard operation
grails-core copied to clipboard

Problem With Generated Tests In An App Using JUnit

Open jeffscottbrown opened this issue 8 months ago • 4 comments

Issue description

If I use start.grails.org to create a 7.0.0-SNAPSHOT application configured with JUnit, inside that app when I run a command like grails create-controller Demo, the command will generate a Spock specification (not a JUnit test) and when a command like ./gradlew check is run the test will not be run. The generated app does include testFramework: junit in grails-cli.yml. I believe the same thing happens with Grails 6.2.3.

jeffscottbrown avatar Apr 10 '25 12:04 jeffscottbrown

This is confirmed for 7.0.0-SNAPSHOT and is the same all the way back to 6.0.0.

create-controller does not check grails-cli.yml and uses a rocker template which always generates a spock test.

https://github.com/apache/grails-forge/blob/a68ec70d574707b5a06ddf276d272db11fadf22d/grails-cli/src/main/java/org/grails/forge/cli/command/CreateControllerCommand.java#L65

https://github.com/apache/grails-forge/blob/a68ec70d574707b5a06ddf276d272db11fadf22d/grails-cli/src/main/java/org/grails/forge/cli/command/templates/controllerSpec.rocker.raw#L1-L25

jamesfredley avatar Apr 10 '25 14:04 jamesfredley

Just FYI... I believe this is still in the case in 7.0.0-RC1.

jeffscottbrown avatar Aug 11 '25 14:08 jeffscottbrown

We also need to add this to the projects generated build.gradle if Junit is selected, otherwise the test won't be executed:

tasks.withType(Test).configureEach {
    useJUnitPlatform()
}

https://github.com/dauer/grails-core/blob/aa561174da33261c26d78cd546b1b5f8810414a6/grails-forge/grails-forge-core/src/main/java/org/grails/forge/feature/build/gradle/templates/buildGradle.rocker.raw#L119

Should we just skip generating tests if JUnit is selected og generate templates as with Spock?

dauer avatar Sep 16 '25 13:09 dauer

@dauer a test runner must be configured. It's reasonable that the check here: https://github.com/dauer/grails-core/blob/aa561174da33261c26d78cd546b1b5f8810414a6/grails-forge/grails-forge-core/src/main/java/org/grails/forge/feature/build/gradle/templates/buildGradle.rocker.raw#L117 be changed to be if either junit or spock is used.

jdaugherty avatar Sep 16 '25 13:09 jdaugherty