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;
You can cause this by trying to create an invalid queue. Probably safer then being held captive by framework changes (through using private/protected constructors):
MessageQueue mq = MessageQueue.Create("\\invalid");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With