John Little
John Little

Reputation: 12371

GRAILS 2.4.4: how to run unit tests only, or how to fix the generated integration tests?

With grails 2.4, when you create a controller for example, it creates a unit test class such as:

@TestFor(SessionService)
class SessionServiceSpec extends Specification {

    def setup() {
    }

    def cleanup() {
    }

    void "test something"() {
    }
}

so you create 20 controllers, and get 20 test classes.

When you try to run grails test-app, it fails with the following error:

java.lang.Exception: 
No tests found matching grails test target pattern filter from org.junit.runner.Request

It never gets to run our integration tests.

So tests Grails creates for you do not work out of the box.

We could delete all the created test specs classes, but then we kind of want them there ready for if we want to write them.

  1. is there a way to run our integration tests, without running the unit tests (which are broken) or:
  2. is there a way to fix the test so they don't fall over? Someone posted add a valid when/then, but we don't know what to put in this, as what we have tried still generates the same exceptions.

It's a shame Grails doesn't tell you which tests or class throws this exception.

Upvotes: 2

Views: 1641

Answers (1)

dmahapatro
dmahapatro

Reputation: 50265

This looks like a weird argument to me. When you create a controller, unit spec is created for it by default based on a template which can be easily tailored according to your need.

grails install-templates

does it for you. Now to answer your questions:

  1. Yes there is a way to only run integration tests.

    grails test-app integration:

  2. After using install-templates modify the Controller.groovy template under src\templates\testing to something

like:

@artifact.package@import grails.test.mixin.TestFor
import spock.lang.Specification

/**
 * See the API for {@link grails.test.mixin.web.ControllerUnitTestMixin} 
 * for usage instructions
 */
@TestFor(@artifact.testclass@)
class @artifact.name@ extends Specification {

    def setup() {
    }

    def cleanup() {
    }

    void "test something"() {
        expect: 
        1 == 1
    }
}

This will not fail your test when run but it defeats the whole purpose of

writing failing test -> modify code -> fixing test approach

of TDD. I would rather implement a failing test and then write minimal logic in controller to pass the test.

Upvotes: 6

Related Questions