locked
New to Unit Testing Question RRS feed

  • Question

  • I have a method in which I would like to "unit test". I am new to writing unit tests. Now, it maybe that I still don't completely understand how everything should work. I know SQL Server is in fact a "downstream system". So maybe this isn't a proper use of a unit test. Still I would think that this should be possible??

      I have written a method that returns a boolean based on a database call. Now I know if I give it driverid 1673 it returns as true. In my test I give it driverid 1673 and  I assert that it should return a true. I know this method works, however, when I run my UnitTest in VisualStudio TestExplorer I get the following error :

    Result Message: 
    Test method UnitTesting.UnitTest1+UnitTestSample.TestForEmployeeDriver threw exception:
    System.NullReferenceException: Object reference not set to an instance of an object.
    Result StackTrace: 
    at ConsoleApplication3_withUnitTests.JasonClass.AreTheyanEmployee(Int32 driverid) in c:\Users\jlivengood\Documents\Visual Studio 2012\Projects\ConsoleApplication3_withUnitTests\ConsoleApplication3_withUnitTests\Program.cs:line 46

    Line 46 is where the string connection is created  that uses ConfigurationManager class. The AreTheyanEmployee method is actually part of a ConsoleApp.When I run this method (giving it driver id 1673) in my console app it works fine with it returning a true. The "DHP" connection string is defined in my app config. It seems the that the test just doesn't like waiting for the response from sql server. Is this a proper use of a unit test? Is there something I'm not understanding? Would this be where I would use a mock object ? Can anybody help me to better understand what is going on here? Any help or advice would be greatly appreciated. The Method and the tests are below

    Jason 

    public bool AreTheyanEmployee(int driverid) { string connection = ConfigurationManager.ConnectionStrings["DHP"].ConnectionString; UserInfo useinfo = new UserInfo(); SqlConnection ConDHP = new SqlConnection(connection); ConDHP.Open(); SqlCommand selUseInfo = new SqlCommand("JL_GetLoginInfo", ConDHP); selUseInfo.CommandType = CommandType.StoredProcedure; selUseInfo.Parameters.AddWithValue("@DriverID", driverid); SqlDataReader rdr = selUseInfo.ExecuteReader(); if (rdr.Read()) { if (rdr["NonEmployeeParentID"] == null || rdr["NonEmployeeParentID"].ToString() == "" || rdr["NonEmployeeParentID"] == "") { ConDHP.Close(); return true; } else { ConDHP.Close(); return false;

    } } ConDHP.Close(); return false; }


                [TestMethod]
                public void TestTrueForEmployeeDriver()
                {
                    JasonClass js = new JasonClass();
                    Boolean tf = js.AreTheyanEmployee(1673);
                    Assert.IsTrue(tf);
    
                }

     

    Jason

    Friday, February 27, 2015 9:18 PM

Answers

  • Hi Jason,

    >> System.NullReferenceException: Object reference not set to an instance of an object.

    I have tried to reproduce your issue, and I found that for this error message, you could add the app.config in your consoleapp to your Unit Test Project. It seems that when you add your class library to the Unit Test Project, the app.config did not add to the Test Project, and when you call the “string connection = ConfigurationManager.ConnectionStrings["DHP"].ConnectionString;”. It run into error.

    So, I think you could do as below:

    #1 add a app.config to your Test Project
    #2 add the DHP ConnectionString to the app.config
    #3 build your project.

    Best Regards,

    Edward


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    • Marked as answer by Jazzcatone Monday, March 2, 2015 2:30 PM
    Monday, March 2, 2015 7:27 AM

All replies

  • Hi Jason,

    >> System.NullReferenceException: Object reference not set to an instance of an object.

    I have tried to reproduce your issue, and I found that for this error message, you could add the app.config in your consoleapp to your Unit Test Project. It seems that when you add your class library to the Unit Test Project, the app.config did not add to the Test Project, and when you call the “string connection = ConfigurationManager.ConnectionStrings["DHP"].ConnectionString;”. It run into error.

    So, I think you could do as below:

    #1 add a app.config to your Test Project
    #2 add the DHP ConnectionString to the app.config
    #3 build your project.

    Best Regards,

    Edward


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    • Marked as answer by Jazzcatone Monday, March 2, 2015 2:30 PM
    Monday, March 2, 2015 7:27 AM
  • That absolutely positively worked . That was the problem.Thank you so much as that would have never even occurred to me !!

    I will say that I'm a little puzzled by the fact that the unit test project would need the app.config with a connectionstring. The db connection itself is really made in the console app project....

    Is there a way to keep it to 1 app.config in the solution that we know of?

    --Jason


    Jason

    Monday, March 2, 2015 2:30 PM
  • Hi Jazzcatone,

    >> Is there a way to keep it to 1 app.config in the solution that we know of?
    In my option, it could be achieved that one app.config worked for multiple projets. The link below might be useful to you.

    http://stackoverflow.com/questions/4738/using-configurationmanager-to-load-config-from-an-arbitrary-location

    Since your original issue has been resolved, If the link above do not work for you, I will recommend you to open a new thread for this new issue.

    Thanks for your understanding.

    Best Regards,

    Edward


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Wednesday, March 4, 2015 9:27 AM