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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T00:17:50+00:00 2026-05-30T00:17:50+00:00

I have an AuthenticationManager.authenticate(username,password) method that gets called in someMethod of a SomeService under

  • 0

I have an AuthenticationManager.authenticate(username,password) method that gets called in someMethod of a SomeService under test. The AuthenticationManager is injected into SomeService:

@Component
public class SomeService {
    @Inject
    private AuthenticationManager authenticationManager;

    public void someMethod() {
        authenticationManager.authenticate(username, password);
        // do more stuff that I want to test
    }
}

Now for the unit test I need the authenticate method to just pretend it worked correctly, in my case do nothing, so I can test if the method itself does the expected work (Authentication is tested elsewhere according to the unit testing principles, however authenticate needs to be called inside that method) So I am thinking, I need SomeService to use a mocked AuthenticationManager that will just return and do nothing else when authenticate() gets called by someMethod().

How do I do that with PowerMock (or EasyMock / Mockito, which are part of PowerMock)?

  • 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-30T00:17:51+00:00Added an answer on May 30, 2026 at 12:17 am

    With Mockito you could just do that with this piece of code (using JUnit) :

    @RunWith(MockitoJUnitRunner.class)
    class SomeServiceTest {
    
        @Mock AuthenitcationManager authenticationManager;
        @InjectMocks SomeService testedService;
    
        @Test public void the_expected_behavior() {
            // given
            // nothing, mock is already injected and won't do anything anyway
            // or maybe set the username
    
            // when
            testService.someMethod
    
            // then
            verify(authenticationManager).authenticate(eq("user"), anyString())
        }
    }
    

    And voila. If you want to have specific behavior, just use the stubbing syntax; see the documentation there.
    Also please note that I used BDD keywords, which is a neat way to work / design your test and code while practicing Test Driven Development.

    Hope that helps.

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

Sidebar

Related Questions

Have simple Spring Security webapp with password encoding: <security:authentication-manager alias=authenticationManager> <security:authentication-provider user-service-ref=personService> <security:password-encoder hash=md5
I have a working 3.0.2 applicationContext-security.xml that uses a custom authenticator <global-method-security pre-post-annotations=disabled> </global-method-security>
Have config (applicationContext-security.xml): <authentication-manager alias=authenticationManager> <authentication-provider> <password-encoder hash=sha/> <jdbc-user-service data-source-ref=dataSource/> </authentication-provider> </authentication-manager> from other
I have a class that uses a method from a bean. I'm trying to
I have an AuthenticationManager with multiple AuthenticationProviders. That works very well, the first AP
I have the following bean defined: <sec:authentication-manager alias=authenticationManager> <sec:authentication-provider user-service-ref=userDetailsService /> </sec:authentication-manager> I guess
I'm trying to make a simple username/password authentication in a Spring Security web app.
What I have is: <authentication-manager alias=authenticationManager> <authentication-provider user-service-ref=securityService/> </authentication-manager> As I understand, the default
Have a simple form that has a PictureBox in one location. I want to
I'm configuring and testing spring security 3.1.0. I have a user that has ROLE_COMPANY_OWNER.

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.