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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T05:30:50+00:00 2026-06-08T05:30:50+00:00

My Integration-Test for my grails application is returning a null object when I try

  • 0

My Integration-Test for my grails application is returning a null object when I try to get a domain object using grails dynamic get method.
This is a simplified example of my problem. Lets say I have a controller TrackerLogController that uses a service TrackerLogService to save an updated Log domain for another Tracker domain.
Domain Tracker:

class Tracker {

   int id
   String name

   static hasMany = [logs: Log] 
}

Domain Log:

class Log {
   int id
   String comment

   static belongsTo = [tracker: Tracker]
}

Controller TrackerLogController save:

def TrackerLogService

def saveTrackerLog() {

   def trackerId = params.trackerId

   def trackerInstance = Tracker.get(trackerId)
   Log log = TrackerLogService.saveTrackerLogs(trackerInstance, params.comment)
   if( log.hasErrors() ){
      //render error page
   }
   //render good page
}

Service TrackerLogService save:

Log saveTrackerLogs( Tracker tracker, String comment) {
   Log log = new Log(tracker: tracker, comment: comment)
   log.save()
   return log
}

So now I want to write an Integration-Test for this service but I’m not sure if I should be writing one just for the simple logic in the controller (if error, error page else good page) I would think I would write a Unit test for that, and an Integration-Test to check the persistence in the Database.

This is what I have for my Integration-Test:

class TrackerLogServiceTests {

   def trackerLogService

   @Before
   void setUp(){

      def tracker = new Tracker(id: 123, name: "First")

      tracker.save()

      //Now even if I call Tracker.get(123) it will return a null value...
   }

   @Test
   void testTrackerLogService() {
      Tacker trackerInstance  = Tracker.get(123) //I have tried findById as well 

      String commit = "This is a commit" 

      //call the service
      Log log = trackerLogService.saveTrackerLogs(trackerInstance , commit)

      //want to make sure I added the log to the tracker Instance
      assertEquals log , trackerInstance.logs.findByCommit(commit)
   }
}

So for this example my trackerInstance would be a null object. I know the Grails magic doesn’t seem to work for Unit tests without Mocking, I thought for Intigration-Tests for persistence in the DB you would be able to use that grails magic.

  • 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-06-08T05:30:53+00:00Added an answer on June 8, 2026 at 5:30 am

    You can’t specify the id value unless you declare that it’s “assigned”. As it is now it’s using an auto-increment, so your 123 value isn’t used. It’s actually ignored by the map constructor for security reasons, so you’d need to do this:

    def tracker = new Tracker(name: "First")
    tracker.id = 123
    

    but then it would get overwritten by the auto-increment lookup. Use this approach instead:

    class TrackerLogServiceTests {
    
       def trackerLogService
    
       private trackerId
    
       @Before
       void setUp(){    
          def tracker = new Tracker(name: "First")
          tracker.save()
          trackerId = tracker.id
       }
    
       @Test
       void testTrackerLogService() {
          Tacker trackerInstance  = Tracker.get(trackerId)
    
          String commit = "This is a commit" 
    
          //call the service
          Log log = trackerLogService.saveTrackerLogs(trackerInstance , commit)
    
          //want to make sure I added the log to the tracker Instance
          assertEquals log , trackerInstance.logs.findByCommit(commit)
       }
    }
    

    Also, unrelated – don’t declare the id field unless it’s a nonstandard type, e.g. a String. Grails adds that for you, along with the version field. All you need is

    class Tracker {
       String name
       static hasMany = [logs: Log] 
    }
    

    and

    class Log {
       String comment
       static belongsTo = [tracker: Tracker]
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am running an Integration Test for a Grails application. I am using the
I'm writing an integration test for a rails application using Capybara within Rspec. After
I'm writing an integration test for a rails application using webrat. After filling out
I'm writing an integration test in Grails using GORM. I want to do something
I'm using Maven 2.2 with Grails 1.2.1. When trying to run the integration-test target,
I have created an integration test using the White automation framework. This is the
I'm trying to test my grails app using integration test that makes http requests.
I'm trying to run a very simple integration test and keep getting this error:
I am trying to write a simple integration test on a Rails 3.1 application
I'm trying to do some integration test in a csharp project, I'm using Entity

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.