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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T18:05:03+00:00 2026-06-09T18:05:03+00:00

I need to get the dependencies injected in my domain objects in my tests.

  • 0

I need to get the dependencies injected in my domain objects in my tests.

This tests are placed in the test/integration directory and extends from spock.lang.Specification.

How can I achieve this?

Note: I’ve seen this post How to inject spring beans into spock test, but it is not related with grails.

Edit:

The dependency I want to get injected is springSecurityService in my SecUser subclass called Player. The method that is failing is the encodePassword(), which is called in the beforeInsert().

I can mock this encodePassword() method in some tests, but when I want to test my controllers method save(), I can’t mock the Player that is being created because it all happens inside the controllers method.

After changing to extend IntegrationSpec, this is my test code:

package intertigre.test.domain
import intertigre.domain.Fecha;
import intertigre.test.util.DomainFactoryTestService
import grails.plugin.spock.IntegrationSpec
import grails.test.mixin.TestFor

    @TestFor(Fecha)
    class FechaSpec extends IntegrationSpec{

    DomainFactoryTestService domainFactoryTestService = new DomainFactoryTestService()

    def 'test'(){
        given:
            def fecha = new Fecha()
        when:
            fecha.save()
        then:
            Fecha.get(1) == fecha
    }

}

I’m getting this exception when running grails test-app :spock:

java.lang.NullPointerException: Cannot get property 'mainContext' on null object
    at grails.plugin.spock.IntegrationSpec.$spock_initializeSharedFields(IntegrationSpec.groovy)

And this one when I run the test alone:

| Failure:  intertigre.test.domain.FechaSpec
|  java.lang.NullPointerException: Cannot get property 'autowireCapableBeanFactory' on null object
    at grails.plugin.spock.IntegrationSpec.setupSpec(IntegrationSpec.groovy:47)
| Failure:  intertigre.test.domain.FechaSpec
|  java.lang.NullPointerException: Cannot invoke method isActive() on null object
    at grails.test.mixin.support.GrailsUnitTestMixin.shutdownApplicationContext(GrailsUnitTestMixin.groovy:232)
    at org.spockframework.util.ReflectionUtil.invokeMethod(ReflectionUtil.java:176)
    at org.spockframework.runtime.extension.builtin.JUnitFixtureMethodsExtension$FixtureType$FixtureMethodInterceptor.intercept(JUnitFixtureMethodsExtension.java:145)
    at org.spockframework.runtime.extension.MethodInvocation.proceed(MethodInvocation.java:84)
    at org.spockframework.util.ReflectionUtil.invokeMethod(ReflectionUtil.java:176)
  • 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-09T18:05:04+00:00Added an answer on June 9, 2026 at 6:05 pm

    Try declaring the springSecurityService into the test, as you would do in a controller. Grails is supposed to do all the job for you 🙂

    For an integration test you do something like this:

    package intertigre.test.domain
    import intertigre.domain.Fecha;
    import intertigre.test.util.DomainFactoryTestService
    import grails.plugin.spock.IntegrationSpec
    
    class DomainFactoryTestServiceSpec extends IntegrationSpec{
    
    def domainFactoryTestService // you dont need to create a new instance, it's injected by spring
    
    def 'test'(){
         given:
             // ...
         when:
             // ...
         then:
             // ....
     }
    

    If you need to test a specific domain object (as your Fecha class), you probably need a unit test, something like this:

    package intertigre.test.domain
    import intertigre.domain.Fecha
    import intertigre.test.util.DomainFactoryTestService
    import grails.test.mixin.TestFor
    import grails.test.mixin.Mock
    import spock.lang.Specification
    
    @TestFor(Fecha)
    @Mock([OtherObject1, OtherObject2])
    class FechaSpec extends Specification {
    
    def domainFactoryTestService // same thing here, if you need the service
    
    def 'test'() {
         given:
             def fecha = new Fecha()
         and:
             def oo1 = new OtherObject1()
         when:
             // ...
         then:
             // ....
     }
    

    You can use unit test to test services as well, it depends on what are you going to test (a class -the service- or a “situation” -the way the service is used-).

    Ps. Of course, this code here hasn’t been tested and can contain typos. 🙂 But I hope you get my point about how to test.

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

Sidebar

Related Questions

Why do I need the json jersey dependencies on client side for service.path(rest).path(object).path(id).accept(MediaType.APPLICATION_JSON).get(ObjectDTO.class); but
I have linq request. I need get item.Title in select. how do this? var
Tables bugs and dependencies looks like this: select * from bugs; +--------+--------+ | bug_id
I need to test a method belonging to a service class. This service class
This is a simple question. I need to get a complete list of the
I need help fixing this error I get when trying to deploy my web
I need get the id of an images which is place inside a datatemplate..
Just need get some vals located in application.ini(main ini) in the Controller plugin I
I need get all items these have no categories int? categoryId = null; var
I have two tables with a weak relation. I need get a text value

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.