How do Java mocking frameworks work?

前端 未结 5 455
攒了一身酷
攒了一身酷 2021-02-03 23:10

This is NOT a question about which is the best framework, etc.

I have never used a mocking framework and I\'m a bit puzzled by the idea. How does it know how to create t

5条回答
  •  北海茫月
    2021-02-04 00:12

    EasyMock (http://easymock.org/) is the mocking library that I have used the most. (There are others: jMock, Mockito, etc.)

    Most of these create an object at runtime, that you can control. The basic flow is:

    1. Create a mock
    2. Specify how it should act by telling the mock framework what calls to expect. (This varies based on the framework.)
    3. Use the mock in your test as if it were the real thing
    4. Some frameworks allow (or require) that you "verify" that the mock was used correctly.

提交回复
热议问题