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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:39:55+00:00 2026-05-27T16:39:55+00:00

So I have been asked to read up on mocking and BDD for our

  • 0

So I have been asked to read up on mocking and BDD for our development team and play around with mocks so as to improve a handful of our existing unit tests (as an experiment).

I have ultimately chosen to go with Mockito for a number of reasons (some outside the scope of my control), but namely because it supports both stubbing and mocking for instances when mocking would not be appropriate.

I have spent all day learning about Mockito, mocking (in general) and BDD. And now I am ready to dig in and start augmenting our unit tests.

So we have a class called WebAdaptor that has a run() method:

public class WebAdaptor {

    private Subscriber subscriber;

    public void run() {

        subscriber = new Subscriber();
        subscriber.init();
    }
}

Please note: I do not have a way to modify this code (for reasons outside the scope of this question!). Thus I do not have the ability to add a setter method for Subscriber, and thus it can be thought of as an unreachable “blackbox” inside of my WebAdaptor.

I want to write a unit test which incorporates a Mockito mock, and uses that mock to verify that executing WebAdaptor::run() causes Subscriber::init() to be called.

So here’s what I’ve got so far (inside WebAdaptorUnitTest):

@Test
public void runShouldInvokeSubscriberInit() {

    // Given
    Subscriber mockSubscriber = mock(Subscriber.class);
    WebAdaptor adaptor = new WebAdaptor();

    // When
    adaptor.run();

    // Then
    verify(mockSubscriber).init();
}

When I run this test, the actual Subscriber::init() method gets executed (I can tell from the console output and seeing files being generated on my local system), not the mockSubscriber, which shouldn’t do (or return) anything.

I have checked and re-checked: init is public, is neither static or final, and it returns void. According to the docs, Mockito should have no problem mocking this object.

So it got me thinking: do I need to explictly associate the mockSubscriber with the adaptor? If this is a case, then ordinarily, the following would normally fix it:

adaptor.setSubscriber(mockSubscriber);

But since I cannot add any such setter (please read my note above), I’m at a loss as to how I could force such an association. So, several very-closely-related questions:

  • Can anyone confirm that I’ve set the test up correctly (using the Mockito API)?
  • Is my suspicion about the missing setter correct? (Do I need to associate these objects via a setter?)
  • If my above suspicion is true, and I can’t modify WebAdaptor, are there any circumventions at my dispose?

Thanks in advance!

  • 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-27T16:39:56+00:00Added an answer on May 27, 2026 at 4:39 pm

    You need to inject the mock into the class which you are testing. You do not need access to Subscriber. The way mockito and other mocking frameworks help is that you do not need access to objects which you are interacting with. You do however need a way to get mock objects into the class you are testing.

    public class WebAdaptor {
    
        public WebAdaptor(Subscriber subscriber) { /* Added a new constructor */
           this.subscriber = subscriber;
        }
    
        private Subscriber subscriber;
    
        public void run() {
            subscriber.init();
        }
    }
    

    Now you can verify your interactions on the mock, rather than on the real object.

    @Test
    public void runShouldInvokeSubscriberInit() {
    
        // Given
        Subscriber mockSubscriber = mock(Subscriber.class);
        WebAdaptor adaptor = new WebAdaptor(mockSubscriber);  // Use the new constructor
    
        // When
        adaptor.run();
    
        // Then
        verify(mockSubscriber).init();
    }
    

    If adding the Subscriber to the constructor is not the correct approach, you could also consider using a factory to allow WebAdaptor to instantiate new Subscriber objects from a factory which you control. You could then mock the factory to provider mock Subscribers.

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

Sidebar

Related Questions

I have been asked to set up some authentication for some content on our
As part of preparation for an Interview I have been asked to read up
I know this has been asked several times and I have read all the
I have been asked to make some customisation changes to our Dynamics CRM 4.0
I have been asked to identify all read only text boxes in an Asp.Net
I know similar questions have been asked, I have read them. I have also
I have been asked to look at how to restrict read access on certain
Sorry if you feel like this has been asked but I have read the
I know that similar questions have been asked, but having read through them it
I have been asked to look into FileMaker for creating a pretty simple database

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.