EasyMock 3.0, mocking class throws java.lang.IllegalStateException: no last call on a mock available

前端 未结 3 1836
自闭症患者
自闭症患者 2020-12-09 16:02

Running the following unit test throws the exception: java.lang.IllegalStateException: no last call on a mock available


import org.easymock.*;
import org.ju         


        
3条回答
  •  我在风中等你
    2020-12-09 16:36

    The reason for this exception is that Thread#isAlive() is a final method, but EasyMock does not support the mocking of final methods. So, the call to this method which appears inside EasyMock.expect(...) is not seen as a "call on a mock".

    To mock final methods you would need a different mocking tool, such as JMockit (which I develop):

    public void testMockingFinalMethod(@Mocked("isAlive") Thread mock)
    {
        new Expectations()
        {{
            mock.isAlive(); result = true;
        }};
    
        assertTrue(mock.isAlive());
    }
    

    The mocking API doesn't actually require that methods to be mocked are specified explicitly, in the general case. The Thread class is a tricky one, though.

提交回复
热议问题