chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male
Welcome to Typemock Community! Here you can ask and receive answers from other community members. If you liked or disliked an answer or thread: react with an up- or downvote.
0 votes
I'm running into a strange problem. I'm in the process of implementing TypeMock into my existing unit tests so i have a mixture of unit tests that use TypeMock objects and some that don't.

I'm trying to run the tests through TestDriven.Net in visual studio and i'm running into problems. when i run the tests one by one, they all pass. when i try to run a set of tests, some of them fail with the following error:

TestCase 'XXXX' failed: System.NullReferenceException : Object reference not set to an instance of an object.

The tests that fail are the ones that are not using TypeMock but the Object that is coming up null is of a type that is mocked in another unit test.

also, when i run the tests through the NUnit gui, they all pass.

Any ideas?
Michael.
asked by maird (800 points)

11 Answers

0 votes
Hi Michael,
The problem that you are stating is quite strange.
I guess that there are some dependancies between the tests.
To solve tell we need your help in investigating this.
a. If you stop the rocket and run all the tests, do you get this problem?
b. Is the mocked type a System type?
c. Do you always MockManager.Init() before each TypeMocked test?
d. Can you send me the TypeMock.out log (you can do this by setting: MockManager.LogFile=true in the first Test that is run), the file will be in the TestDriven.Net bin directory. [Showing the part from the failing test method name till the next test method is sufficiant]
answered by scott (32k points)
0 votes
Hi Michael,
The problem that you are stating is quite strange.
I guess that there are some dependancies between the tests.
To solve tell we need your help in investigating this.
a. If you stop the rocket and run all the tests, do you get this problem?


yep.

b. Is the mocked type a System type?


nope. it's one of my own classes.

c. Do you always MockManager.Init() before each TypeMocked test?


yep.

d. Can you send me the TypeMock.out log (you can do this by setting: MockManager.LogFile=true in the first Test that is run), the file will be in the TestDriven.Net bin directory. [Showing the part from the failing test method name till the next test method is sufficiant]


here's my TypeMock'ed method:
      [Test]
      public void TestAuthenticateNewCustomerMock()
      {
         MockManager.Init();

         Mock store = MockManager.Mock(typeof(Egrocer.BLL.Store));
         store.ExpectAndReturn("GetStore",null);

         Customer customer = new Customer();

         CustomerInfo customerDetails = customer.AuthenticateCustomer(m_BadPostalCode);

         Assert.IsNull(customerDetails);

         Mock store2 = MockManager.Mock(typeof(Egrocer.BLL.Store));
         store2.ExpectAndReturn("GetStore",new StoreInfo(StoreId,m_Name,m_Description,m_AddressId,m_MinimumOrder, m_Tax1Description, m_Tax2Description, m_PreferredAt, m_PreauthRate, m_RedeliveryCharge, m_CancellationRate, m_ElevatorFee, m_FreeDeliveries, m_SubstitutionDiscountRate, m_FreeLateChanges, m_FreeLateChangeWindow));

         customerDetails = customer.AuthenticateCustomer(m_GoodPostalCode);

         Assert.IsNotNull(customerDetails);

         Assert.AreEqual(Egrocer.BLL.Tests.DAL.Customer.NewCustomerId,customerDetails.CustomerId);

         MockManager.Verify();
      }


Any other tests in this fixture that call methods that call methods on "Store" get that System.NullReferenceException.

where should i sent the TypeMock.out file?

Michael.
answered by maird (800 points)
0 votes
Hi Michael
where should i sent the TypeMock.out file?.

I sent you a link offline, I will post the relevant pieces.

here's my TypeMock'ed method:
[Test]
public void TestAuthenticateNewCustomerMock()
{
    MockManager.Init();

    Mock store = MockManager.Mock(typeof(Egrocer.BLL.Store));
...
   CustomerInfo customerDetails = customer.AuthenticateCustomer(m_BadPostalCode);
   Assert.IsNull(customerDetails);

   Mock store2 = MockManager.Mock(typeof(Egrocer.BLL.Store));
...
   customerDetails = customer.AuthenticateCustomer(m_GoodPostalCode);

   Assert.IsNotNull(customerDetails);
   MockManager.Verify();
}

Any other tests in this fixture that call methods that call methods on "Store" get that System.NullReferenceException.


I inderstand from the code that somewhere in CustomerInfo.AuthenticateCustomer you create a new Store - is this true?
Or is Store a Singleton?
If it is a singleton you should use MockAll (See the topic in Testing Pattens Forum)

Another scenario that could produce this is if you mock a method that sets up static fields.

If you debug the tests, are you getting the NullReferenceExecption in TypeMock.dll code or in your own code?
answered by scott (32k points)
0 votes
Scott,

After i got your message, i did a little more digging and here's what i turned up. The lines where i get the System.NullReferenceException are actually calls to log4net. within my class i have my logger defined as:

private static readonly log4net.ILog Log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

is this causing a problem because it's static?

Michael.

PS: i'm posting this here because whenever i try to reply to your email i get The user does not accept email in non-Western (non-Latin) character sets.
answered by maird (800 points)
0 votes
Hi Mike,

whenever i try to reply to your email i get an error that says you don't accept non-western character sets...wierd...

I will talk to our admin to try and fix it :-)

The lines where i get the System.NullReferenceException are actually calls to log4net

About the NullReferenceException, it is very possible that you get this error from log4net, Probraby because it is not setup correctly.

You will have to either mock out the Logger before each test, this can be done easily by:
MockObject iLogMockControl = MockManager.MockObject(typeof(ILog));
// set Always expectations...for all methods then...
ILog mockedLogger = (Ilog)iLogMockControl.Object; 
// You can also just make your own class called FakeLog that implements 
// ILog and make sure that LogManager.GetLogger always returns this 
// logger using typemock

Mock logManagerMock = MockManager.Mock(typeof(LogManager));
logManagerMock.AlwaysReturn("GetLogger",logManagerMock);


This should be in a utility class and called before each test,
or
you can setup log4net to do nothing before each test

I hope that this solves you problem
answered by scott (32k points)
0 votes
Scott,

the thing is, when i comment out the test that uses TypeMock, everything
runs fine. i'm pretty sure that log4net is setup properly because when
i'm not using TypeMock, it properly outputs log entries.

also, if i take out the "static" keyword, all the tests run fine. i guess i
could do that in all my classes but i'd really rather have it as a
static member...

Michael.
answered by maird (800 points)
0 votes
Hi Michael,

the thing is, when i comment out the test that uses TypeMock, everything runs fine. i'm pretty sure that log4net is setup properly because when i'm not using TypeMock, it properly outputs log entries.

This might be because of TypeMock, but it can also be because of the actual test.

also, if i take out the "static" keyword, all the tests run fine.

Where are you removing the static from?
answered by scott (32k points)
0 votes
i'm taking the static keyword out of this line:

private static readonly log4net.ILog Log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
answered by maird (800 points)
0 votes
Hi Michael,

i'm taking the static keyword out of this line:

private static readonly log4net.ILog Log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);


I think that we are getting to the root of the problem, When you mock a type with TypeMock the default is to mock all static initilizator and the constructors too. If this is the case the Log will not be initialized (As it is initialized in the static constructor).
In order to mock a class LEAVING the static fields initialized do the following:
Mock store = MockManager.Mock(typeof(Egrocer.BLL.Store),Constructor.StaticNotMocked); 
answered by scott (32k points)
0 votes
Thank you! that did the trick.
answered by maird (800 points)
...