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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T22:14:23+00:00 2026-06-12T22:14:23+00:00

I have a problem trying to unit test a service in Grails (1.3.6). The

  • 0

I have a problem trying to unit test a service in Grails (1.3.6).

The service has an instance of another service which it uses to create an object. I am trying to mock the creator service and also the object that it returns.

In the test class:

mockMyService = new MyService() // service under test

// Fake Asset object to 'create'
asset = mockFor(Asset)
asset.createMock()

// Mock the CreatorService and return the mocked asset
mockCreatorService = mockFor(CreatorService)
mockCreatorService.demand.generateAsset(){Section s, Date d, User u, AssetStatus a, String name -> return asset}

mockMyService = new MyService()
mockMyService.assetService = mockCreatorService.createMock()

From debugging and inspecting I’m fairly sure that the mock object is created ok and the service is mocked correctly and faking the generateAsset call.

In MyService, the call to the other (creator) service is:

Asset asset = creatorService.generateAsset(section, date, author, status, assetName)

I’m getting this error:

Cannot cast object ‘grails.test.GrailsMock@56c88357’ with class ‘grails.test.GrailsMock’ to class ‘com.xxxxxx.Asset

Which I think is due to the assigning of the mocked Asset in the call to generateAsset by MyService.

Can anyone shed light on this? If the reason is due to the assignment, is there a way around it? Or am I approaching this wrongly or have I missed something obvious.

It’s grails 1.3.6 (the port to Grails 2 won’t be complete for a while so stuck with this version this for a while).

Thanks.

Update:

OK so I’ve got around the cast exception by declaring the mocked objects like:

def testAssets = [
 new Asset(id:123, name:......)]
mockDomain(Asset, testAssets)

But now it seems I have to declare all the non-null properties for the test Asset. This doesn’t seem right. For testing, I don’t care about the Asset at all, apart from the Id it has.

  • 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-12T22:14:25+00:00Added an answer on June 12, 2026 at 10:14 pm

    Your mocked generateAsset() method needs to return not asset (the mockFor(Asset)), but instead the value you got back from calling its createMock():

    // Fake Asset object to 'create'
    mockAsset = mockFor(Asset)
    def asset = mockAsset.createMock()
    
    // Mock the CreatorService and return the mocked asset
    mockCreatorService = mockFor(CreatorService)
    mockCreatorService.demand.generateAsset(){Section s, Date d, User u, AssetStatus a, String name -> return asset}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to unit test a web application using mstest I have that problem with
I have an odd problem. I have a unit test that keeps getting stuck
I'm trying to unit test some of my classes and having a problem where
I have a simple object I'm trying to serialize using DataContractSerializer. In my unit
I'm trying to learn dependency injection and have come across a problem, when unit
I am trying to mock database operations. I have problem in mocking SqlParameterCollection .
I am trying to write my first unit test for one of my service
I have a unit test which contains the following line of code Site.objects.get(name=UnitTest).delete() and
I am trying to create a unit test framework using CPPUnit for a large
How do I create unit test with context based on integration component? I have

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.