views:

430

answers:

6

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) { ...}
}
A: 

Can you write a trivial stored procedure that fails/errors each time, then use that to test?

cagcowboy
I don't want to have to hit the database every time!
George Mauer
+2  A: 

Use reflection to instantiate the OracleException object? Replace

new OracleException()

with

object[] args = ... ;
(OracleException)Activator.CreateInstance(typeof(OracleException), args)
Matt Howells
This seems like it will work, how do I specify constructor arguments?
George Mauer
Activator.CreateInstance(typeof(OracleException), new object[] {yourParams});
Matt Howells
ugh, doesn't work for private parameters
George Mauer
+3  A: 

Use reflection to instantiate OracleException. See this blog post

Mauricio Scheffer
+3  A: 

Here is how you do it:

    ConstructorInfo ci = typeof(OracleException).GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance, null, new Type[] {typeof(string), typeof(int)}, null);
    var c = (OracleException)ci.Invoke(new object[] { "some message", 123 });

Thanks to all that helped, you have been upvoted

George Mauer
+1  A: 

Good solution George. This also works for SqlException too:

        ConstructorInfo ci = typeof( SqlErrorCollection ).GetConstructor( BindingFlags.NonPublic | BindingFlags.Instance, null, new Type[] {  }, null );
        SqlErrorCollection errorCollection = (SqlErrorCollection) ci.Invoke(new object[]{});

        ci = typeof( SqlException ).GetConstructor( BindingFlags.NonPublic | BindingFlags.Instance, null, new Type[] { typeof( string ), typeof( SqlErrorCollection ) }, null );
        return (SqlException) ci.Invoke( new object[] { "some message", errorCollection } );

-dave

David Gardiner
+2  A: 

I'm using the Oracle.DataAccess.Client data provider client. I am having trouble constructing a new instance of an OracleException object, but it keeps telling me that there are no public constructors. I tried all of the ideas shown above and keep getting a null reference exception.

object[] args = { 1, "Test Message" };
ConstructorInfo ci = typeof(OracleException).GetConstructor(BindingFlags.NonPublic 
     | BindingFlags.Instance, null, System.Type.GetTypeArray(args), null);
var e = (OracleException)ci.Invoke(args);

When debugging the test code, I always get a NULL value for 'ci'.

Has Oracle changed the library to not allow this? What am I doing wrong and what do I need to do to instantiate an OracleException object to use with NMock?

By the way, I'm using the Client library for version 10g.

Thanks,

Charlie

Charles Crawford