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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T18:51:57+00:00 2026-06-07T18:51:57+00:00

Mockito seems like a pretty sweet stubbing/mocking framework for Java. The only problem is

  • 0

Mockito seems like a pretty sweet stubbing/mocking framework for Java. The only problem is I can’t find any concrete documentation on the best ways of using their API. Common methods used in tests include:

doXXX(???) : Stubber
when(T) : OngoingStubbing
then(T) : OngoingStubbing
verify(???) : T
given(T) : BDDOngoingStubbing
willXXX(???) : BDDStubber

When you see examples of Mockito in practice, you see code like:

when(yourMethod()).thenReturn(5);

From all the docs I’ve read, I’ve identified several "patterns" of Mockito "grammars" obtained from daisy-chaining these method calls together like the example above. Some common patterns I’ve found are:

When/Then: when(yourMethod()).thenReturn(5);

Given/Will: given(yourMethod()).willThrow(OutOfMemoryException.class);

Do/When: doReturn(7).when(yourMock.fizzBuzz());

Will/Given/Do: willReturn(any()).given(yourMethod()).doNothing();

Verify/Do: verify(yourMethod()).doThrow(SomeException.class);

What I’m choking on is how to select the right pattern/combination of method calls to model my test cases. It seems like you can daisy-chain these together in seemingly endless combos and I’m not sure what pattern is right for which problem.

Can some Mockito Guru help shed some light as to which patterns/combinations of Mockito methods are used for which types of test cases (and why)? Thanks in advance!

  • 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-07T18:51:58+00:00Added an answer on June 7, 2026 at 6:51 pm

    Mockito often have several ways of doing things.

    I find myself using mostly:

    // Setup expectations
    when(object.method()).thenReturn(value);
    when(object.method()).thenThrow(exception);
    doThrow(exception).when(object.voidMethod());
    
    
    // verify things
    verify(object, times(2)).method();
    verify(object, times(1)).voidMethod();
    

    I’ve found that i can do 95% of what i need to with these three kinds of calls.

    Also, what version of Mockito are you using? “given” and “will” constructs are not present in the latest version (1.9.0+)

    However, there are cases where I want the return value or exception to respond to the input. In this case, you can use the Answer interface to inspect the method arguments and return an appropriate value.

    public class ReturnFirstArg<T> implements Answer<T> {
        public T answer(InvocationOnMock invocation) {
            return invocation.getArguments()[0];
        }
    }
    
    when(object.method(7)).thenAnswer(new ReturnFirstArg<Integer>());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to do a test like: http://stevelibonati.wordpress.com/2012/02/05/spring-mvc-test-with-a-sprinkle-of-mockito/ My problem is that I
Consider a method signature like: public String myFunction(String abc); Can Mockito help return the
With Mockito I can do the following: verify(someService).process(any(Person.class)); But how do I write this
I am using mockito as mocking framework. I have a scenerio here, my when(abc.method()).thenReturn(value)
I'm looking for a mail server mock that can be configured like a Mockito
I am new to Mockito. Given the class below, how can I use Mockito
I'm currently studying the Mockito framework and I've created several test cases using Mockito.
Can I use Mockito to capture what was passed to the HttpServletResponse#sendError() method? I
I just started using mock objects (using Java's mockito) in my tests recently. Needless
I'm using the Mockito library for Java testing and getting errors in Mockito when

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.