Derik Whittaker

Syndication

News


Passing NULL into Overloaded Methods in unit tests…

Today I was adding some new logic to some older code and I noticed there was very little test coverage on the logic so I decided to add some.  As I was adding coverage I wanted to add some tests that proved out our Contract style bound checks.  However there was one slight problem.  The method(s) I wanted to add coverage to was overloaded as such:

public virtual IMessageTransporter GetTransporter( Destination destination )
{
    // The null transporter will allow for testing without hittng rhapsody or an engine
    if ( ConfigurationReader.UseNullTransporter ) { return new NullTransporter(); }

    Contracts.Requires( destination != null );
    
    // for now we only have one transporter for now.... will add more later
    return new HttpTransporter( destination.Endpoint );

}

and

public virtual IMessageTransporter GetTransporter( Endpoint endpoint )
{
    // The null transporter will allow for testing without hittng rhapsody or an engine
    if ( ConfigurationReader.UseNullTransporter ) { return new NullTransporter(); }

    Contracts.Requires( endpoint != null );
    
    // for now we only have one transporter for now.... will add more later
    return new HttpTransporter( endpoint  );

}

As you can see from the 2 methods above they are the same with ONLY the provided parameter being different.  When I started to write my tests I did this:

transporterFactory.GetTransporter( null );

But when I simply added NULL as the parameter the compiler provided me the following error

The call is ambiguous between the following methods or properties: ‘TransporterFactory.GetTransporter(Destination)' and TransporterFactory.GetTransporter(Endpoint)'    C:\PathHere\TransporterFactoryTests.cs    13    45    Common.Tests

Now if you think about it for even 2 seconds the above error makes perfect sense.  So the real question is how do you get around this….?  Simple you do the following.

transporterFactory.GetTransporter( (Destination)null );

By providing a type on your call the compiler now will know which overload you are attempting to call and the world will be a happier place.

Till next time,


Posted 01-21-2010 6:08 AM by Derik Whittaker
Filed under: ,

[Advertisement]

Comments

Matt Long wrote re: Passing NULL into Overloaded Methods in unit tests…
on 01-21-2010 9:58 AM

I might be missing something here, but why does this method even need to be overloaded?

The only meaningful object in either is the EndPoint object, only in the first example you access it via the Destination object.

Surely the calling code could just pass in Destination.EndPoint, rather than the Destination object.

What was the reasoning for needing the overload?

Moutasem Al-awa wrote re: Passing NULL into Overloaded Methods in unit tests…
on 01-21-2010 10:43 AM

its a nice notice actually

Maurício wrote re: Passing NULL into Overloaded Methods in unit tests…
on 01-21-2010 12:07 PM

Well, that surelly is an unexpected hack.

Seth Petry-Johnson wrote re: Passing NULL into Overloaded Methods in unit tests…
on 01-21-2010 12:21 PM

@Matt: I agree, without some additional information it's hard to tell if the overload is justified. And if there's a good reason for it, I'd suggest that one of the methods should delegate to the other, so that the actual logic isn't duplicated.

@Mauricio: I wouldn't call this a "hack" at all. The point is that when you have overloaded methods like this, you sometimes can't pass an unadorned "null" because it foils the compiler's overload resolution. The solution is to cast null as a reference type so that the compiler knows which method you want to pass a null to.

Steven wrote re: Passing NULL into Overloaded Methods in unit tests…
on 01-21-2010 8:00 PM

Could a generic function not be created here using a converter object to select the correct parameter for HttpTransporter. I would say the function has the wrong name it does not "get" yet create, it is a factory method.

mob wrote re: Passing NULL into Overloaded Methods in unit tests…
on 01-27-2010 6:54 PM

Wow, I've never run into this before but I just ran into it today and remembered this post. Thanks!

About The CodeBetter.Com Blog Network
CodeBetter.Com FAQ

Our Mission

Advertisers should contact Brendan

Subscribe
Google Reader or Homepage

del.icio.us CodeBetter.com Latest Items
Add to My Yahoo!
Subscribe with Bloglines
Subscribe in NewsGator Online
Subscribe with myFeedster
Add to My AOL
Furl CodeBetter.com Latest Items
Subscribe in Rojo

Member Projects
DimeCasts.Net - Derik Whittaker

Friends of Devlicio.us
Red-Gate Tools For SQL and .NET

NDepend

SlickEdit
 
SmartInspect .NET Logging
NGEDIT: ViEmu and Codekana
LiteAccounting.Com
DevExpress
Fixx
NHibernate Profiler
Unfuddle
Balsamiq Mockups
Scrumy
JetBrains - ReSharper
Umbraco
NServiceBus
RavenDb
Web Sequence Diagrams
Ducksboard<-- NEW Friend!

 



Site Copyright © 2007 CodeBetter.Com
Content Copyright Individual Bloggers

 

Community Server (Commercial Edition)