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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T21:02:22+00:00 2026-06-09T21:02:22+00:00

I recently stumbled upon this interesting concept that may save me much testing efforts.

  • 0

I recently stumbled upon this interesting concept that may save me much testing efforts.
What I do not understand is how can the provider be injected in runtime?

The scenario is trivial: I am constructing a mock object at run-time with my mocking framework of choice, but I do not know the name of the generated class in advance because it is a mock (so I can’t configure it in advance, not do I want to).

Did anybody make successful use of this technique in unit tests?

Thank you.

  • 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-09T21:02:24+00:00Added an answer on June 9, 2026 at 9:02 pm

    The concept described in that article is an Ambient Context that uses a Service Locator in the background.

    Because of the use of a static property and the use of the Service Locator, this pattern is very inconvenient for unit testing. To be able to run a test that verifies code that uses this singleton, you need to set up a valid Service Locator and configure it with the singleton (probably a mock instance) that you care about using testing.

    Even the example given by the article already suffers from these problems, since the “Do you like singletons?” code, is hard to test:

    if (DialogDisplayer.getDefault().yesOrNo(
        "Do you like singletons?"
    )) {
        System.err.println("OK, thank you!");
    } else {
        System.err.println(
            "Visit http://singletons.apidesign.org to"
            + " change your mind!"
        );
    }
    

    A better alternative is to use constructor injection to inject that singleton (please excuse my French, but I’m not a native Java speaker):

    public class AskTheUserController
    {
        private DialogDisplayer dialogDisplayer;
        private MessageDisplayer messageDisplayer;
    
        public AskTheUserController(DialogDisplayer dialogDisplayer,
            MessageDisplayer messageDisplayer)
        {
            this.dialogDisplayer = dialogDisplayer;
            this.messageDisplayer = messageDisplayer;
        }
    
        public void AskTheUser()
        {
            if (this.dialogDisplayer.yesOrNo(
                "Do you like singletons?"
            )) {
                this.messageDisplayer.display("OK, thank you!");
            } else {
                this.messageDisplayer.display(
                    "Visit http://singletons.apidesign.org to"
                    + " change your mind!"
                );
            }
        }
    }
    

    There was another ‘hidden’ dependency in that code: System.err.println. It got abstracted using a MessageDisplayer interface. This code has a few clear advantages:

    • By injecting both dependencies, the consumer doesn’t even need to know that those dependencies are singletons.
    • The code clearly communicates the dependencies it takes.
    • The code can easily be tested using mock objects.
    • The test code doesn’t need to configure a service locator.

    Your tests might look like this:

    @Test
    public void AskTheUser_WhenUserSaysYes_WeThankHim()
    {
        // Arrange
        bool answer = true;
    
        MockMessageDisplayer message = new MockMessageDisplayer();
        MockDialogDisplayer dialog = new MockDialogDisplayer(answer);
    
        AskTheUserController controller =
            new AskTheUserController(dialog, message);
    
        // Act
        controller.AskTheUser();
    
        // Assert
        Assert.AreEqual("OK, thank you!", message.displayedMessage);
    }
    
    @Test
    public void AskTheUser_WhenUserSaysNo_WeLetHimChangeHisMind()
    {
        // Arrange
        bool answer = true;
    
        MockMessageDisplayer message = new MockMessageDisplayer();
        MockDialogDisplayer dialog = new MockDialogDisplayer(answer);
    
        AskTheUserController controller =
            new AskTheUserController(dialog, message);
    
        // Act
        controller.AskTheUser();
    
        // Assert
        Assert.IsTrue(
            message.displayedMessage.contains("change your mind"));
    }
    

    Your test code will never be as intend revealing as the code above when you’re using the ‘injectable singleton’ pattern as shown in the article.

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

Sidebar

Related Questions

I want to share this odd but interesting situation I stumbled upon recently while
I just recently stumbled upon the fact that Declarative Services in OSGi can set
Recently stumbled upon a stock video footage site http://www.pond5.com/stock-video-footage/1/*.html , and saw that they
I've been working on topcoder recently and I stumbled upon this question which I
I recently stumbled upon a global hotkey class ( This one ), it works
I recently stumbled upon a cool feature in CVS where you can name revisions
A problem that I stumbled upon recently, and, even though I solved it, I
I recently stumbled upon this page . And I was particularly interested about the
I recently stumbled upon this: PCRE Regex Syntax - Recursive Patterns It appears to
So I recently stumbled upon this great library for handling HTTP requests in Python;

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.