Search code examples
c#.netoraclemockingoracleclient

How can I mock/fake/stub sealed OracleException with no public constructor?


In my tests I need to test what happens when an OracleException is thrown (due to a stored procedure failure). I am trying to setup Rhino Mocks to

Expect.Call(....).Throw(new OracleException());

For whatever reason however, OracleException seems to be sealed with no public constructor. What can I do to test this?

Edit: Here is exactly what I'm trying to instantiate:

public sealed class OracleException : DbException {
  private OracleException(string message, int code) { ...}
}

Solution

  • It seems that Oracle changed their constructors in later versions, therefore the solution above will not work.

    If you only want to set the error code, the following will do the trick for 2.111.7.20:

    ConstructorInfo ci = typeof(OracleException)
                .GetConstructor(
                    BindingFlags.NonPublic | BindingFlags.Instance, 
                    null, 
                    new Type[] { typeof(int) }, 
                    null
                    );
    
    Exception ex = (OracleException)ci.Invoke(new object[] { 3113 });