How do you test maintenance or controller methods in grails 2.3

I am just starting out with grails 2.3 and I have problems running unit tests. What I have done so far, I launched

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

It creates

 | 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 for

 void "test something"() { assert false } 

and run again, I get again

 | 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 

Then I went through the spock documentation and tried to change my test again. This time

 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 the next step is to test the methods in my NewService class, so I changed my test again 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 Spock exmaples

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

What works like a charm ...

So finally my question is:

  • How to test my own service / controller methods (I get the 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.

+6
source share
2 answers

The solution was to go to the created service class and remove the @Transaction parameter. Then, for some reason, a service variable is created in the test.

I published a JIRA report about this.

Bypass

Just remove the annotation and add the line

 static transactional = true 

instead, to get the same behavior.

+5
source

As you understand, Grails 2.3 comes with Spock for tests instead of JUnit. I suggest you look at the basics . If you want class-level attributes, look at the Fields session.

There is also a sample repository that you can check out. Looking at the service example , you can see that your test already has a variable called a β€œservice” that Grails defines when using the @TestFor annotation.

 @TestFor(NewService) class NewServiceSpec { def "some description of what the test should do"() { expect: service.serviceMethod() ... } } 
+2
source

Source: https://habr.com/ru/post/954563/


All Articles