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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T02:32:42+00:00 2026-06-12T02:32:42+00:00

I have a class A which instantiates an object from class B internally. That

  • 0

I have a class A which instantiates an object from class B internally. That second class has lots of external side-effects, e.g. it uses a network connection.

public class A {
  private B b;
  public A() {
    b = new B(); // 3rd party class that calls out externally (e.g. to a db)
  }
}

I would like to unit test class A by providing a mock implementation of class B. I can easily create a mocked version of B by writing my own class or using something like Mockito and other frameworks, but how do I inject this mock implementation into class A’s code?

I guess I could ask for an instance of class B in class A’s constructor, but that seems ugly. No one really needs to know how class A does it’s business.

Python has a “mock” library that allows you to “patch” functions at run time during a test. Does Java have something similar?

  • 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-12T02:32:43+00:00Added an answer on June 12, 2026 at 2:32 am

    In this scenario I typically have a 2nd package (default) scope constuctor that allows you to pass a mock in for testing purposes.

    public class A {
      private B b;
    
      /*
      *  Used by clients
      */
      public A() {
        this(new B());
      }
    
      /*
      * Used by unit test
      * 
      * @param b A mock implementation of B
      */
      A(B b) {
        this.b = b;
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class that instantiates two classes which implement interfaces. I want one
I have a data object which has a base class with three derived classes,
The Problem: I have a forms project which instantiates a class that is defined
I have a initApp.as which instantiates a class which needs to access the currentState
I have class World which manages creation of object... After creation it calls afterCreation
Let's say that I have a class defined in moduleA.py which I want to
I have an class A which uses a heap memory allocation for one of
I have a search class that I am using to fetch results from two
I have class Item and class List (which has an NSMutableArray). Every time class
I have a class that instantiates a COM exe out of process. The class

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.