Wednesday, February 13, 2008

100% code coverage or is it really

Hi wrote a little application that I wanted demonstrate code coverage features of MS TEST.

public interface IDataAccess
{
List<string> GetUsers(bool includeInactive);
}
public class BusinessLogic
{
private IDataAccess m_DataAccess;
public BusinessLogic(IDataAccess da)
{
m_DataAccess = da;
}
public List<string> ListUsers(bool includeInactive)
{
return m_DataAccess.GetUsers(includeInactive);
}
}
[TestMethod()]
public void TestListUsers()
{
MockRepository mockery = new MockRepository();
IDataAccess mockDataAccess = mockery.CreateMock<IDataAccess>();
BusinessLogic bl = new BusinessLogic(mockDataAccess);
bool includeInactive = true;
List<string> expectedList = new List<string>();
List<string> returnedList;
expectedList.Add("1");
using (mockery.Record())
{
Expect.Call(mockDataAccess.GetUsers(includeInactive)).Return(expectedList);
}
using (mockery.Playback())
{
returnedList = bl.ListUsers(includeInactive);
}
Assert.Equals(returnedList, expectedList);
}
}



code coverage results are (100%):



Mezer_02-14_01-14-45



Take a minute and look again at the above code ( review it :) ).



The big issue in the above test is that we didn't take in mind the input parameter to the BL that also HAVE to be passed to the DAL.



So actually we forgot one important test - Testing that when we pass includeInactive = False to the BL it will also be passed to the DAL, as a result a user might change the behavior of the BL to always pass True and we would never find it in the test.



In a nut shell 100% code coverage using MSTest  (also other programs) means nothing!  if the input parameters are not being tested.



Another important issue to remember is that if you method doesn't throw an exception (swallow it) than you must add a test that this method will never throw exception - that will be a proactive test so that other developer wont decide to throw and exception in a running code (and actually changing the interface) without knowing the outcome)

Spring.net and cyclic references in context

Recently I have implemented a simple Passive View as a POC for one of the projects my team is going to be working on, part of this implementation I wanted to use IoC and AoP (at least put the infrastructure for it).

To make it simple I will describe a simple scenario (class definition)

public interface IPresenter
{
IView View { set; }
}
public interface IView
{
IPresenter Presenter{ set; }
}
public class ConcretePresenter : IPresenter
{
private IView m_View;
public ConcretePresenter(IView view)
{
m_View = view;
}
public IView View
{
set { m_View = value; }
}
}
public class ConcreteView : Page, IView
{
private IPresenter m_Presenter;
public ConcreteView()
{
}
public IPresenter Presenter
{
set { m_Presenter = value; }
}
}


The thing I wanted to achieve using spring is to set the ConcretePresenter for ConcreteView.



<object id="Presenter"
type="ConcretePresenter, Demo" singleton="false">
<
property name="View" ref="View" />
</
object>
<
object id="View"
type="ConcreteView, Demo" singleton="false">
<
property name="Presenter" ref="Presenter" />
</
object>


But as said I got StackOverFlow exception on runtime because of the cyclic references (actually the object dependencies),



I find this very weird as I would expect that creation of an object and assignments of property would be two different actions (in object life cycle perspective).



I searched Google and found a note on the forum that talk about that specific issue of cyclic references (link to article), the solution is to use IObjectPostProcessor class and create an object that will run after the object was initialized and link the objects.



You can download the code at this link.



Hope this will save you some time.