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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T12:28:58+00:00 2026-06-15T12:28:58+00:00

before I begin with my question I want to point out that I am

  • 0

before I begin with my question I want to point out that I am aware that there are tons of similar questions on stack overflow. Unfortunately none of these questions helped me finding a good solution in my concrete scenario.

The Problem:

I want to write a unit test for a static factory method which contains logic. I am looking for a way to unit test this method even if it is static. If that is not possible maybe someone can point out a better design for my class under test. I also considered using IoC but couldn’t see the advantage considering unit-testing.

The Code:

public class Db
{
    private XmlMapping mapping;

    public static Db<T> Create()
    {
        var mapping = XmlMapping.Create(typeOf(T).Name);
        return new Db(mapping);
    }

    private Db(XmlMapping mapping)
    {
        this.mapping = mapping;
    }
}

public class XmlMapping //class under test
{
    public static XmlMapping Create(string filename) //method under test
    {            
        try
        {
            ValidateFilename(filename);
            //deserialize xml to object of type XmlMapping
            var result = Deserialize(filename);
            if (result.IsInValid())
                throw Exception()
            return result; 
        }
        catch (Exception)
        {
            throw new DbException();
        }
    }
}

The method Create which I want to unit test is within the class XmlMapping. This method serializes a xml file and generates an object of type XmlMapping. I tried to write a stub for the serialization part. But didn’t want to call my Database Factory with a Mapping class in the constructor (constructor injection).

Edit:

My database factory is generic. The generic type is used to figure out which xml file should be louded i.e.: typeOf(T) = Customer –> XmlMapping-File = Customer.xml

The Solution (Thx to Jeff!):

public class XmlMapping : IMapping //class under test
{
    internal static Func<Type, IMapping> DeserializeHandler { get; set; }

    static XmlMapping()
    {
        DeserializeHandler = DeserializeMappingFor;
    }

    public static IMapping Create(Type type)
    {
        try
        {
            var mapping = DeserializeHandler(type);
            if (!mapping.IsValid())
                throw new InvalidMappingException();
            return mapping;
        }
        catch (Exception ex)
        {
            throw new DataException("Failed to load mapping configuration from xml file.", ex);
        }
    }

    internal XmlMapping(IMapping mapping)
    {
        this.Query = mapping.Query;
        this.Table = mapping.Table;
        this.Entity = mapping.Entity;
        this.PropertyFieldCollection = mapping.PropertyFieldCollection;
    }

    private XmlMapping() { }
}


[TestClass]
public class MappingTests //testing class
{
    [TestMethod]
    public void Create_ValidDeserialization_ReturnsObjectInstance()
    {
        XmlMapping.DeserializeHandler = MakeFakeHandlerFor(MakeMappingStub());
        var result = XmlMapping.Create(typeof(ActivityDto));
        Assert.IsInstanceOfType(result, typeof(XmlMapping));
    }
}
  • 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-15T12:28:59+00:00Added an answer on June 15, 2026 at 12:28 pm

    I would use a fake action handler to assist in verifying the content of the call to deserialize. Let’s add a Func delegate property and default that to your serialize method. Your XmlMapping class and test would like something like:

    public class XmlMapping //class under test
    {
    
        static XmlMapping()
        {
            // Default the handler to the normal call to Deserialize
            DeserializeHandler = Deserialize;
        }
    
        public static XmlMapping Create(string filename) //method under test
        {
            //deserialize xml to object of type XmlMapping
            //preudocode:
            var result = DeserializeHandler(string.Format("{0}.xml",filename));
            //...
            return result;
        }
    
        // Abstract indirection function to allow you to swap out Deserialize implementations
        internal static Func<string, XmlMapping> DeserializeHandler { get; set; }
    
        private static XmlMapping Deserialize(string fileName)
        {
            return new XmlMapping();
        }
    
    }
    
    public class CreateTests {
    
        public void CallingDeserializeProperly()
        {
    
            // Arrange
            var called = false;
            Func<string, XmlMapping> fakeHandler = (string f) =>
            {
                called = true; // do your test of the input and put your result here
                return new XmlMapping();
            };
    
            // Act
            XmlMapping.DeserializeHandler = fakeHandler;
            var m = XmlMapping.Create("test");
    
            // Assert
            Assert.IsTrue(called);
    
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

before I start I want to point out that I tagged this question as
Before I begin, let me just say that I know my question is almost
Before I begin: I have spent a long time on many forums (including Stack
I would like to turn everyone's attention to this question before I begin as
Before we begin, don't blame me, I didn't design the database. I am well
Before I begin I'll freely admit it's something in my code but it's not
Before I begin, I would like to highlight the structure of what I am
Just before I begin heres a small overview of what I'm trying to achieve
To clarify before I begin, this is NOT homework but rather I am studying
First of all, before I begin, I am using VC++ 2008 professional, running an

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.