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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T21:43:38+00:00 2026-06-11T21:43:38+00:00

I have a fairly involved test case I am trying to add the following

  • 0

I have a fairly involved test case I am trying to add the following verify() to:

verify(userService).getUserById(anyLong()).setPasswordChangeRequired(eq(Boolean.TRUE));

This fails with this error:

org.mockito.exceptions.verification.TooManyActualInvocations: 
userService.getUserById(<any>);
Wanted 1 time:
-> at     test.controllers.AuthenticationControllerMockTest.testLookupsExceeded(AuthenticationControllerMockTest.java:404)
But was 4 times. Undesired invocation:

So I changed it to this:

verify(userService, atLeastOnce()).getUserById(anyLong()).setPasswordChangeRequired(eq(Boolean.TRUE));

And now it fails with:

java.lang.NullPointerException
    at test.controllers.AuthenticationControllerMockTest.testLookupsExceeded(AuthenticationControllerMockTest.java:404)

because this is returning null:

verify(userService, atLeastOnce()).getUserById(anyLong())

This seems puzzling – If I use the default (one invocation only), it fails because it’s being invoked multiple times, but if I tell it that multiple invocations are OK, it fails because it can’t find any invocations!

Can anyone help with this?

  • 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-11T21:43:40+00:00Added an answer on June 11, 2026 at 9:43 pm

    It looks like you both want to mock what happens when userService.getUserById() is called, and also verify that setPasswordChangeRequired(true) is called on that returned object.

    You can accomplish this with something like:

    UserService userService = mock(UserService.class);
    User user = mock(User.class);
    when(userService.getUserById(anyLong())).thenReturn(user);
    
    ...
    
    // invoke the method being tested
    
    ...
    
    verify(user).setPasswordChangeRequired(true);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a fairly involved managed data model which has a central object with
I have a fairly involved LispWorks Common Lisp module that sits atop some .NET
I have a fairly large C++ project, and am trying to use the debugger
I'm trying to pull back a fairly involved query from mysql - I've got
I have a few fairly involved work item queries saved in My Queries in
I have a fairly simple web2py form with a few validators like, IS_FLOAT_IN_RANGE(...) on
I have a fairly complex model for one of my pages; it's made of
I have a fairly basic data.table in R, with 250k rows and 90 columns.
I have a fairly simple question, however the answer seems to elude me. If
I have a fairly straightforward Java class here which creates 2 thread pools.... Connects

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.