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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T17:05:29+00:00 2026-06-14T17:05:29+00:00

I am mocking an object with Mockito, the same method on this object is

  • 0

I am mocking an object with Mockito, the same method on this object is called multiple times and I want to return the same value every time.
This is what I have:

LogEntry entry = null; // this is a field
// This method is called once only.
when(mockLogger.createNewLogEntry()).thenAnswer(new Answer<LogEntry>() {
  @Override
  public LogEntry answer(InvocationOnMock invocationOnMock) throws Throwable {
    entry = new LogEntry();
    return entry;
  }
});
// This method can be called multiple times, 
// If called after createNewLogEntry() - should return initialized entry.
// If called before createNewLogEntry() - should return null.
when(mockLogger.getLogEntry()).thenAnswer(new Answer<LogEntry>() {
  @Override
  public LogEntry answer(InvocationOnMock invocationOnMock) throws Throwable {
    return entry;
  }
});

The problem is, it seems that my getLogEntry method is called only once. For all subsequent invocations, null is returned instead and I get NPEs in tests.
How can I tell mockito to use stubbed version for all calls?

=================================================================
Post mortem for future generations

I did some additional investigation and as always it is not library’s fault, it is my fault. In my code one of the methods called getLogEntry() before calling createNewLogEntry(). NPE was absolutely legitimate, the test actually found a bug in my code, not me finding bug in Mockito.

  • 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-14T17:05:30+00:00Added an answer on June 14, 2026 at 5:05 pm

    Your stub should work as you want it. From Mockito doc:

    Once stubbed, the method will always return stubbed value regardless
    of how many times it is called.

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

Sidebar

Related Questions

I want a certain method of an object to always return a certain value
I have a test where I'm mocking 2 service calls that get called in
I have a utility method to find an object's getter method for a particular
Quite new to this mocking thing, I have a couple of questions. Correct me
I have the following simplified class I'm mocking: class myClass(object): @staticmethod def A(): #...
I am having problem mocking an object to test a descriptor. This is the
I am mocking in this sort of situation: class A { public IB B
I am mocking a repository that should have 1 entity in it for the
I'm trying to use the fluent mocking style of Rhino.Mocks and have the following
I'm new to mocking, and I'm having a hard time solving an issue with

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.