2

Lets say I have the following code:
Mocked object class

public class SomeClass {
    private Foo someField;
    public SomeClass {
        someField = new Foo();
    }
    public Foo getSomeField { return someField; }
    public void getSomething() {}
    public boolean doSomething(Object object) {}
}

Next I have test suite

public class TestSuite {
    private ClassToTest classToTest;
    private SomeClass mock;

    @Before
    public void setUp() {
        classToTest = new ClassToTest();
        mock = EasyMock.createMock(SomeClass.class);
    }

    @Test
    public void testMethod() throws Exception { 
        mock.getSomething();
        EasyMock.replay(mock);

        classToTest.methodToTest(mock); //Where methodToTest accepts SomeClass and int

        EasyMock.verify(mock);
    }
}

And method which is being tested

public void methodToTest(SomeClass a) {
    //Logic here
    Foo b = a.getSomeField();
    b.do(); // <--- null pointer exception here because someField isn't initialized
    a.getSomething(); // <--- thing I want to test if it is being called, but can't due to exception prior to this line
    //Logic after
}

I am stuck.. So yea basically SomeClass isn't initialized like I wanted to. Is there any workaround? Or maybe any other frameworks which can do something like that?

randomUser56789
  • 936
  • 2
  • 13
  • 32

1 Answers1

2

Your methodToTest calls a.getSomeField(), but the setup part of your test doesn't expect that call. You want something like:

Foo foo = new Foo();
EasyMock.expect(mock.getSomeField()).andReturn(foo);

Or to stub the call:

Foo foo = new Foo();
EasyMock.expect(mock.getSomeField()).andStubReturn(foo);

(before your call to mock.getSomething()).

See this question for the differences between andReturn and andStubReturn.

Community
  • 1
  • 1
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • You are right I forgot to test it, too. But that is not the case. Problem is that you can see my constructor initializes `someField` and when I am making mock, that `someField` is null and as a result whole `methodToTest` breaks. So that means constructor isn't getting called? – randomUser56789 Feb 06 '13 at 20:04
  • @su234fk234hawuhsafklsfjs: What your *normal* implementation does is irrelevant. You're *mocking* that class, so its behaviour doesn't matter. So long as you mock the call you're making on to so that `getSomeField()` returns a non-null reference, it's fine. – Jon Skeet Feb 06 '13 at 20:08
  • But the problem is that `getSomeField();` does return null, because `SomeClass` initializes `someField` nothing else also there isn't a setter. I don't understand how can test pass if NPE is thrown by `b.do();` – randomUser56789 Feb 06 '13 at 21:07
  • @su234fk234hawuhsafklsfjs: It *won't* return null if you tell EasyMock to return something different though. That's the point of the code in my answer. Have you *tried* this solution yet? – Jon Skeet Feb 06 '13 at 21:18
  • Ah right, now I get it, thanks I will try it and inform you tomorrow – randomUser56789 Feb 06 '13 at 21:39
  • Works like a charm, I didn't fully understand it until I got working example. Thanks big time. – randomUser56789 Feb 07 '13 at 12:18