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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T02:58:58+00:00 2026-05-11T02:58:58+00:00

My first programming job introduced me to unit testing and the concept of mock

  • 0

My first programming job introduced me to unit testing and the concept of mock objects, but something always felt wrong about it.

Let’s say we were writing a bank app, and needed to mock a BankAccount object:

     // boilerplate code     public interface IBankAccount {         void Deposit(int amount);         void Withdrawal(int amount);         int getBalance();         int getAccountNumber();     }      public interface IBankAccountFactory {         IBankAccount getAccount(int accountNumber);     }      public class ProductionBankAccountFactory implements IBankAccountFactory {          public IBankAccount getAccount(int accountNumber) {             return new RealBankAccount(accountNumber);         }     }      public class MockBankAccountFactory implements IBankAccountFactory {         public IBankAccount getAccount(int accountNumber) {             return new MockBankAccount(accountNumber);         }     }      public static class BankAccountFactory {         // ewww, singletons!         public static IBankAccountFactory Instance;     }      // finally, my actual business objects     public class MockBankAccount implements IBankAccount {         public MockBankAccount(int accountNumber) { ... }         // interface implementation     }      public class RealBankAccount implements IBankAccount {         public RealBankAccount(int accountNumber) { ... }         // interface implementation     } 

Each class has a purpose:

  • The factory and factory interfaces exist to wrap up the constructors to our mock and real objects.
  • The static BankAccountFactory class allows us to assign BankAccountFactory.Instance an instance of IRealBankAccountFactory or MockBankAccountFactory at the start of our production app or tests respectively.
  • Once everything is set up properly, any class can grab an instance of IBankAccount simply by calling:
     BankAccountFactory.Instance.getAccount(accountNum); 

This works, but it results in a lot of boilerplate code. I shouldn’t have to write 5 new classes for each class I want to mock. I’m convinced there’s an easier way, so I have to ask the SO community:

Is there a better or preferred way of writing mock objects?

[Edit to add:] I appreciate the links to the mocking and DI frameworks, but right now I’m working on a 500 KLOC application, and at least 60% of the code consists of the boilerplate mock classes in the style above.

I just want to reduce the size of the codebase without re-writing large chunks of code for Yet-Another-Framework™, so it helps me more to see mock classes written by hand. 🙂

  • 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. 2026-05-11T02:58:58+00:00Added an answer on May 11, 2026 at 2:58 am

    The better way is to have someone else write it. Some options here are:

    Moq – http://code.google.com/p/moq/

    Rhino Mocks – http://ayende.com/projects/rhino-mocks.aspx

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

Sidebar

Ask A Question

Stats

  • Questions 77k
  • Answers 77k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer The $.getJSON() method does an HTTP GET and not POST.… May 11, 2026 at 3:27 pm
  • added an answer You need to supply a False parameter on the Close… May 11, 2026 at 3:27 pm
  • added an answer Why do you want to create an image? You could… May 11, 2026 at 3:27 pm

Related Questions

It's been mentioned to me that I'll be the sole developer behind a large
What should a small team choose for their first game, when they are low
I just made a browser using c# as one of my first projects of
I've just started my first development job for a reasonably sized company that has

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.