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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T06:07:25+00:00 2026-05-29T06:07:25+00:00

Given a abstract factory implementation: public class FooFactory : IFooFactory { public IFoo Create(object

  • 0

Given a abstract factory implementation:

public class FooFactory : IFooFactory {
   public IFoo Create(object param1, object param2) {
      return new Foo(param1, param2);
   }
}

What unit tests would be written for this class? How can I verify that param1 and param2 were forwarded to the creation of Foo? Do I have to make these public properties of Foo? Wouldn’t that be breaking encapsulation? Or should I leave this to integration testing?

  • 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-29T06:07:25+00:00Added an answer on May 29, 2026 at 6:07 am

    Here’s how I would write one of a couple of unit tests for such a factory (with xUnit.net):

    [Fact]
    public void CreateReturnsInstanceWithCorrectParam1()
    {
        var sut = new FooFactory();
        var expected = new object();
        var actual = sut.Create(expected, new object());
        var concrete = Assert.IsAssignableFrom<Foo>(actual);
        Assert.Equal(expected, concrete.Object1);
    }
    

    Does it break encapsulation? Yes and no… a little bit. Encapsulation is not only about data hiding – more importantly, it’s about protecting the invariants of objects.

    Let’s assume that Foo exposes this public API:

    public class Foo : IFoo
    {
        public Foo(object param1, object param2);
    
        public void MethodDefinedByInterface();
    
        public object Object1 { get; }
    }
    

    While the Object1 property slightly violate the Law of Demeter, it doesn’t mess with the invariants of the class because it’s read-only.

    Furthermore, the Object1 property is part of the concrete Foo class – not the IFoo interface:

    public interface IFoo
    {
        void MethodDefinedByInterface();
    }
    

    Once you realize that in a loosely coupled API, concrete members are implementation details, such concrete-only read-only properties have a very low impact on encapsulation. Think about it this way:

    The public Foo constructor is also a part of the API of the concrete Foo class, so just by inspecting the public API, we learn that param1 and param2 are part of the class. In a sense, this already ‘breaks encapsulation’, so making each parameter available as read-only properties on the concrete class doesn’t change much.

    Such properties provide the benefit that we can now unit test the structural shape of the Foo class returned by the factory.

    This is much easier than having to repeat a set of behavioral unit tests that, we must assume, already cover the concrete Foo class. It’s almost like a logical proof:

    1. From tests of the concrete Foo class we know that it correctly uses/interacts with its constructor parameters.
    2. From these tests we also know that the constructor parameter is exposed as a read-only property.
    3. From a test of the FooFactory we know that it returns an instance of the concrete Foo class.
    4. Furthermore, we know from the tests of the Create method that its parameters are correctly passed to the Foo constructor.
    5. Q.E.D.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given the following code : public abstract class Participant { private String fullName; public
Given this example: abstract class Base { type Value } case object Foo extends
Given the following classes... public abstract class FooBase<TBar> where TBar : BarBase{} public abstract
Given the following CRTP type in C#: public abstract class DataProviderBase<TProvider> where TProvider :
Given this snippet of code public abstract class Foo { private static SqlConnection _sqlConnection;
Given the following code : package core; public abstract class GeometricElement { private float
Given the model: public abstract class Person { public int Id {get;set;} } public
Given these base classes and interfaces public abstract class Statistic : Entity, IStatistic {
Given the following model: class Project(models.Model): project_name = models.CharField(max_length=255) abstract = models.TextField(blank=True, null=True) full_description
Given this: abstract class ViewPresenterPair { type V <: View type P <: Presenter

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.