The result of my test gives com.example.book.Book : null . When I debug the test, b "MyBook" object is created as name . But since it has a belongsTo static mapping, the test fails. How to do it? When I comment on the belongsTo display in Books.groovy, the test passes. So, how do I check domain classes with mappings. Should I create an instance of the Library object and add a Book object to it? But this does not test the domain class in isolation, as it is intended for unit test, right?
Below is my code.
Domains:
//Book.groovy package com.example.book class Book { static constraint = { name blank: false, size: 2..255, unique: true } static belongsTo = [lib: Library] String name } //Library.groovy package com.example.library class Library { static hasMany = [book: Book, branch: user: User] static constraints = { name blank: false place blank: false } String name String place }
Unit tests:
//BookUnitTests.groovy package com.example.book import grails.test.* class BookUnitTests extends GrailsUnitTestCase { protected void setUp() { super.setUp() mockForConstraintsTests(Book) } protected void tearDown() { super.tearDown() } void testPass() { def b = new Book(name: "MyBook") assert b.validate() } }
Test Output:
Failure: testPass(com.example.book.BookUnitTests) | Assertion failed: assert b.validate() | | | false com.example.book.Book : null at com.example.book.BookUnitTests.testPass(BookUnitTests.groovy:17)
Thanks.
kadaj source share