• Skip to main content
  • Skip to primary sidebar
  • Home
  • About
  • Subscribe BMA
  • Contact Us!

Be My Aficionado

Inspire Affection

You are here: Home / Programming / How To Mock SecurityContextHolder in Spring Security using Mockito?

How To Mock SecurityContextHolder in Spring Security using Mockito?1 min read

June 25, 2020 by Varun Shrivastava Leave a Comment

Test-driven development is a pretty awesome design process. And while building your code, you may want to mimic the logged-in user.

In Spring Security, the logged-in user principal is stored inside the Authentication object. This Authentication object can be accessed from any part of your application using SecurityContextHolder.

The SecurityContextHolder is a singleton class that holds the SecurityContext.

So, in the test case, we should never actually mock the SecurityContextHolder rather mock the SecurityContext and Authentication object that it holds.

Let’s see how you can perform the mocking. You can simply copy & paste the below method inside your test class and call it the test method or setup. Just provide the logged-in user that will be returned on calling authentication.getPrincipal() in your actual code.

private void mockAuthentication() {
    Authentication auth = mock(Authentication.class);

    when(auth.getPrincipal()).thenReturn(buildLoggedInUser());

    SecurityContext securityContext = mock(SecurityContext.class);
    when(securityContext.getAuthentication()).thenReturn(auth);
    SecurityContextHolder.setContext(securityContext);
}

Hope this helps. If you have any problem, just comment below. The comment is the fastest way to reach me.

Share this:

  • Click to share on Facebook (Opens in new window)
  • Click to share on LinkedIn (Opens in new window)
  • Click to share on Twitter (Opens in new window)
  • Click to share on Pinterest (Opens in new window)
  • Click to share on Reddit (Opens in new window)
  • More
  • Click to print (Opens in new window)
  • Click to share on Tumblr (Opens in new window)
  • Click to share on Pocket (Opens in new window)
  • Click to share on Telegram (Opens in new window)
  • Click to share on WhatsApp (Opens in new window)
  • Click to share on Skype (Opens in new window)
  • Click to email this to a friend (Opens in new window)

Filed Under: Programming Tagged With: mock, mockito, spring-security, tdd, test driven development

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Featured Posts

Quick and Tasty Summer snacks for you

April 7, 2017 By Priyanka Yadav Leave a Comment

Best VPS Cloud Hosting to Start your Blogging website

November 4, 2016 By Varun Shrivastava 1 Comment

Never Do These 10 Things Ever

July 4, 2020 By Varun Shrivastava Leave a Comment

10 Ways to Become Mentally Stronger

November 7, 2018 By Varun Shrivastava 1 Comment

10 Best Wireless Modems cum Wireless Router Right Now in India (under 2000 INR)

April 19, 2017 By Varun Shrivastava 32 Comments

Latest Posts

  • 3 Best Laptops (Mid-Range) For Every Use Case In India
  • Distributed System Architectural Patterns
  • The Power of being in the Present
  • Basic Calculator Leetcode Problem Using Object-Oriented Programming In Java
  • Study Abroad Destinations : Research and Review

Categories

  • Blogging (103)
  • Cooking (11)
  • Fashion (7)
  • Finance & Money (12)
  • Programming (51)
  • Reviews (4)
  • Technology (22)
  • Travelling (4)
  • Tutorials (12)
  • Web Hosting (8)
  • Wordpress N SEO (19)

Follow us on facebook

Follow us on facebook

Grab the Deal Now!

Hostgator Starting @$3.95/mo

DigitalOcean Free Credits

Trending

Affordable Hosting amazon aoc-2020 bad luck believe in yourself best database earn money blogging education experience fashion finance Financial Freedom food friends goals google india indian cuisine indian education system java javascript life life changing love make money microservices motivation oops poor education system principles of microservices problem-solving programmer programming reality search engines seo SSD Hosting success technology tips top 5 web web developer wordpress

Copyright © 2021 · BeMyAficionado by Varun Shrivastava · WordPress

loading Cancel
Post was not sent - check your email addresses!
Email check failed, please try again
Sorry, your blog cannot share posts by email.