8

I am just starting to learn Grails testing and I tried to write my first grails test.For this, I created a fresh grails project and created a controller named com.rahulserver.SomeController:

package com.rahulserver

class SomeController {

    def index() { }
    def someAction(){

    }
}

When I created this controller, grails automatically created a com.rahulserver.SomeControllerSpec under test/unit folder. Here is my SomeControllerSpec.groovy:

package com.rahulserver

import grails.test.mixin.TestFor
import spock.lang.Specification

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

    def setup() {
    }

    def cleanup() {
    }

    void testSomeAction() {
        assert 1==1
    }
}

When I right click this class, and run this test, I get following:

Testing started at 5:21 PM ...
|Loading Grails 2.4.3
|Configuring classpath
.
|Environment set to test
....................................
|Running without daemon...
..........................................
|Compiling 1 source files
.
|Running 1 unit test...|Running 1 unit test... 1 of 1
--Output from initializationError--
Failure:  |
initializationError(org.junit.runner.manipulation.Filter)
 |
java.lang.Exception: No tests found matching grails test target pattern filter from org.junit.runner.Request$1@1f0f9da5
    at org.junit.internal.requests.FilterRequest.getRunner(FilterRequest.java:35)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:138)
No tests found matching grails test target pattern filter from org.junit.runner.Request$1@1f0f9da5
java.lang.Exception: No tests found matching grails test target pattern filter from org.junit.runner.Request$1@1f0f9da5
    at org.junit.internal.requests.FilterRequest.getRunner(FilterRequest.java:35)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:138)

|Completed 1 unit test, 1 failed in 0m 0s
.Tests FAILED 
|
 - view reports in D:\115Labs\grailsunittestdemo\target\test-reports
Error |
Forked Grails VM exited with error

Process finished with exit code 1

So why is it failing?

EDIT

I am using grails 2.4.3

tim_yates
  • 167,322
  • 27
  • 342
  • 338
rahulserver
  • 10,411
  • 24
  • 90
  • 164

1 Answers1

18

The unit tests are defined with Spock by default:

void testSomeAction() {
    assert 1==1
}

Should be written as:

void "Test some action"() {
    expect:
      1==1
}

See http://spockframework.github.io/spock/docs/1.0/index.html

Graeme Rocher
  • 7,985
  • 2
  • 26
  • 37
  • 2
    @rahulserver you can still use assert but name your method like the way the answer above shows. But doing it the Spock way makes your tests more readable and is recommended. – Stealth Jul 17 '15 at 12:50
  • Just read in documentation https://code.google.com/p/spock/wiki/SpockBasics that "A feature method must have at least one explicit (i.e. labelled) block " – rahulserver Jul 19 '15 at 08:24