Major difference between: Mockito and JMockIt

前端 未结 2 1678
花落未央
花落未央 2021-02-08 09:51

This is what I found from my initial attempts to use JMockIt. I must admit that I found the JMockIt documentation very terse for what it provides and hence I might have missed s

2条回答
  •  渐次进展
    2021-02-08 10:45

    In fact, all mocking APIs mock or stub out every method in the mocked type, by default. I think you confused mock(type) ("full" mocking) with spy(obj) (partial mocking).

    JMockit does all that, with a simple API in every case. It's all described, with examples, in the JMockit Tutorial. For proof, you can see the sample test suites (there are many more that have been removed from newer releases of the toolkit, but can still be found in the old zip files), or the many JMockit integration tests (over one thousand currently).

    The equivalent to Mockito's spy is "dynamic partial mocking" in JMockit. Simply pass the instances you want to partially mock as arguments to the Expectations constructor. If no expectations are recorded, the real code will be executed when the code under test is exercised. BTW, Mockito has a serious problem here (which JMockit doesn't), because it always executes the real code, even when it's called inside when(...) or verify(...); because of this, people have to use doReturn(...).when(...) to avoid surprises on spied objects.

    Regarding verification of invocations, the JMockit Verifications API is considerably more capable than any other. For example:

    new VerificationsInOrder() {{
        // preceding invocations, if any
        mockDatabase.save((Article) withNotNull()); times = 2;
        // later invocations, if any
    }};
    

提交回复
热议问题