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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T07:08:45+00:00 2026-05-24T07:08:45+00:00

I have a question regarding unit-testing of factory method. Let say we have following

  • 0

I have a question regarding unit-testing of factory method.
Let say we have following code and want to test Foo.ToBar method.

class Bar
{
    public Bar(int someparam)
    {
    }
}

class Foo
{
    int m_someprivate;

    public Foo()
    {
        m_someprivate = 1;
    }

    public Bar TooBar()
    {
        return new Bar(m_someprivate);
    }
}

I saw two different methods. One of them will be something like that

[Test]
void TooBarTest()
{
    Foo foo = new Foo();

    Bar result = foo.TooBar();

    Assert.AreEqual(new Bar(1), result);
}

It’s very straight forward. However, I don’t like it for two reasons

a) We inadvertent test part of Bar class while testing Foo class. As example, if Bar constructor throws than our test will fail, which is kind of wrong, because
Foo class is fine. It’s a Bar class which have a problem.

b) I don’t like that production code is used in Assert new Bar(1). I would rather prefer to use some constant than some code which
may as example return different results depending on some external state (etc).

The other method which I saw is based on creation of standalone factory, which will create Bar

class Bar 
{
    public Bar(int someparam)
    {
    }
}

interface IBarFactory
{
    Bar create(int someparam);
}

class BarFactory : IBarFactory
{
    public Bar create(int someparam)
    {
        return new Bar(someparam);
    }
}

class Foo
{
    int m_someprivate;
    BarFactory m_barFactory;

    public Foo()
    {
        m_someprivate = 1;
        m_barFactory = new BarFactory();
    }

    public Bar TooBar()
    {
        return m_barFactory.create(m_someprivate);
    }

    public void setBarFactory(BarFactory barFactory)
    {
        m_barFactory = barFactory;
    }
}


[Test]
void TooBarTest()
{
    Mockery mockery = new Mockery()
    IBarFactory barFactoryMock = mockery.NewMock<IBarFactory>();
    Expect.Once.On(barFactoryMock).Method("create").With(new Object[] { 1 }).Will(Return.Value(new Bar(1)); 

    Foo foo = new Foo();
    foo.setBarFactory(barFactoryMock);

    foo.ToBar();
}

It looks like it’s better way. However, I don’t like that we have to create factory interface, factory, factory setter and make test more complex, just to test 1 line of code.

What are your thoughts on that? What do you prefer? Do you have any other method of testing it?

  • 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-24T07:08:45+00:00Added an answer on May 24, 2026 at 7:08 am

    I aggree with Tomas Jansson. Further I would inject IBarFactory in the constructor instead of instantiating BarFactory. Thus you never have to touch neither Bar nor BarFactory. Instead you can just stub IBarFactory when testing.

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

Sidebar

Related Questions

I have a question regarding unit testing. I'll outline it using an example. Say
I have a question regarding unit testing. I have a function which does the
I have a question regarding dependency injection. say i want to create a class
I have a question regarding unit test. I am going to test a module
I have a question regarding static function in php. let's assume that I have
I have question regarding the SQLAlchemy. How can I add into my mapped class
I have question regarding the use of function parameters. In the past I have
I have question regarding disabling browser caching. I have already found few solutions, and
I have a question regarding the onkeypress event on JavaScript. Is it possible to
I have a question regarding the integration of business objects developed using Linq To

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.