Mocking ExceptionManager.Process

Sep 18, 2012 at 11:22 PM

I am using Exception Handling Block in our application, and passing it using Dependency Injection.

As part of my Unit tests I mock the ExceptionManager, but now i need to ensure I mock the exceptionManager.Process method to make my tests work.

can you please advice on this?

 

- Vikram

 

 

 

 

Sep 19, 2012 at 6:38 AM

I would think that for the most part you would be unit testing smaller discrete portions of the application that wouldn't usually have a need for ExceptionManager.  For example, if you were unit testing the data access layer then you would test that you received an expected detailed exception and not a wrapped DataAccess exception.

Actually, how are you mocking ExceptionManager now?  With a concrete class or a mocking library or some other way?  What behavior do you want out of ExceptionManager.Process?

I'm assuming that you want the Process method to invoke the Action and then perform some predetermined exception handling (e.g. replacing an exception).  If that is the case then I would configure the container programmatically to give me the ExceptionManager behavior that I want for the test.  For example if I want to test that when I pass in null to a method I get back a SystemException and not the ArgumentNullException that is originally thrown then I could set up the test like this:

    public class MyLogic
    {
        public void DoSomething(string val)
        {
            if (val == null)
            {
                throw new ArgumentNullException("val");
            }
        }
    }


    public class UnitTest1
    {
        [TestMethod]
        [ExpectedException(typeof(SystemException))]
        public void DoSomethingWithNullArgument()
        {
            var builder = new ConfigurationSourceBuilder();

            builder.ConfigureExceptionHandling()
                   .GivenPolicyWithName("MyPolicy")
                     .ForExceptionType<ArgumentNullException>()
                       .ReplaceWith<SystemException>()
                       .ThenThrowNewException();
                    
            var configSource = new DictionaryConfigurationSource();
            builder.UpdateConfigurationWithReplace(configSource);

            EnterpriseLibraryCoreExtension coreExtension = new EnterpriseLibraryCoreExtension(configSource);
            IUnityContainer container = new UnityContainer();
            container.AddExtension(coreExtension);
            
            var exceptionManager = container.Resolve<ExceptionManager>();
            MyLogic logic = container.Resolve<MyLogic>();

            exceptionManager.Process(() => logic.DoSomething(null), "MyPolicy");
        }
    }

If you want Process() to do nothing then just configure empty policies.  i.e. builder.ConfigureExceptionHandling().GivenPolicyWithName("MyPolicy");

--
Randy Levy
Enterprise Library support engineer
entlib.support@live.com 

Sep 19, 2012 at 8:50 PM
Edited Sep 19, 2012 at 9:27 PM

Thanks for the Detailed example, actually here is what I was trying to do

Following is my DAO method

        public myObject GetDataById(int Id)
        {
            mExceptionManager.Process(mClient.getData(id),"my policy");
        }

to Mock the ExceptionManager -  I am using Rhino Mocks, 

Now If I need to test this method using my unit test, I would pass the the mock exceptionManager to the DAO in my unit test, here I would just want the exception manager's Process method to just execute the acton/func. I am not concerned about the exception type./handling of the exception.

Sep 19, 2012 at 9:44 PM

I can actually.. get rid of this issue by passing the actual ExceptionManager...  (as ideally I should not be mocking it...) but just wanted to know.. if there was a way to do that..

Or please advice if there is a better way of doing this..?

Sep 19, 2012 at 10:14 PM
vshantharaj wrote:

I can actually.. get rid of this issue by passing the actual ExceptionManager...  (as ideally I should not be mocking it...) but just wanted to know.. if there was a way to do that..

Or please advice if there is a better way of doing this..?

here is my temp solution.... i use a helper class to get my ExceptionManager... this is just in my unit test projects...

public static class EnterpriseLibraryHelper
    {
        public static ExceptionManager GetExceptionManager(string policyName)
        {
            var builder = new ConfigurationSourceBuilder();
            builder.ConfigureExceptionHandling().GivenPolicyWithName(policyName);
            var configSource = new DictionaryConfigurationSource();
            builder.UpdateConfigurationWithReplace(configSource);
            var coreExtension = new EnterpriseLibraryCoreExtension(configSource);
            IUnityContainer container = new UnityContainer();
            container.AddExtension(coreExtension);
            return container.Resolve<ExceptionManager>();
        }
    }

Sep 19, 2012 at 10:38 PM
Edited Sep 19, 2012 at 10:45 PM

You can also new up an ExceptionManagerImpl directly.  For example:

    var policies = new Dictionary<string, ExceptionPolicyImpl>();
    var policy1Entries = new Dictionary<Type, ExceptionPolicyEntry>
    {
        {
            typeof (ArithmeticException),
            new ExceptionPolicyEntry(typeof (ArithmeticException),
                PostHandlingAction.NotifyRethrow,
                new IExceptionHandler[] {new TestExceptionHandler("handler")})
        }
    };

    policies.Add("policy1", new ExceptionPolicyImpl("policy1", policy1Entries));

    ExceptionManager manager = new ExceptionManagerImpl(policies);

Where TestExceptionHandler is an IExceptionHandler for testing.  Or you could add an actual built-in IExceptionHandler.

--
Randy Levy
Enterprise Library support engineer
entlib.support@live.com