Saving Domain object in controller test

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Saving Domain object in controller test

Joe Greenawalt
Hi,
I'm trying to save a domain object in a controller test because the controller action is a general search action.
However its failing with the following message:
Failure:  testSearch(com.phg.pgh.v1.WsV1ControllerTests)
|  groovy.lang.MissingMethodException: No signature of method: com.phg.pgh.my.LegacyUser.save() is applicable for argument types: () values: []
Possible solutions: save(), save(boolean), save(java.util.Map), wait(), any(), wait(long)

I assume i need to save an object to test the search on it, correct?  If not, then how do you test for something like that in grails?

If so, how do you correctly save a domain object in a controller test?

Thanks,
Joe

@TestFor(WsV1Controller)
class WsV1ControllerTests {

def populateValidParams(params) {
assert params != null
params["className"] = 'LegacyUser'
params["searchQuery"] = "firstName:=:joe"
}
def saveLegacyUser(){
def legacyUser = new LegacyUser(oldId:"1", firstName:"joe")
assert legacyUser.save() != null
}

void testSearch() {
saveLegacyUser()
controller.search()
println response.json.toString()
assert "joe" == response.json.firstName
}
}
Reply | Threaded
Open this post in threaded view
|

Re: Saving Domain object in controller test

sergiomichels
In Unit Tests you have to mock the domain classes that you will use. Try this:

@TestFor(WsV1Controller)
@Mock(LegacyUser)
class WsV1ControllerTests {
...
}




On Tue, Aug 28, 2012 at 10:53 PM, Joe Greenawalt <[hidden email]> wrote:
Hi,
I'm trying to save a domain object in a controller test because the controller action is a general search action.
However its failing with the following message:
Failure:  testSearch(com.phg.pgh.v1.WsV1ControllerTests)
|  groovy.lang.MissingMethodException: No signature of method: com.phg.pgh.my.LegacyUser.save() is applicable for argument types: () values: []
Possible solutions: save(), save(boolean), save(java.util.Map), wait(), any(), wait(long)

I assume i need to save an object to test the search on it, correct?  If not, then how do you test for something like that in grails?

If so, how do you correctly save a domain object in a controller test?

Thanks,
Joe

@TestFor(WsV1Controller)
class WsV1ControllerTests {

def populateValidParams(params) {
assert params != null
params["className"] = 'LegacyUser'
params["searchQuery"] = "firstName:=:joe"
}
def saveLegacyUser(){
def legacyUser = new LegacyUser(oldId:"1", firstName:"joe")
assert legacyUser.save() != null
}

void testSearch() {
saveLegacyUser()
controller.search()
println response.json.toString()
assert "joe" == response.json.firstName
}
}

Reply | Threaded
Open this post in threaded view
|

Re: Saving Domain object in controller test

Joe Greenawalt

Sorry about that I did some more reading and did mock it and it worked, but I cannot perform the search, I'm assuming because it's mocked and not really saved.  The executequery blows up with can't load type.  
I did some more reading and read that for saving and retrieval you should use integration tests. Which I'm going to move it to.

Thanks for the feedback,
Joe

On Aug 28, 2012 10:08 PM, "Sergio Michels" <[hidden email]> wrote:
In Unit Tests you have to mock the domain classes that you will use. Try this:

@TestFor(WsV1Controller)
@Mock(LegacyUser)
class WsV1ControllerTests {
...
}




On Tue, Aug 28, 2012 at 10:53 PM, Joe Greenawalt <[hidden email]> wrote:
Hi,
I'm trying to save a domain object in a controller test because the controller action is a general search action.
However its failing with the following message:
Failure:  testSearch(com.phg.pgh.v1.WsV1ControllerTests)
|  groovy.lang.MissingMethodException: No signature of method: com.phg.pgh.my.LegacyUser.save() is applicable for argument types: () values: []
Possible solutions: save(), save(boolean), save(java.util.Map), wait(), any(), wait(long)

I assume i need to save an object to test the search on it, correct?  If not, then how do you test for something like that in grails?

If so, how do you correctly save a domain object in a controller test?

Thanks,
Joe

@TestFor(WsV1Controller)
class WsV1ControllerTests {

def populateValidParams(params) {
assert params != null
params["className"] = 'LegacyUser'
params["searchQuery"] = "firstName:=:joe"
}
def saveLegacyUser(){
def legacyUser = new LegacyUser(oldId:"1", firstName:"joe")
assert legacyUser.save() != null
}

void testSearch() {
saveLegacyUser()
controller.search()
println response.json.toString()
assert "joe" == response.json.firstName
}
}

Reply | Threaded
Open this post in threaded view
|

Re: Saving Domain object in controller test

sergiomichels
Actually, I don't know in where version this feature was add, but in 2.0.4 I can save an retrieve objects in unit tests. Grails use an in memory database for it.

What's the error with your search? Can you post more details?


On Wed, Aug 29, 2012 at 2:21 AM, Joe Greenawalt <[hidden email]> wrote:

Sorry about that I did some more reading and did mock it and it worked, but I cannot perform the search, I'm assuming because it's mocked and not really saved.  The executequery blows up with can't load type.  
I did some more reading and read that for saving and retrieval you should use integration tests. Which I'm going to move it to.

Thanks for the feedback,
Joe

On Aug 28, 2012 10:08 PM, "Sergio Michels" <[hidden email]> wrote:
In Unit Tests you have to mock the domain classes that you will use. Try this:

@TestFor(WsV1Controller)
@Mock(LegacyUser)
class WsV1ControllerTests {
...
}




On Tue, Aug 28, 2012 at 10:53 PM, Joe Greenawalt <[hidden email]> wrote:
Hi,
I'm trying to save a domain object in a controller test because the controller action is a general search action.
However its failing with the following message:
Failure:  testSearch(com.phg.pgh.v1.WsV1ControllerTests)
|  groovy.lang.MissingMethodException: No signature of method: com.phg.pgh.my.LegacyUser.save() is applicable for argument types: () values: []
Possible solutions: save(), save(boolean), save(java.util.Map), wait(), any(), wait(long)

I assume i need to save an object to test the search on it, correct?  If not, then how do you test for something like that in grails?

If so, how do you correctly save a domain object in a controller test?

Thanks,
Joe

@TestFor(WsV1Controller)
class WsV1ControllerTests {

def populateValidParams(params) {
assert params != null
params["className"] = 'LegacyUser'
params["searchQuery"] = "firstName:=:joe"
}
def saveLegacyUser(){
def legacyUser = new LegacyUser(oldId:"1", firstName:"joe")
assert legacyUser.save() != null
}

void testSearch() {
saveLegacyUser()
controller.search()
println response.json.toString()
assert "joe" == response.json.firstName
}
}


Reply | Threaded
Open this post in threaded view
|

Re: Saving Domain object in controller test

craigatkinson
Sergio's right, dynamic finders ( .findBy...() ) are available in unit tests (and criteria queries in Grails 2.X), but HQL queries with .executeQuery() aren't yet supported in unit tests. For testing HQL queries you'll need an integration test.

On Wed, Aug 29, 2012 at 8:09 AM, Sergio Michels <[hidden email]> wrote:
Actually, I don't know in where version this feature was add, but in 2.0.4 I can save an retrieve objects in unit tests. Grails use an in memory database for it.

What's the error with your search? Can you post more details?



On Wed, Aug 29, 2012 at 2:21 AM, Joe Greenawalt <[hidden email]> wrote:

Sorry about that I did some more reading and did mock it and it worked, but I cannot perform the search, I'm assuming because it's mocked and not really saved.  The executequery blows up with can't load type.  
I did some more reading and read that for saving and retrieval you should use integration tests. Which I'm going to move it to.

Thanks for the feedback,
Joe

On Aug 28, 2012 10:08 PM, "Sergio Michels" <[hidden email]> wrote:
In Unit Tests you have to mock the domain classes that you will use. Try this:

@TestFor(WsV1Controller)
@Mock(LegacyUser)
class WsV1ControllerTests {
...
}




On Tue, Aug 28, 2012 at 10:53 PM, Joe Greenawalt <[hidden email]> wrote:
Hi,
I'm trying to save a domain object in a controller test because the controller action is a general search action.
However its failing with the following message:
Failure:  testSearch(com.phg.pgh.v1.WsV1ControllerTests)
|  groovy.lang.MissingMethodException: No signature of method: com.phg.pgh.my.LegacyUser.save() is applicable for argument types: () values: []
Possible solutions: save(), save(boolean), save(java.util.Map), wait(), any(), wait(long)

I assume i need to save an object to test the search on it, correct?  If not, then how do you test for something like that in grails?

If so, how do you correctly save a domain object in a controller test?

Thanks,
Joe

@TestFor(WsV1Controller)
class WsV1ControllerTests {

def populateValidParams(params) {
assert params != null
params["className"] = 'LegacyUser'
params["searchQuery"] = "firstName:=:joe"
}
def saveLegacyUser(){
def legacyUser = new LegacyUser(oldId:"1", firstName:"joe")
assert legacyUser.save() != null
}

void testSearch() {
saveLegacyUser()
controller.search()
println response.json.toString()
assert "joe" == response.json.firstName
}
}



Reply | Threaded
Open this post in threaded view
|

Re: Saving Domain object in controller test

Joe Greenawalt
Yep, and thats what I'm trying to do a HQL query.  Thanks for the all the help.

Joe

On Wed, Aug 29, 2012 at 12:47 PM, Craig Atkinson <[hidden email]> wrote:
Sergio's right, dynamic finders ( .findBy...() ) are available in unit tests (and criteria queries in Grails 2.X), but HQL queries with .executeQuery() aren't yet supported in unit tests. For testing HQL queries you'll need an integration test.


On Wed, Aug 29, 2012 at 8:09 AM, Sergio Michels <[hidden email]> wrote:
Actually, I don't know in where version this feature was add, but in 2.0.4 I can save an retrieve objects in unit tests. Grails use an in memory database for it.

What's the error with your search? Can you post more details?



On Wed, Aug 29, 2012 at 2:21 AM, Joe Greenawalt <[hidden email]> wrote:

Sorry about that I did some more reading and did mock it and it worked, but I cannot perform the search, I'm assuming because it's mocked and not really saved.  The executequery blows up with can't load type.  
I did some more reading and read that for saving and retrieval you should use integration tests. Which I'm going to move it to.

Thanks for the feedback,
Joe

On Aug 28, 2012 10:08 PM, "Sergio Michels" <[hidden email]> wrote:
In Unit Tests you have to mock the domain classes that you will use. Try this:

@TestFor(WsV1Controller)
@Mock(LegacyUser)
class WsV1ControllerTests {
...
}




On Tue, Aug 28, 2012 at 10:53 PM, Joe Greenawalt <[hidden email]> wrote:
Hi,
I'm trying to save a domain object in a controller test because the controller action is a general search action.
However its failing with the following message:
Failure:  testSearch(com.phg.pgh.v1.WsV1ControllerTests)
|  groovy.lang.MissingMethodException: No signature of method: com.phg.pgh.my.LegacyUser.save() is applicable for argument types: () values: []
Possible solutions: save(), save(boolean), save(java.util.Map), wait(), any(), wait(long)

I assume i need to save an object to test the search on it, correct?  If not, then how do you test for something like that in grails?

If so, how do you correctly save a domain object in a controller test?

Thanks,
Joe

@TestFor(WsV1Controller)
class WsV1ControllerTests {

def populateValidParams(params) {
assert params != null
params["className"] = 'LegacyUser'
params["searchQuery"] = "firstName:=:joe"
}
def saveLegacyUser(){
def legacyUser = new LegacyUser(oldId:"1", firstName:"joe")
assert legacyUser.save() != null
}

void testSearch() {
saveLegacyUser()
controller.search()
println response.json.toString()
assert "joe" == response.json.firstName
}
}