Click here to Skip to main content
13,146,663 members (67,444 online)
Rate this:
Please Sign up or sign in to vote.
I'm a bit new to unit testing, most of my tests are fairly easy to do and just works brilliantly. However I'm trying to test this simple function and it fails me with the following error:
System.NullReferenceException: Object reference not set to an instance of an object.

What am I doing wrong? VS2010SP1
SOLUTION: Thanks to the solution below I realized my mistakes and changed one line to this and now all is bliss.

var s = Path.GetDirectoryName(new Uri(System.Reflection.Assembly.GetExecutingAssembly().Location).LocalPath)

Static method in class FolderSettings
public static String ProgramFolder
    get {
        var s = (Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location));
        return s;


/// <summary>
///A test for ProgramFolder
public void ProgramFolderTest()
    string actual;
    actual = FolderSettings.ProgramFolder;
    Assert.Inconclusive("Verify the correctness of this test method.");
Posted 22-Feb-12 4:37am
Erik Rude5.1K
Updated 22-Feb-12 4:58am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

When you're running the unit tests, the entry assembly will be the test hosting shell. That could be something useless like the nunit.exe assembly, or if you're using a binary test harness (e.g. Visual Studio itself), it will be null. (See the documentation for GetEntryAssembly[^].)

You should either use GetExecutingAssembly, if that's what you actually want to know in the general case (i.e. if someone links to your assembly, do you want the location of your assembly still?), or accept that something like this which is making assumptions about environment (i.e. that a particular assembly will be the primary entry point) is intrinsically not unit testable and not worry about it.

Considering it's a one line call to an external dependency (the Framework) which you can assume is well tested, I don't think you need a (pseudo-)unit test for this property anyway.
Erik Rude 22-Feb-12 10:50am
First of all Thanks!
OK, the thing is that I am of course using this feature in other 'units' that fail because I don't get the right path (that would be a null as you said). I had been looking at the GetExecutingAssembly, but abandoned it because I'm lazy and it gave me uripaths. I would assume in my program that the Executing assembly path was always the one I'm after.
BobJanova 22-Feb-12 11:06am
If you're relying on an external dependency like this for other tests, then they're not really unit tests in the strictest sense. See if you can make everything genuinely unit testable, and pass in a mocked-out path instead of actually asking the environment.

Some things (file access, database connection code etc) actually needs integration testing (i.e. needs to know stuff like this), but it's a good discipline to carefully isolate that and to provide carefully controlled mocks to unit tests of logic.
Erik Rude 23-Feb-12 3:58am
Thanks, it looks like I should do a bit of reading up on the theoretical background of Unit Testing.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web02 | 2.8.170915.1 | Last Updated 22 Feb 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100