System.NullReferenceException thrown when ExpectedExceptionAttribute is used in a static class

Bug #1223294 reported by Dennis Fantoni
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
NUnit Framework
Fix Released
Critical
Charlie Poole
NUnitLite
Fix Released
Critical
Charlie Poole

Bug Description

I have this simple unit test :

      [Test]
        [ExpectedException("System.ArgumentOutOfRangeException")]
        public static void CreateQueryEndNegative()
        {
            Console.WriteLine("QQEN");
            throw new ArgumentOutOfRangeException();
        }

When I run the test program (no args) i get :

1) CreateQueryEndNegative (TightDbCSharpTest.QueryTests.CreateQueryEndNegative)
   System.NullReferenceException : Object reference not set to an instance of an
 object.
   at System.Object.GetType()
   at NUnit.Framework.Internal.Commands.ExpectedExceptionCommand.ProcessExceptio
n(Exception exception, TestExecutionContext context)
   at NUnit.Framework.Internal.Commands.ExpectedExceptionCommand.Execute(TestExe
cutionContext context)
   at NUnit.Framework.Internal.Commands.SetUpTearDownCommand.Execute(TestExecuti
onContext context)

i cannot confirm if the error will still be there in a small test project , I will investigate further - but as it is now, the unit test method does not call any of my code, and still I get the rather weird error from inside the unit testing framework (would have expected the test to pass silently)

My project has 200+ unit tests and almost all of them succeed, also other that use expectedexception - but perhaps not others that use [ExpectedException("System.ArgumentOutOfRangeException")]

I will comment further on this bug report when I have created a test project away from my main project.

Even if I did something wrong with setup or whatever, the error is still a bit confusing.

The same test in the same source passes fine with Nuint (reference the same sourcefile from a NUnit using project)

I use NUnitLite 1.0.0 .net 4.5 64 bit windows 7, running 64 bit assembly CLR runtime 4.0.30319.1852, building with Visual Studio 2012 sp3

the same bug appears also on the same machine running mono , 32 bit (also if I compile in mono (using xamarin) and run in .net or the other way around)

The NUnitlite installation I use was gotten via NuGet, i simply reference a binary from my test project.

Related branches

Revision history for this message
Dennis Fantoni (dennis-4) wrote :

I have now created a small solution that does the same - and it does NOT fail the same way - in fact it works fine. I Will try to investigate what the difference is between the two, and report back.

If anyone have an idea what might provoke this kind of error, please let me know .

Revision history for this message
Dennis Fantoni (dennis-4) wrote :

I have now succeded in reducing my large project to a very small project that fails. I have attached a very small solution that provokes the bug (or perhaps show how I use NUnitLite in the wrong way - I might've made an obvious mistake, please let me know).

The entire solution consists of less than 10 active lines of code, and some boilerplate comments as well as various annotations. It should be easy to work with.

The bug shows here on windows 7, Visual Studio 2012 sp3, NUnitLite beta 1.0.0 2 (the one from aug.18 2013). .net 4.5 Debug/AnyCpu You will have to add a reference to NUnitLite before running the solution.

If I run the program with mono 3.10 , built inside xamarin, i get the same error. (still on windows 7).

Please take a look at attached solution, and let me know what I did wrong...

Regards,

Dennis

Revision history for this message
Charlie Poole (charlie.poole) wrote :

This turns out to be due to the test fixture being static. ExpectedExceptionCommand, which processes tests that have an expected exception makes the assumption that an instance of the test fixture class has been created and tries to use it. For static classes, no instance is created, of course.

Changed in nunitlite:
status: New → Triaged
importance: Undecided → Critical
assignee: nobody → Charlie Poole (charlie.poole)
milestone: none → 1.0
summary: - System.NullReferenceException thrown when expected ArgumentOutOfRange
- exception is caught
+ System.NullReferenceException thrown when ExpectedExceptionAttribute is
+ used in a static class
Changed in nunit-3.0:
status: New → Triaged
importance: Undecided → Critical
assignee: nobody → Charlie Poole (charlie.poole)
milestone: none → 2.9.6
Changed in nunitlite:
status: Triaged → Fix Committed
Changed in nunitlite:
status: Fix Committed → Fix Released
Changed in nunit-3.0:
status: Triaged → Fix Committed
Changed in nunit-3.0:
status: Fix Committed → Fix Released
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.