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

  • Home
  • SEARCH
  • 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 8612001
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T04:31:10+00:00 2026-06-12T04:31:10+00:00

In my unit test I need to mock an interface which among different methods

  • 0

In my unit test I need to mock an interface which among different methods has nextItem() and isEmpty() methods:

public interface MyQueue {
    Item nextItem();
    boolean isEmpty();
    //other methods
    ...
}

My requirement for the mock is that isEmpty() initially should return false, but after nextItem() was called isEmpty() should return true. Thus I’m mocking a queue with one item.

  1. What is the simplest way to implement this kind of mock with mockito?
  2. Can I implement additional requirement: calling nextItem() second, third time and so on will result in a specific kind of exception?

P.S. I don’t want to provide the full implementation of my interface for the test, because of other methods in it, resulting in hard-to-understand and verbose code.

  • 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-12T04:31:11+00:00Added an answer on June 12, 2026 at 4:31 am

    You can achieve that with thenAnswer(), a feature Mockito documentation sees as controversial:

    Yet another controversial feature which was not included in Mockito originally. We recommend using simple stubbing with toReturn() or toThrow() only. Those two should be just enough to test/test-drive any clean & simple code.

    Here’s thenAnswer:

    private boolean called = false;
    
    when(mock.nextItem()).thenAnswer(new Answer() {
     Object answer(InvocationOnMock invocation) {   
         called = true;       
         return item;
     }
    when(mock.isEmpty()).thenAnswer(new Answer() {
     Object answer(InvocationOnMock invocation) {          
         return called;
     }
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following class structure that I need to unit test: public interface
I have a method I need to unit test, which as the subject implies,
I am working with Silverlight Unit Testing i need to test a function which
For a unit test, I need to mock several dependencies. One of the dependencies
I'm new to unit testing and PHPUnit. I need a mock, on which I
Has anyone been able to successfully unit test methods that are, by necessity, coupled
I am trying to unit test some code, and I need to to replace
I'm writing a unit test for a custom subclass of org.springframework.http.converter.xml.AbstractXmlHttpMessageConverter<T> and I need
Problem I have a UDPlistener application that I need to write a unit test
I need to know, in a context of a unit test, if Jetbrains IntelliJ

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.