mjhugo / bookstore

sample app used to figure out grails testing plugin

This URL has Read+Write access

bookstore / test / unit / BookTests.groovy
100644 45 lines (34 sloc) 1.115 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
class BookTests extends grails.test.GrailsUnitTestCase {
 
    Book book
 
    void setUp() {
        super.setUp()
 
        // in testing-plugin 0.4 you can just do "mockForConstraintsTests(Book)" instead of these two lines
        registerMetaClass(Book)
        grails.test.MockUtils.prepareForConstraintsTests(Book)
 
        book = new Book(title:'Office Politics',
                publishedDate: new Date(),
                pages: 0,
                primaryAuthor: new Author())
        assertTrue book.validate()
    }
 
    void testTitleConstraint() {
        book.title = null
        assertTrue book.validate()
 
        book.title = ''
        assertFalse book.validate()
        assertEquals "blank", book.errors["title"]
    }
 
    void testPublishedDateConstraint() {
        book.title = null
        assertTrue book.validate()
    }
 
    void testPagesConstraint() {
        book.pages = -2
        assertFalse book.validate()
        assertEquals "min", book.errors["pages"]
    }
 
    void testPrimaryAuthorConstraint() {
        book.primaryAuthor = null
        assertTrue book.validate()
    }
 
}