10

Ok so i just got an assignment where i have to perform unit testing on a class with a private constructor.

Now how am i suppose to do unit testing without initializing a class when all the methods are also non static.

Is there any way i can do unit testing(without reflection)on a class with a private constructor ?

5
  • basically, not possible without reflection, but you can post your code sample to see whether it can be worked around Commented Mar 27, 2013 at 16:24
  • 4
    How do you create instances of these classes? Factory Method? Singleton? I love when unit tests show design issues in your code. Commented Mar 27, 2013 at 16:25
  • I guess, the class is a Singleton. Can you confirm? Commented Mar 27, 2013 at 16:27
  • Can you add a snippet of the class to your question to provide more context? Commented Mar 27, 2013 at 17:32
  • nop its not a singleton class. it just has 2 overloaded private constructors and 2 private methods. Commented Mar 27, 2013 at 17:41

6 Answers 6

17

If you cannot make the class public, you can still test it easily by creating an instance of it this way:

var anInstance = (YourPrivateClass)Activator.CreateInstance(typeof(YourPrivateClass), true);

This will give you an instance of your class that you can then populate.

Another helpful testing bit is if you have internal methods (not private), you can access them by making internals visible to your test class. You add this line in assemblyinfo.cs of the class with the internal methods:

[assembly: InternalsVisibleTo("YourSolution.Tests")]
Sign up to request clarification or add additional context in comments.

2 Comments

How can you create a fake / mock instance and not a real one?
this doesn't work if the parameterless constructor is private
8

If this class has a private constructor, is this to be used publicly? If not, it may be best not to unit test it. If this is the case, the code that is public should test this code in itself by calling it.

Unit testing is there to test what is to be used by the public - by interfacing code in between application layers for instance. Take an input, I want this output. That is really what unit testing is about. Unit testing doesn't care what is in the actual method. As long as it returns what you want, performs the desired action, you have a pass.

5 Comments

well infact there is nothing public in the class at all.
@WinCoder Then how the hell do you use it?
well today when i asked for clarification i have been told that i have to infact correct the mistakes in the code too. Like making stuff public....
A private constructor can be used to protect state, such as the case of DDD entities, while enabling libraries like entity framework to still instantiate the class. While public constructors only exist with parameters that can instantiate the entity in a valid state. Creating an (invalid) entity via the private constructor is plenty for testing the behavior of individual methods. It's an unnecessary amount of setup to try and mock all the other requirements for the entity (Many of which have private or internal constructors of their own)...
Often a class has static method(s) to create instances instead of calling new.
2

You should be testing through a public API -- there must be some way that the class you want to test is instantiated and used.

Comments

0

Unit tests are typically written and run to ensure that code meets its design and behaves as intended.

Creating a non-static class on which you cannot create an instance i.e. private constructor(s) only, might never be useful, in otherwords its is never Unit Testable.

In order to be Unit testable:

  1. You should be able to create an instance of the class.
  2. Testable Function should be either Public or Internal. You could test Internal function by making your assembly as a Friend Assembly

Comments

0

It might be a singleton and you don't want the public constructor for the class. Decorate the constructor with: [ExcludeFromCodeCoverage]

Comments

0

I realize that the OP (Win Coder) specifically asks for a solution that doesn't use reflection in their question. However, Google serves this as a top result for general questions about testing private constructors in C#.

So, in case you've arrived here and are okay with using reflection. Here's a solution.

In this example, I'm creating an instance of MyClass using a private constructor that takes three (and only three) parameters of types int, string, and string in that order. Types, order and number of parameters need to match.


var constructor = typeof(MyClass).GetConstructor(
    BindingFlags.NonPublic | BindingFlags.Instance,
    [typeof(int), typeof(string), typeof(string)]);

var myObject = constructor?.Invoke([id, name, desc]) as MyClass;

// Run whatever unit tests you need to run on myObject.

For those who disagree with unit testing private constructors, there is an Entity Framework Core use case where your private constructor is being used publically. Douglas Gaskell mentioned it in his comment on the accepted answer.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.