Error debugging by null on connection

Asked

Viewed 60 times

2

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

System.Exception: 'Error closing database connection: Undefined object reference for an instance of an object.'

My test case was so coded:

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 so coded

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 correct 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 unsuccessful.

Debugger points out that the error is always when closing the connection:

Mostrando o erro

What is the solution?

  • Did the answer solve your question? Do you think you can accept it? See [tour] if you don’t know how you do it. This would help a lot to indicate that the solution was useful to you. You can also vote on any question or answer you find useful on the entire site

1 answer

1

There are several errors there. Testing this way and nothing is almost the same thing. It doesn’t really test, it doesn’t create a situation that deserves testing.

Capturing exception to throw another exception doesn’t make any sense, but mostly capture Exception is a mistake, you capture errors that nm are wanting to treat.

Opening a connection to then close it on the next line without doing anything makes less sense yet.

In fact it is not right to have to explicitly close the connection like this, or has architectural errors in its application. It is even possible to separate the opening of the connection but it is much more complex than this and only recommended for those who know how to create classes with full functionality, proper handling of exceptions

And you can see there are other minor problems with your code. The impression is that you don’t understand what this code performs, and that’s even more reason not to worry about formal tests like this, they are valid when there is understanding.

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.