How to raise an OracleException for test purposes

2.1k Views Asked by At

I have a function that executes some SQL commands, and I've created a logger that I write in the file the command that was executed and the number of rows that were affected, but I also need to write down the command that may have raised an OracleException, so I've done this piece of code:

public string ExecuteCommand(List<string> comandos)
        {
            var excepção = string.Empty;
            var executar = new OracleCommand
            {

                Connection = Updater.OraConnection,
                CommandType = CommandType.Text
            };

            try
            {
                Logg("Inicio da execução de comandos");
                foreach (var variable in comandos)
                {
                    excepção = variable;
                    executar.CommandText = variable;
                    throw new OracleException(0, "comando", "stuff", "adasds");
                  var Afectados =  executar.ExecuteNonQuery();
                    Logg(variable);
                    Logg("Linhas afectadas: " + Afectados);

                }

            }
            catch (OracleException)
            {
                Logg("Erros:");
                Logg(excepção);
                return excepção;

            }

            return excepção;


        }

I've tried to search everywhere but I cant fint any suitable or even focused answers, so Im kinda lost for why cant I raise an oracleException as I did like this: throw new OracleException(0, "comando", "stuff", "adasds");

It just says that Cannot access constructor here due to its protection level. Any help would be aprecciated

2

There are 2 best solutions below

1
Jeremy Thompson On BEST ANSWER
public class OracleException : Exception
{

}

The class needs to have its scope set to public or internal. The constructor cannot be accessed as its a private class.

0
Thulani Chivandikwa On

If you just want to simulate the exception being thrown and do not care about interrogating the object then.

private OracleResilienceManager CreateSut()
{
    return new OracleResilienceManager(_resilienceSettings);
}

throw System.Runtime.Serialization.CreateSafeUninitializedProtectedType<OracleException>();

In my case I was testing a retry policy and wanted to test the retry logic when this exception is thrown. Had no need to access the object itself.