Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you test service or controller methods in grails 2.3

I'm just starting out with grails 2.3 and I have problems getting the unit tests to run. What I've done so far is I've run

grails create-app new-app
grails create-service NewService
grails test-app

This produces

| Running 1 unit test...
| Completed 0 unit test, 0 failed in 0m 2s
| Tests PASSED - view reports in C:\Git\aspera_web\target\test-reports

So far so good but if I edit the method

void "test something"() {
}

in the NewServiceSpec class to

void "test something"() {
    assert false
}

and run again I again get

| Running 1 unit test...
| Completed 0 unit test, 0 failed in 0m 2s
| Tests PASSED - view reports in C:\Git\aspera_web\target\test-reports

I then looked into the spock documentation and tried to edit my test again. This time to

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

which produces

| Running 1 unit test...
| Running 1 unit test... 1 of 1
| Failure:  test something(aspera_web.NewServiceSpec)
|  Condition not satisfied:
false
        at aspera_web.NewServiceSpec.test something(NewServiceSpec.groovy:19)
| Completed 1 unit test, 1 failed in 0m 2s
| Tests FAILED  - view reports in C:\Git\aspera_web\target\test-reports

which looks promising so then the next step is to test methods in my NewService class so I again change my test to

def service = new NewSevice()
void "test something"() {
    expect: service.serviceMethod()
}

and when I run it I get

| Running 1 unit test...
| Running 1 unit test... 1 of 1
| Failure:  test something(aspera_web.NewServiceSpec)
|  java.lang.NullPointerException
        at aspera_web.NewServiceSpec.test something(NewServiceSpec.groovy:21)
| Completed 1 unit test, 1 failed in 0m 2s
| Tests FAILED  - view reports in C:\Git\aspera_web\target\test-reports

just for good measure I also added a test directly from the Spock exmaples

def stack = new Stack()
def "size"() {
    expect: stack.size() == 0
}

Which works like a charm...

So at last my question:

  • How do I test my own service/controller methods (I get the exact same result if I replace create-service with create-controller)

EDIT

Apparently this is a bug in Grails 2.3.0 see my answer below.

like image 234
Spade Avatar asked Sep 25 '13 07:09

Spade


People also ask

How do you write a controller unit test?

Writing a Unit Test for REST Controller First, we need to create Abstract class file used to create web application context by using MockMvc and define the mapToJson() and mapFromJson() methods to convert the Java object into JSON string and convert the JSON string into Java object.

How do you test threads?

To test multi-thread functionality, let the multiple instances of the application or program to be tested be active at the same time. Run the multi-thread program on different hardware. Thread testing is a form of session testing for which sessions are formed of threads.

What to unit test?

Unit testing is a software development process in which the smallest testable parts of an application, called units, are individually and independently scrutinized for proper operation. This testing methodology is done during the development process by the software developers and sometimes QA staff.


1 Answers

The solution was to go into the generated service class and remove the @Transaction parameter. Then for some reason the service variable in the test gets instantiated.

I've filed a JIRA report about it.

Workaround

Just remove the annotation and add the line

static transactional = true

to the service instead to get the same behaviour.

like image 134
Spade Avatar answered Nov 29 '22 06:11

Spade