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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:33:13+00:00 2026-05-27T18:33:13+00:00

I’ve been reading up on (and experimenting with) several Java mocking APIs such as

  • 0

I’ve been reading up on (and experimenting with) several Java mocking APIs such as Mockito, EasyMock, JMock and PowerMock. I like each of them for different reasons, but have ultimately decided on Mockito. Please note though, that this is not a question about which framework to use – the question really applies to any mocking framework, although the solution will look different as the APIs are (obviously) different.

Like many things, you read the tutorials, you follow the examples, and you tinker around with a few code samples in a sandbox project. But then, when it comes time to actually use the thing, you start to choke – and that’s where I am.

I really, really like the idea of mocking. And yes, I am aware of the complaints about mocking leading to “brittle” tests that are too heavily coupled with the classes under test. But until I come to such a realization myself, I really want to give mocking a chance to see if it can add some good value to my unit tests.

I’m now trying to actively use mocks in my unit tests. Mockito allows both stubbing and mocking. Let’s say we have a Car object that has a getMaxSpeed() method. In Mockito, we could stub it like so:

Car mockCar = mock(Car.class);
when(mockCar.getMaxSpeed()).thenReturn(100.0);

This “stubs” the Car object to always return 100.0 as the max speed of our car.

My problem is that, after writing a handful of unit tests already…all I’m doing is stubbing my collaborators! I’m not using a single mock method (verify, etc.) available to me!

I realize that I’m stuck in a “stubbing state of mind” and I’m finding it impossible to break. All this reading, and all this excitement building up to using mocks in my unit testing and… I can’t think of a single use case for behavior verification.

So I backed up and re-read Fowler’s article and other BDD-style literatures, and still I’m just “not getting” the value of behavior verification for test double collaborators.

I know that I’m missing something, I’m just not sure of what. Could someone give me a concrete example (or even a set of examples!) using, say, this Car class, and demonstrate when a behavior-verifying unit test is favorable to a state-verifying test?

Thanks in advance for any nudges in the right direction!

  • 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-27T18:33:14+00:00Added an answer on May 27, 2026 at 6:33 pm

    Well, if the object under test calls a collaborator with a computed value, and the test is supposed to test that the computation is correct, then verifying the mock colaborator is the right thing to do. Example:

    private ResultDisplayer resultDisplayer;
    
    public void add(int a, int b) {
        int sum = a + b; // trivial example, but the computation might be more complex
        displayer.display(sum);
    }
    

    Clearly, in this case, you’ll have to mock the displayer, and verify that its display method has been called, with the value 5 if 2 and 3 are the arguments of the add method.

    If all you do with your collaborators is call getters without arguments, or with arguments which are direct inputs of the tested method, then stubbing is probably sufficient, unless the code might get a value from two different collaborators and you want to verify that the appropriate collaborator has been called.

    Example:

    private Computer noTaxComputer;
    private Computer taxComputer;
    
    public BigDecimal computePrice(Client c, ShoppingCart cart) {
        if (client.isSubjectToTaxes()) {
            return taxComputer.compute(cart);
        }
        else {
            return noTaxComputer.compute(cart);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
Basically, what I'm trying to create is a page of div tags, each has
I've got a string that has curly quotes in it. I'd like to replace
I am reading a book about Javascript and jQuery and using one of the
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have thousands of HTML files to process using Groovy/Java and I need to

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.