Can't make unit test work with a simple example

Hi,
I am a newbie to GRAILS, I try to create a simple domain class and make the associated Unit Tests as describeded in the "Beginning Groovy and Grails" book.

The issue is that groovy code in the test class can't exceute. Here is the example for a Todo domain class:
...
void setUp() {
        Todo.list()*.delete()
    }
void testPersist() {
        new Todo(name: "1", createdDate: new Date(), priority: "", status: "").save()

... etc.

I can build well but the tests fail at execution time both inside IntelliJ and with the test-app command ...
here is what I get:


No signature of method: static collab.todo.Todo.list() is applicable for argument types: () values: []

groovy.lang.MissingMethodException: No signature of method: static collab.todo.Todo.list() is applicable for argument types: () values: []
at collab.todo.TodoTests.setUp(TodoTests.groovy:7)


It Happens that Domain related methods like list()*, save() etc.. aren't recognized.

If I create a test controler like:


def index = {
        Todo.list()*.delete()
        new Todo(name: "1", createdDate: new Date(), priority: "", status: "").save()
        render Todo.findByName("1").toString()
    }


this works fine ... the domain class behaves OK ...

I can see that target/classes are generated OK but target/test-classes doesn't exist.

inside the IDE I can see that in any project, the BuildConfig.groovy has errors. For instance:


grails.project.class.dir = "target/classes" //cannot resorve project
grails.project.test.class.dir = "target/test-classes" //cannot resorve test


auto completion work great but grails.project and grails.project.test cannot be resolved.

I am lost here: do I miss an import, do I miss an export ??

Thanks for your help, I can't get a basic grails project working.

My conf:
Win 7 64b + jdk 1.6.0.18 64b + Grails 1.2.2 + IntelliJIdea 901 build 93.94

Thanks

3 comments
Comment actions Permalink

Please try 9.0.2. This will at least fix the errors in BuildConfig.groovy.

Are you running the tests as JUnit or Grails tests? Please try the
latter. You can also try running 'grails test-app' from the command line
as a workaround to see if it helps.

0
Comment actions Permalink

Hi Peter, thanks for your answer.

I'll give a try to 9.0.2.



I have tried both  JUnit or Grails tests: same results.

It happens that i forgot to Mok the domain, here is the new code:

unfortunately I have an other issue:
the code is :

import grails.test.*

class TodoTests extends GrailsUnitTestCase {
void setUp() {
mockDomain(Todo)
Todo.list()*.delete()
}

void testPersist() {
mockDomain(Todo)
new Todo(name: "1", createdDate: new Date(), priority: "", status: "").save()
etc..

I now get

Cannot invoke method containsKey() on null object

java.lang.NullPointerException: Cannot invoke method containsKey() on null object
at java_util_Map$containsKey$0.call(Unknown Source)
at grails.test.GrailsUnitTestCase.registerMetaClass(GrailsUnitTestCase.groovy:87)
at grails.test.GrailsUnitTestCase.mockDomain(GrailsUnitTestCase.groovy:131)
at grails.test.GrailsUnitTestCase.mockDomain(GrailsUnitTestCase.groovy:130)
at collab.todo.TodoTests.setUp(TodoTests.groovy:7)

Isn't the mockDomain primitive enough to mock the domain ?

Thanks a lot for any help.

Nicolas.

0
Comment actions Permalink

It looks like your Todo class is null for some reason. You can check it
with a code like:

Class c = Todo
println c

Are you running the tests as JUnit or Grails tests? Please try the
latter. You can also try running 'grails test-app' from the command line
as a workaround to see if it helps.

0

Please sign in to leave a comment.