This site requires JavaScript, please enable it in your browser!
Greenfoot back
Xevinaly
Xevinaly wrote ...

2019/3/17

Using EasyMock with greenfoot.Greenfoot

Xevinaly Xevinaly

2019/3/17

#
I am doing TDD with Greenfoot and trying to mock the Greenfoot class. I'm using the EasyMock mocking utility with JUnitTest. Below is my code:
@Test
    public void testPlayerSelection(){
        Greenfoot gfoot = EasyMock.mock(Greenfoot.class);
        EasyMock.expect(gfoot.ask("How many players")).andReturn("3");
        EasyMock.replay(gfoot);
    }
When I run the code it says that there is a NullPointerException in Greenfoot.ask(), is there something I am missing about properly mocking the Greenfoot class?
nccb nccb

2019/3/17

#
I'm not very familiar with EasyMock, but the Greenfoot class is not standalone; it carries internal state that indirectly allows it to communicate back to the GUI. So I don't think you're likely to be able to mock it directly. Your best bet might be to wrap it in another class that you can then mock instead.
Xevinaly Xevinaly

2019/3/21

#
Thank you for the advice. I created an identical wrapper and it worked perfectly for mocking :)
You need to login to post a reply.