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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T23:46:20+00:00 2026-05-22T23:46:20+00:00

I have to unit test some old code that wasn’t designed to support unit

  • 0

I have to unit test some old code that wasn’t designed to support unit testing (No DI). Is there a way to mock an object that is being initialized within a public method?

public int method() {

    A a = new A(ar1, arg2); //How to mock this?

}

Thanks,

-Abidi

  • 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-22T23:46:21+00:00Added an answer on May 22, 2026 at 11:46 pm

    Another option is to refactor the code into

    public int method() {
       A a = createA(arg1,arg2);
    }
    
    A createA(int arg1, int arg2) {
        return new A(arg1,arg2);
    }
    

    In your test method now you can use Mockito’s spy and doAnswer functions to override createA on your test fixture with something along the lines of:

    Foo foo = new Foo();
    Foo spiedFoo = spy(foo); // a spied version when you can copy the behaviour
    doAnswer(new Answer() {
        @Override
        public Object answer(InvocationOnMock inv) throws Throwable {
            A a = mock(A.class);
            return a;
        }
    }).when(mySpy).createA(anyInt(), anyInt());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a unit test which depends on some code that uses MEF. When
I am writing some unit test and I have a unit testing base class
I have some written a number of unit tests that test a wrapper around
I have a unit test that creates a mock calls my method to be
I have a situation where I need to unit test some scenarios that require
I'm attempting to unit test some code using NUnit. I have a method: public
I have different projects written in .NET 3.5 and some unit test projects to
I have some kind of test data and want to create a unit test
I'm using Google Test Framework to set some unit tests. I have got three
I have a unit test which contains the following line of code Site.objects.get(name=UnitTest).delete() and

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.