dimanche 3 mai 2015

Unit test faces NullReferenceException when try to access a private accessor

In the main application:

namespace ConsoleApplication1
{
    public class Patient
    {
    ...
        public double height { get; private set; }

And in unit test:

Patient p1=new Patient();
BindingFlags flags=BindingFlags.Instance | BindingFlags.NonPublic;
typeof(Patient).GetField("height", flags).SetValue(p1, "67.2");

My unit test does not work and it gives me

UnitTest.MyTest.idealCal threw exception: System.NullReferenceException: Object reference not set to an instance of an object.

The error occures at the line of SetValue

How to fix it?





Aucun commentaire:

Enregistrer un commentaire