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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T08:15:51+00:00 2026-06-11T08:15:51+00:00

I am writing unit tests for my application and I was wondering if it

  • 0

I am writing unit tests for my application and I was wondering if it is possible for the Mockito framework to affect an object that is passed into a method that returns void of a mocked class. For instance, calling a mocked validation class that contains a method that returns void but tracks various changes and metadata via an object passed in as an argument. .

public GetCartItemsOutput getCartItems(GetCartItemsInput getCartItemsInput) {
    CartItemsFilter cartItemsFilter = new CartItemsFilter();
    validator.validateCartItemsInput(getCartItemsInput, cartItemsFilter); ...

I mocked the validator class for my other tests but for this one I need mock the changes to the cartItemsFilter object which I do not know how to do.

  • 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-11T08:15:53+00:00Added an answer on June 11, 2026 at 8:15 am

    The answer is yes, you can, and there are basically two levels of doing this, based on the need of your test.

    If you merely want to test the interaction with the mocked object, you can simply use the verify() method, to verify that the void method was called.

    If your test genuinely needs the mocked object to modify parameters passed to it, you will need to implement an Answer:

    EDITED to show proper form of using Answer with void method

    doAnswer(new Answer() {
        @Override
        Object answer(InvocationOnMock invocation) {
            Object[] args = invocation.getArguments();
            ((MyClass)args[0]).myClassSetMyField(NEW_VALUE);
            return null; // void method, so return null
        }
    }).when(mock).someMethod();
    

    In Java 8+, the above is simplified with a lambda:

    doAnswer(invocation-> {
        Object[] args = invocation.getArguments();
        ((MyClass)args[0]).myClassSetMyField(NEW_VALUE);
        return null;
    }).when(mock).someMethod();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some unit tests that I'm writing for a WPF application, and as
We are writing unit tests for our ASP.NET application that run against a test
I am writing unit tests for a spring mvc 3.1 application that is not
I have heard that writing (coding) unit tests for your application effectively doubles the
I'm writing unit tests for a portion of an application that runs as an
I'm currently writing unit tests for an android application and stumbled into the following
I decided to start writing unit tests in our application. It uses Entity Framework
I am used to writing unit tests in Zend Framework 1.9 using PHPUnit_Framework_TestCase for
I'm writing tests for a Django application that uses an external data source .
I am writing unit tests for a multi-threading application, where I need to wait

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.