Created
August 27, 2011 22:29
-
-
Save sourcerebels/1175949 to your computer and use it in GitHub Desktop.
Grails, simple controller unit test example.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.sourcerebels | |
class HomeController { | |
def postService | |
def index = { | |
[postList: postService.list()] | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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