Skip to content

Instantly share code, notes, and snippets.

@sourcerebels
Created August 27, 2011 22:29
Show Gist options
  • Save sourcerebels/1175949 to your computer and use it in GitHub Desktop.
Save sourcerebels/1175949 to your computer and use it in GitHub Desktop.
Grails, simple controller unit test example.
package com.sourcerebels
class HomeController {
def postService
def index = {
[postList: postService.list()]
}
}
package com.sourcerebels
import static org.hamcrest.CoreMatchers.*
import static org.junit.Assert.assertThat
import static org.junit.matchers.JUnitMatchers.*
import grails.test.*
class HomeControllerTests extends ControllerUnitTestCase {
void test_should_retrieve_post_list() {
def postService = mockFor(PostService)
postService.demand.list(1) {->
return [new Post(title: "post1", content: "content1"),
new Post(title: "post2", content: "content2")]
}
controller.postService = postService.createMock()
def model = controller.index()
postService.verify()
assertNotNull model["postList"]
assertThat model["postList"].size(), is(2)
}
}
package com.sourcerebels
class PostService {
static transactional = true
def list() {
return Post.list()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment