Search code examples
c#unit-testingado.netconnection-stringn-tier-architecture

ConnectionString Property Not initialized error occurded while unit testing business logic function


I am trying to perform unit test on a business logic function of a 3-tier Windows form C# application. But I'm getting this error:

Message: Test method Cognizant.Dotnet.EMS.UnitTest.BusinessAddEmpDtlsTest.TestBusinessAddEmpDtls15 threw exception: System.InvalidOperationException: The ConnectionString property has not been initialized.

Now I'm new to unit testing. I have created a visual studio unit test project in my solution. Here is my business layer or tier class:

public class BusinessAddEmployee {
    DataAddEmployee objDataAddEmp;
    public BusinessAddEmployee()
    {            
        objDataAddEmp = new DataAddEmployee();
    }
    public int BusinessAddEmpDetails(EntityAddEmployee objEntityAddEmployee) {          
        SqlParameter[] objDataParams = new SqlParameter[5];
        objDataParams[0] = new SqlParameter("@EmpId", SqlDbType.Int) {Value = objEntityAddEmployee.EmpID};
        objDataParams[1] =
            new SqlParameter("@EmpName", SqlDbType.VarChar, 25) {Value = objEntityAddEmployee.EmpName};
        objDataParams[2] =
            new SqlParameter("@DeptName", SqlDbType.VarChar, 25) {Value = objEntityAddEmployee.DepartmentName};
        objDataParams[3] =
            new SqlParameter("@Location", SqlDbType.VarChar, 25) {Value = objEntityAddEmployee.Location};
        objDataParams[4] = 
            new SqlParameter("@ContactNumber", SqlDbType.BigInt) {Value = objEntityAddEmployee.ContactNo};
        var result = objDataAddEmp.DataAddEmployeeDetails(objDataParams);
        return result;
    }
}

And here is the Entity layer/tier class for AddEmployee

public class EntityAddEmployee {
    public Int32? EmpID { get; set; }
    public string EmpName { get; set; }
    public string DepartmentName { get; set; }
    public string Location { get; set; }
    public long? ContactNo { get; set; }        
}

The data layer class is this:

public class DataAddEmployee
{
    private static string conStr;
    private SqlConnection objConnnection;
    SqlCommand objCommand;        
    public DataAddEmployee()
    {
        conStr = ConfigurationManager.AppSettings["Connection"];
        objConnnection = new SqlConnection(conStr);            
    }
    public int DataAddEmployeeDetails(SqlParameter[] objParams) {
        objCommand = new SqlCommand("USPEmpDtls", objConnnection);
        objCommand.CommandType = CommandType.StoredProcedure;
        objCommand.Parameters.AddRange(objParams);
        objConnnection.Open();**//This line throws the exception**    
        int result;
        try
        {
            result = objCommand.ExecuteNonQuery();
        }
        catch (Exception e)
        {
            throw new InvalidOperationException();
        }           
        objConnnection.Close();
        return result;
    }       
}

The unit test class is given below:

[TestClass]
public class BusinessAddEmpDtlsTest
{
    private BusinessAddEmployee objBusinessAddEmp;
    private EntityAddEmployee objEntityAddEmp;

    [TestInitialize]
    public void Init()
    {
        objBusinessAddEmp = new BusinessAddEmployee();
        objEntityAddEmp = new EntityAddEmployee();

    }
    [TestMethod]        
    public void TestBusinessAddEmpDtls15()
    {
        objEntityAddEmp.EmpID = 11112;
        objEntityAddEmp.EmpName = "John Die";
        objEntityAddEmp.DepartmentName = "Audit";
        objEntityAddEmp.Location = "Dhaka";
        objEntityAddEmp.ContactNo = Convert.ToInt64(01999999999);           
        Assert.AreEqual(1, objBusinessAddEmp.BusinessAddEmpDetails(objEntityAddEmp));//**I have compared with 1 since it is a valid operation**//           
    }
}

The connection string works fine since I have successfully inserted data into database from the WindowsForm I created. Everything works fine except the unit test function.

Can anyone tell me what is the problem here why is the exception is occurring? I have initialized the connection string in the data layer class constructor.


Solution

  • Connection string appears to be coming from configuration manager

    conStr = ConfigurationManager.AppSettings["Connection"];
    

    Confirm you are getting a proper connection string when running the unit test.

    If not then check to make sure the unit test project has an app.config file with the settings necessary to exercise the test.

    That said, consider reviewing the current design of the code in question as it appears to be tightly coupled to implementation concerns that make it difficult to test in isolation without negative side-effects.