Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7696571
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T21:48:40+00:00 2026-05-31T21:48:40+00:00

The output of my test gives com.example.book.Book : null . When I debug the

  • 0

The output of my test gives com.example.book.Book : null. When I debug the test, b object is created with "MyBook" as its name. But since its has a static mapping belongsTo, the test fails. How do I make this work. When I comment the belongsTo mapping in the Books.groovy, the test passes. So how do I test Domain classes with mappings. Should I instantiate a Library object and add a Book object to it? But that doesn’t make testing the domain class in isolation as it is meant to be in a unit test, does it?

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T21:48:42+00:00Added an answer on May 31, 2026 at 9:48 pm

    Yes, the way you have this set up the Book cannot exist without a Library. You will have to create a Library and assign the book to it.

    Whether using belongsTo makes sense depends on your requirements. Do you really need to save the library and have all the books get saved as a result?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

$url = $_GET['url']; echo $url; and I request /test.php?url=ok gives me ok as output..
I'm writing unit tests using Test::More and Test::Output. I use Test::More to validate the
When executing echo 0 > test.txt in cmd.exe, the output in test.txt is 0_
This is an output of Boost.Test when test case failes: bjam toolset=msvc ...patience... ...found
I have these two queries to test my output SELECT DISTINCT( providerId ), SUM((
I expected to see the word test appear in the output once and the
when i run my flash file (test it) in the output panel i get
Is it possible to compare the result / output of one JUnit test to
I'm trying to disable log output from all external libraries in logback-test.xml. Somehow it
This question is related to the codebreaker example found in the RSpec book. See

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.