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
May i know is there any wrong in the coding below? Null reference exception when trying to invoke the doSomething() function under call() in test. In testing, will the class1 and class2 be mocked?

for eg,

public class ClassCalled
{
public ClassManager cm;

public ClassCalled(ClassManager cm)
{
cm = cm;
}

public void call
{
cm.class1Instance.doSomething();
}
}
public class ClassManager
{
private Class1 c1;
private Class1 c2;

public ClassManager()
{
c1 = new Class1;
c2 = new Class2;
}

public Class1 class1Instance
{
return c1;
}
}

public class Class1
{
public Class1()
{

}

public void doSomething()
{
......................
}
}

public class Class2
{
public Class2()
{

}
}


Testing
[Test]
public void test()
{
MockManager.Init();
Mock mockClassManager = MockManager.Mock(typeof(MockManager));
Mock mockClass1 = MockManager.Mock(typeof(Class1));
Mock mockClass2 = MockManager.Mock(typeof(Class2));

ClassManager cm = new ClassManager();
ClassCalled cc = new ClassCalled(cm);

cm.class1Instance.ExpectCall("doSomething");

cc.call();
}
asked by clteh9 (5.3k points)

5 Answers

0 votes
Hi

OK
We have here few issues.
First you have a bug in your code.
In the constructor of ClassCalled I assume you mean

this.cm = cm;

instead of
cm = cm;


Second In the test method I assume you mean to write
Mock mockClassManager = MockManager.Mock(typeof(ClassManager));
mockClass1.ExpectCall("doSomething");

instead of
Mock mockClassManager = MockManager.Mock(typeof(MockManager));
cm.class1Instance.ExpectCall("doSomething");

You don't want to mock TypeMock.NET classes do you? :wink:

So now our test method look like this:
        public void test()
        {
            MockManager.Init();
            Mock mockClassManager = MockManager.Mock(typeof(ClassManager));
            Mock mockClass1 = MockManager.Mock(typeof(Class1));
            Mock mockClass2 = MockManager.Mock(typeof(Class2));

            ClassManager cm = new ClassManager();
            ClassCalled cc = new ClassCalled(cm);

            mockClass1.ExpectCall("doSomething");

            cc.call();
        }


But we still get that evil Null reference exception :evil: that we all learned to hate.
What happened here is that when you mock the ClassManager class its constructor is mocked by default so c1 and c2 are not built
and that's was causing the exception.
The solution in this case is to use the second parameter in MockManager.Mock method like this:
Mock mockClassManager = MockManager.Mock(typeof(ClassManager), Constructor.NotMocked);

This will cause your code calling to the real constructor.
answered by ohad (35.4k points)
0 votes
Yup, thank you. I managed to do it yesterday already. thanks
answered by clteh9 (5.3k points)
0 votes
i got another question. using same example, but modify it a bit.

public class ClassCalled
{
public ClassManager cm;

public ClassCalled(ClassManager cm)
{
this.cm = cm;
}

public void call
{
int a = this.cm.class2Instance.a[1]; <------null reference, how can i set this values? is it need to set mock class as constructor.NotMocked and call the read function in class 2?
this.cm.class1Instance.doSomething();
}
}
public class ClassManager
{
private Class1 c1;
private Class1 c2;

public ClassManager()
{
c1 = new Class1;
c2 = new Class2;
}

public Class1 class1Instance
{
return c1;
}

public Class2 class2Instance
{
return c2;
}

}

public class Class1
{
public Class1()
{

}

public void doSomething()
{
......................
}
}

public class Class2
{

private int[] a;
public Class2()
{

}

public void read()
{
a = new int[3]{1, 2, 3};
}
public int[] aInst
{
get{ return a;}
}
}


Testing
[Test]
public void test()
{
MockManager.Init();
Mock mockClassManager = MockManager.Mock(typeof(MockManager), Constructor.NotMocked);
Mock mockClass1 = MockManager.Mock(typeof(Class1));
Mock mockClass2 = MockManager.Mock(typeof(Class2));

ClassManager cm = new ClassManager();
ClassCalled cc = new ClassCalled(cm);

mockClass1.ExpectCall("doSomething");

cc.call();
}
answered by clteh9 (5.3k points)
0 votes
sorry, it is

public int[] a
{
get{ return a;}
}

instead of

public int[] aInst
{
get{ return a;}
}
answered by clteh9 (5.3k points)
0 votes
i try to add in 1 line in test(), but still got same exception, System.NullReferenceException.

MockClass2.ExpectGetAndAlwaysThrow("a",new System.NullReferenceException());
answered by clteh9 (5.3k points)
...