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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T15:49:46+00:00 2026-06-09T15:49:46+00:00

Okay, so this might be a dangerous question to ask. I have been doing

  • 0

Okay, so this might be a dangerous question to ask. I have been doing unit testing for a while, but for some reason, I woke up this morning and I asked myself this question.

Say I have an interface UserFactory, and it has the method CreateUser.

At some point I need to create a user right? So I create a test checking whether CreateUser was called for the UserFactory at the appropriate place.

Now, unit tests are pretty coupled to the actual code – which is fine. But maybe a bit too much? As in, the only way to break the test is by not calling the call to CreateUser. We are not checking its implementation etc. but just checking the interface has been called. But whoever does remove that call, would have a failing test and ultimately remove the verify statement from the step to verify the CreateUser was called.

I have seen this happen over and over again.

Could someone bring the light back to me and explain why is it beneficial to verify mocked objects’ methods have been called? I can see why it may be useful to set them up, say CreateUser should return a dummy user for later part of the code, but in places where we are simply and only verify if they have been called is the part that got me.

Thanks!

  • 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-09T15:49:47+00:00Added an answer on June 9, 2026 at 3:49 pm

    Verifying a mocked object is often a necessary evil and, as you mentioned, unit tests sometimes are really tightly coupled to the class under test.

    I don’t know how to give a good answer to your question, but I’ll try.

    Take this bit of code as an example, where userRepository is a dependency (the example is not exactly great).

    public void doSomething(User user) {
        if( user.isValid() ) {
            userRepository.save(user)
        } else {
            user.invalidate();
        }
    }
    

    One way to test this, would be to plug the real repository, which connects to the DB, and verify that the user is persisted. But since I’m unit testing, I can’t have an external dependency in my test.

    Now, which other options do you have to verify the scenario where the user is valid? Since userRepository.save() returns void, the only way is to verify the side effect is to verify that the mock was called.
    If I wouldn’t verify the mock, then the unit test wouldn’t be very good, as I could delete the line where I save the object, and the test would still pass.

    This is not the case with some mocks that return a value, and then than value is used in the method. This usually means that if the mock returns a null, then the application throws a NullPointerException (in the case of java).

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

Sidebar

Related Questions

Okay, so this might be kind of strange but here's the question: I have
okay, this might be a silly question. So I have some tuples of size
This might be a n00bish question, but whatever. Is okay to use exceptions for
Okay this may be a simple question but I have yet to come with
Okay, I might confuse some people who might be able to answer this question,
Okay, this might be a very silly beginner question, but: I've got an ClassA,
okay this might be a pretty lame and basic question but its stuck in
Okay this is probably a rookie question, but I have never done GUI programming
Okay, this might seem like a weird question, but bear with me. So I
Okay, this question comes through a friend so it might be lost in translation...

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.