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 7520589
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T02:05:48+00:00 2026-05-30T02:05:48+00:00

Using Grails 2.0.0 I am created service, that creates instances of Player class and

  • 0

Using Grails 2.0.0 I am created service, that creates instances of Player class and saves it at database. Then I’m wrote JUnit integration test that check that service method createNewPlayer(String platformID) throws exception if constraint validation failed at Player.save(failOnError : true).

All goes fine, but after method shouldFail(…) if I’m calls:

assert Player.list().size() == 1;

I am get this error:

--Output from testCreatingNewPlayerWithExistingID--
| Error 2012-02-15 21:52:05,293 [main] ERROR hibernate.AssertionFailure  - an assertion failure occured (this may indicate a bug in Hibernate, but is more likely due to unsafe use of the session)
Message: null id in test1.Player entry (don't flush the Session after an exception occurs)

My question is: how to test throwing exceptions within shouldFail method so that Hibernate is not kept in memory invalid instances of Player classes with null ids after this?

Below my code examples:

class WorkingService
{
    Player createNewPlayer(String platformID) throws ValidationException {
        Player player = new Player(platformID: platformID);
        return player.save(failOnError : true);
    }
}


class Player
{
    String platformID
    static constraints = {
        platformID nullable: false, blank: false, unique: true
    }
}


@TestFor(WorkingService)
class WorkingServiceTests
{
    WorkingService workingService;

    void testCreatingNewPlayerWithExistingID()
    {
        def player = new Player(platformID: "1");
        player.save(flush: true);

        assert Player.list().size() == 1;

        shouldFail ValidationException, {
            player = workingService.createNewPlayer("1");
        }

        assert Player.list().size() == 1;
    }
}
  • 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-30T02:05:49+00:00Added an answer on May 30, 2026 at 2:05 am

    I think the problem is that you’re mixing integration tests and unit tests. An integration test should extend GroovyTestCase and not use AST transform annotations like TestFor. This passes in my test app:

    import grails.validation.ValidationException
    
    class WorkingServiceTests extends GroovyTestCase {
    
       WorkingService workingService
    
       void testCreatingNewPlayerWithExistingID() {
    
          new Player(platformID: "1").save(flush: true)
    
          assert Player.count() == 1
    
          shouldFail(ValidationException) {
             workingService.createNewPlayer("1")
          }
    
          assert Player.count() == 1
       }
    }
    

    Unrelated – you shouldn’t use list().size() to get the count from the database since that loads every instance just to get the number; use count() instead. Also nullable: false is the default so it’s redundant to specify it.

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

Sidebar

Related Questions

I'm using Grails 1.3.7. I have a custom UserDetails object that gets created in
I have a Grails project that I created using Groovy/Grails Tool Suite version 3.0.0M3
I'm using: Grails xfire plugin and I created the service mobile containing the following
I have a Grails 2.0.0 project that was created using grails create-app . In
I want to create war using grails, but by default it creates war file
Using Grails database-migration I have a table in production defined like so: +--------------+------------+------+-----+---------+----------------+ |
When using Grails 1.1 together with a MySQL the charsets of the auto-generated database
I am using GGST 3.1 with a Grails 1.3.7 app. I created a unit
I am using Grails 2.1.0 and lesscss-resources 1.3.0.3 I have created a plugin containing
For learning purposes I created a toy project using grails. In this project I

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.