1
[TestClass]
public class SearchTreeTest
{
    static IOiContext iContext;
    static SearchTree searchTree;

    [ClassInitialize]
    public static void InitializeContext(TestContext context)
    {
        //do some stuff here ....
        //initialize searchTree here
    }
    [TestMethod]
    public void SomeMethod_SomeState_SomeExpectation()
    {
        //My test will fail because an error will be thrown here.
        parameters = searchTree.FindParameters(...)
        ...
    }
}

I am using a static field, searchTree, in my unit test because I want all of my tests to have access to this field. I initialize it in the method with the ClassInitialize attribute so that it should be ran before any of my unit tests. However, when I use the field in any of the test methods, it fails because a NullReferenceException is thrown.

I am new to unit testing and also relatively new to c# as well but I figured that this would be quite a simple fix. When searching online though, I couldn't find anything about what I may be doing wrong. Any suggestions?

Edit

    [TestClass]
    public class SearchTreeTest
    {
        static IXiContext iContext;
        static SearchTree searchTree;

        [ClassInitialize]
        public static void InitializeContext(TestContext context)
        {
            string serverUrl = "http://192.168.3.10:58080/XiServices/ServerDiscovery";
            //private ServiceEndpoint sep;
            IXiEndpointDiscovery iEndpointDiscovery = new XiEndpointDiscovery(serverUrl) as IXiEndpointDiscovery;
            ServiceEndpoint sep = iEndpointDiscovery.GetServiceEndpointsByBinding("IResourceManagement", typeof(NetTcpBinding)).First();

            iContext = XiContext.Initiate(sep, iEndpointDiscovery.ServerEntry,
                    300000, (uint)ContextOptions.EnableJournalDataAccess +
                    (uint)ContextOptions.EnableAlarmsAndEventsAccess + (uint)ContextOptions.EnableDataAccess,
                    (uint)System.Threading.Thread.CurrentThread.CurrentCulture.LCID,
                    Guid.NewGuid().ToString());

            ServiceEndpoint readsep = iEndpointDiscovery.GetServiceEndpointsByBinding("IRead", typeof(NetTcpBinding)).First();
            IXiEndpointBase readEndpoit = iContext.OpenEndpoint(readsep, 30000, new TimeSpan(5000));           

            Node ai = new Node("AI");
            Node ti_101 = new Node("TI-101/AI1/PV.CV");
            Node di_101 = new Node("DI-101/DI1/");
            Node ai_101 = new Node("AI-101/AI1/PV.CV");
            Node aic_30 = new Node("AIC_30");
            Node di_101_pv = new Node("DI-101/DI1/PV_D.CV");
            Node di_101_out = new Node("DI-101/DI1/OUT_D.CV");
            Node aic_301 = new Node("AIC_301/AI1/PV.CV");
            Node aic_302 = new Node("AIC_302/AI/");
            Node aic_302_pv = new Node("AIC_302/AI1/PV.CV");
            Node aic_302_out = new Node("AIC_302/AI1/OUT.CV");

            searchTree = new SearchTree("DA:/../MODULES/", iContext);
            searchTree.Root.Children.Add(ai);
            searchTree.Root.Children.Add(ti_101);
            searchTree.Root.Children.Add(di_101);
            ai.Children.Add(ai_101);
            ai.Children.Add(aic_30);
            aic_30.Children.Add(aic_301);
            aic_30.Children.Add(aic_302);
            aic_302.Children.Add(aic_302_pv);
            aic_302.Children.Add(aic_302_out);
            di_101.Children.Add(di_101_pv);
            di_101.Children.Add(di_101_out);
        }

        [ClassCleanup]
        public static void CleanUpContext()
        {
            iContext.Dispose();
            iContext = null;
        }
        [TestMethod]
        public void staticMethod()
        {
            //I added this just so see what would happen.
            Assert.IsNull(searchTree);
        }

        [TestMethod]
        public void FindParameters_SearchRoot_ReturnsListOfParameter()
        {
            var parameters = searchTree.FindParameters("DA:/../MODULES/");
            var count = parameters.Count;
            var paths = from p in parameters
                    select p.Path;

            var actualPaths = paths.ToList();
            Assert.AreEqual(4, count, count.ToString());
            string[] expectedPaths =  
            {
                "DA:/../MODULES/",
                "DA:/../MODULES/AREA_A",
                "DA:/../MODULES/AREA_1",
                "DA:/../MODULES/BOILER_3"
            };

            foreach (string p in expectedPaths)
            {
                Assert.IsTrue(actualPaths.Contains(p), actualPaths[2]);
            }
        }
    }

Edit 2

See the image below. When I debug my test, it crashes when I get to the point indicated in the image below. SearchTree is a class that has a property called Root which is of type Node. Node has a property called Children which is a List. I didn't post this earlier because I could not connect to the server from a remote machine when debugging for some reason.

Debug session

Solution

The issue was not with the searchTree object itself. It was the properties (Children property) of the searchTree which were throwing the NullReferenceException.

searchTree = new SearchTree("DA:/../MODULES/", iContext);
searchTree.Root.Children = new List<Node>();
searchTree.Root.Children.Add(ai);
searchTree.Root.Children.Add(ti_101);
searchTree.Root.Children.Add(di_101);
ai.Children = new List<Node>();
ai.Children.Add(ai_101);
ai.Children.Add(aic_30);
aic_30.Children = new List<Node>();
aic_30.Children.Add(aic_301);
aic_30.Children.Add(aic_302);
aic_302.Children = new List<Node>();
aic_302.Children.Add(aic_302_pv);
aic_302.Children.Add(aic_302_out);
di_101.Children = new List<Node>();
di_101.Children.Add(di_101_pv);
di_101.Children.Add(di_101_out);
jaromey
  • 666
  • 2
  • 10
  • 27

1 Answers1

-1

You should assign a value to a variable before use it

static SearchTree searchTree = new SearchTree();
Alex Ovechkin
  • 810
  • 6
  • 20
  • 1
    The OP is claiming that he *did* already assign a value to the field before using it. That claim might be wrong, but your answer won't help. –  Sep 15 '15 at 12:36
  • 1
    I do not see initialization in the code he provided. Do you? – Alex Ovechkin Sep 15 '15 at 12:38