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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T19:57:44+00:00 2026-05-31T19:57:44+00:00

I have a controller like this : @Secured([‘ROLE_USER’,’IS_AUTHENTICATED_FULLY’]) def userprofile(){ def user = User.get(springSecurityService.principal.id)

  • 0

I have a controller like this :

@Secured(['ROLE_USER','IS_AUTHENTICATED_FULLY'])
    def userprofile(){
        def user = User.get(springSecurityService.principal.id)
        params.id = user.id
        redirect (action : "show", params:params)
    }

I want to test the controller above controller in spock, so I wrote a test code like this:

def 'userProfile test'() {

        setup:
        mockDomain(User,[new User(username:"amtoasd",password:"blahblah")])

        when:
        controller.userprofile()

        then:
        response.redirectUrl == "/user/show/1"
    }

When I run my test, this test fails with this error message :

java.lang.NullPointerException: Cannot get property 'principal' on null object
    at mnm.schedule.UserController.userprofile(UserController.groovy:33)

And in case of Integration test:

class UserSpec extends IntegrationSpec {

    def springSecurityService

    def 'userProfile test'() {

        setup:
        def userInstance = new User(username:"antoaravinth",password:"secrets").save()
        def userInstance2 = new User(username:"antoaravinthas",password:"secrets").save()
        def usercontroller = new UserController()
        usercontroller.springSecurityService = springSecurityService

        when:
        usercontroller.userprofile()

        then:
        response.redirectUrl == "/user/sho"
    } 

}

I get the same error as well.

What went wrong?

Thanks in advance.

  • 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-31T19:57:45+00:00Added an answer on May 31, 2026 at 7:57 pm

    You don’t appear to have done anything to provide a real or mock springSecurityService, so of course it’s null (there’s no dependency injection in unit tests; you have to mock everything that’s not provided by the unit test class). Adding this in setup: should work:

    controller.springSecurityService = [principal: [id: 42]]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a controller method that is a little something like this: def suggestions
I have a controller code like this : def test23() { //params.id23 is been
Lets say i have a Controller like this one : @RequestMapping(method=RequestMethod.GET, value=/error) public void
I have a Controller defined like this: package controllers import play.api._ import play.api.mvc._ import
I have a simple Controller that looks like this:- @Controller @RequestMapping(value = /groups) public
I have added navigation controller into my appication in didFinishLaunchingWithOptions like this LoginViewController *mainView
I have 2 functions/methods in the same controller in CodeIgniter like this: public function
In our code we used to have something like this: *(controller->bigstruct) = ( struct
I have an action like this: public class News : System.Web.Mvc.Controller { public ActionResult
I have this in my controller: Konkurrencer.where(title LIKE ?, %#{params[:q]}%).limit(4) I think this query

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.