Errr, some catchy title.... RSS 2.0
 Wednesday, August 29, 2007

Below are two quick tips for testing non public classes and methods. I am not saying that it is a best practice to test your non public methods (usually indicates that there is something wrong with the design), but sometimes you just have to do it :).


Testing internal classes:



  1. Right click your test assembly.

 

2.Select a file

3.Instead of clicking Add, select 'Add as Link'.

 



Next, comes up testing non public methods:


The code below uses MbUnit testing framework (much better then NUnit). MbUnit has a class called Reflector which allows you to test non public fields, properties and methods. The only problem with this is the same one I have with many of the mocking frameworks: all of the names of properties and methods must be passed as strings, therefore there is no compile time validation. But hey, you can't have everything. :)


internal class ItemProcessor

{

      public ItemProcessor (List<ItemAttributeRiskFactor> attributeRiskFactors)

      {....}

.......

.......


      private bool CheckIfItemAttributeRiskFactorCorrespondsToItemAttibute(ItemAttributeRiskFactor risk,

                                                                                                                        ItemAttribute attribute)

      {

            if (risk.AttributeId == attribute.Id &&

               risk.AttributeValue == attribute.AttributeValue)

                     return true;

            return false;

      }

  }


The above code, is from one of my pet projects so you must forgive me that you don't understand what the class does. The important thing is that the class has private method that I want to test.

Below is the test code.



using MbUnit.Framework;

using MbUnit.Framework.Reflection;


[TestFixture]

class ItemProcessorFixture

{

   private Reflector reflector;

   private ItemAttribute attribute;

   private ItemAttributeRiskFactor risk;


   [TestFixtureSetUp]

   public void FixtureSetup()

   {

         ItemProcessor processor = new ItemProcessor(new List<ItemAttributeRiskFactor>());

         reflector = new Reflector(processor);

   }


   [SetUp]

   public void TestSetup()

   {

         attribute = new ItemAttribute();

         risk = new ItemAttributeRiskFactor(4, "HIGH", 50, false);

   }


   [Test]

   public void ItemAttributeAndItemAttributeRiskFactorShouldCorrespond()

   {

         attribute.Id = 4;

         attribute.AttributeValue = "HIGH";


         Assert.IsTrue((bool)reflector.InvokeMethod(

                     "CheckIfItemAttributeRiskFactorCorrespondsToItemAttibute", risk, attribute));

      }

   }


The interesting part of the code are in bold. We use MbUnit's Reflector class to test private method of the ItemProcessor class. Furthermore, the Reflector allows us to set and get fields and properties.

Overall, while it isn't a perfect way to test non public methods, it sure is better then not testing them at all :).

Wednesday, August 29, 2007 3:42:36 PM (GMT Standard Time, UTC+00:00)  #    Comments [0] -
Testing | TDD
Comments are closed.
About the author/Disclaimer

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright 2008
Marcin Waligora
Sign In
All Content © 2008, Marcin Waligora
DasBlog theme 'Business' created by Christoph De Baene (delarou)