Is it possible to create a mock object that implements multiple interfaces with EasyMock?

后端 未结 6 2036
清酒与你
清酒与你 2020-12-30 18:35

Is it possible to create a mock object that implements several interfaces with EasyMock?

For example, interface Foo and interface Closeable

6条回答
  •  情深已故
    2020-12-30 19:17

    To the best of my knowledge, the only mocking tool for Java that has explicit support for mocking multiple interfaces is JMockit. (My inspiration for adding this feature came from Moq and Rhino Mocks, which are .NET tools.)

    An example (from the mockit.ExpectationsUsingMockedTest JUnit 4 test class):

    
    @Test
    public  void mockParameterWithTwoInterfaces(final M mock)
    {
       new Expectations()
       {
          {
             mock.doSomething(true); returns("");
             mock.run();
          }
       };
    
       assertEquals("", mock.doSomething(true));
       mock.run();
    }
    

    Dependency and Runnable are interfaces. The doSomething method belongs to the first, and run to the second.

提交回复
热议问题