Error debugging by null on connection

2

I want to document a unit test but I'm getting an error:

  

System.Exception: 'Error closing database connection: Object reference not set to an instance of an object.'

My test case has been coded as follows:

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using DAL.Model;
using DAL.Persistence;

namespace GErenciamentoTarefas_TesteUnitario
{
    [TestClass]
    public class UnitTest1
    {
        TarefaDAO td = new TarefaDAO();
        int retorno1 = 0;
        int retorno2 = 0;
        int retorno3 = 0;

        [TestMethod]
        public void testeIncluirTarefa()
        {
            Tarefa tarefa1 = new Tarefa();
            tarefa1.DataEntrega = "19/11/2018";
            tarefa1.Nome = "Nome em Teste";
            tarefa1.Responsavel = "Responsavel em Teste1";
            tarefa1.Tipo = "1";

            Tarefa tarefa2 = new Tarefa();
            tarefa2.DataEntrega = "22/11/2018";
            tarefa2.Nome = "Nome em Teste";
            tarefa2.Responsavel = "Responsavel em Teste2";
            tarefa2.Tipo = "2";

            Tarefa tarefa3 = new Tarefa();
            tarefa3.DataEntrega = "25/11/2018";
            tarefa3.Nome = "Nome em Teste";
            tarefa3.Responsavel = "Responsavel em Teste3";
            tarefa3.Tipo = "3";

            retorno1 = td.Gravar(tarefa1);
            retorno2 = td.Gravar(tarefa2);
            retorno3 = td.Gravar(tarefa3);

            Assert.AreEqual(0, retorno1);
            Assert.AreEqual(0, retorno2);
            Assert.AreEqual(0, retorno3);
        }

    }
}

My connection class is thus encoded

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.OleDb;
using System.Data;
using System.Configuration;

namespace DAL.Persistence
{
    public class ConexaoAccess
    {

        protected OleDbConnection Con;
        protected OleDbCommand Cmd;
        protected OleDbDataAdapter Da;

        protected void AbrirConexao()
        {
            try
            {
                //Busca a string de conexão com o banco no arquivo Web.config
                Con = new OleDbConnection(ConfigurationManager.ConnectionStrings["conexaoBanco"].ConnectionString);
                Con.Open();
            }
            catch (Exception ex)
            {
                throw new Exception("Erro ao abrir conexão com banco de dados: " + ex.Message);
            }

        }

        protected void FecharConexao()
        {
            try
            {
                Con = new OleDbConnection(ConfigurationManager.ConnectionStrings["conexaoBanco"].ConnectionString);
                Con.Close();
            }
            catch (Exception ex)
            {
                throw new Exception("Erro ao fechar conexão com banco de dados: " + ex.Message);
            }
        }



    }
}

I tried to fix it by changing the position of the object OleDbConnection :

public class ConexaoAccess
{

protected OleDbConnection Con = new OleDbConnection(ConfigurationManager.ConnectionStrings["conexaoBanco"].ConnectionString);
protected OleDbCommand Cmd;
protected OleDbDataAdapter Da;

but I was not successful.

The debugger points out that the error is always when closing the connection:

What is the solution?

    
asked by anonymous 17.11.2018 / 23:09

1 answer

1

There are several errors there. Test this way and nothing is almost the same. This does not really test, it does not create testing worthy situations.

Capturing exception to throw another exception does not make sense , but mostly capturing Exception is an error , you catch errors that nm is trying to handle.

Opening a connection and then closing it on the next line doing nothing makes even less sense.

Actually the right thing is not to have to close the connection explicitly like this , that is, you have architectural errors in your application. It is possible to even separate the opening of the connection but it is much more complex than this and is only recommended for those who know how to create classes with complete functionalities, appropriate handling of exceptions

And you can see other minor problems in your code. The impression is that you are not understanding what this code performs, and this is even more of a reason not to worry about formal tests so they are valid when there is understanding.

I could try to fix the error, but would keep all other errors that are more serious. The best help I can give is to give up this architecture or study deeply how it should be done to do it properly. It would be tricky to give an answer with a solution. And I even preferred to have this class. It brings trouble and no advantage.

    
17.11.2018 / 23:34