I am using a mock object in RhinoMocks to represent a class that makes calls to MessageQueue.GetPublicQueues. I want to simulate the exception thrown when message queueing is operating in workgroup mode, which is a MessageQueueException, to ensure that I am catching the exception correctly
The MessageQueueException has no public constructor, only the standard protected constructor for an exception. Is there an appropriate way to throw this exception from the mock object / Expect.Call statement?
Reflection can break the accessibility rulez. You will void the warranty, a .NET update can easily break your code. Try this:
using System.Reflection;
using System.Messaging;
...
Type t = typeof(MessageQueueException);
ConstructorInfo ci = t.GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance,
null, new Type[] { typeof(int) }, null);
MessageQueueException ex = (MessageQueueException)ci.Invoke(new object[] { 911 });
throw ex;