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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T18:23:58+00:00 2026-06-13T18:23:58+00:00

Recently came across with a weird scenario, that dependency injection for a service within

  • 0

Recently came across with a weird scenario, that dependency injection for a service within a service, threw NPE while running test-app

Reason for service-in-service injection is to make the GORM/criteriaBuilder as DRY as possible. So following are my two services.

Following class SearchService1.groovy is implemented to provide search capability.

class SearchService1{
def search = { ...//Calls to local closures to build a dynamic criteria }
...
}

Class SearchService2.groovy uses the search closure of SearchService1 class

class SearchService2{
     def searchService1
     ...
     def searchEntity(){
        searchService1.search()
      }
   }

Now, the above stated code works pretty well in run-app mode.
But for Integration test written for SearchService2 throws NPE as follows :

Cannot invoke method searchEntity() on null object
java.lang.NullPointerException: Cannot invoke method search() on null object
at com.myapp.service.SearchService2.searchEntity(SearchService2.groovy:326)
at com.myapp.service.SearchService2$searchEntity$0.callCurrent(Unknown Source)
at com.myapp.service.SearchService2.searchEntity(SearchService2.groovy:295)
at com.myapp.service.SearchService2$searchEntity.call(Unknown Source)
at com.myapp.integration.SearchService2Tests.testWhenSearch(SearchService2Tests.groovy:125)

Am I missing something very basic here ? Any thoughts are greatly appreciated. Many Thnx 🙂

Snippet from TestClass :

class SearchService2Tests extends GroovyTestCase{
 ...
 def searchService2
 ...
 void testWhenSearch(){
    def resultSet = searchService2.searchEntity() //This is the line throwing NPE
    ...
  }
}
  • 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-13T18:24:00+00:00Added an answer on June 13, 2026 at 6:24 pm

    Woah ! Got rid of this stupid error by this workaround.

    To TestClass, inject the searchService1 to searchService2 object like this :

    def searchService2
    def searchService2.searchService1 = new SearchService1()
    

    But come on ! Is this the right way to do it ? Can anyone explain the above error by the way, that why a Service-in-Service is not instantiated while running test-app.

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

Sidebar

Related Questions

I recently came across some weird looking class that had three constructors: class Class
I recently came across a Windows library called AHK that gives me great control
I recently came across a CSS pseudo class that can do an effect for
I recently came across a server that's sending a Location header (for a redirect)
I recently came across some Java code that simply put some strings into a
I recently came across classes that use a configuration object instead of the usual
I recently came across a bit of not-well-tested legacy code for writing data that's
I recently came across this blog post which basically says that we should not
I recently came across the Ruby EOB / -EOB construct within this context (from
I recently came across some code that looks something like this: <head> <?php /*

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.