kafka
kafka copied to clipboard
KAFKA-13036: Replace EasyMock and PowerMock with Mockito for RocksDBMetricsRecorderTest
Changes
- Migrate to Mockito
- Add more assertive checks using
verify
- Minor indentation fixes
Testing
./gradlew streams:unitTest --tests RocksDBMetricsRecorderTest
is successful
Notes to help the reviewers
- Mockito uses
MockitoJUnitRunner.StrictStubs.class
to assert that all stubs are verified. - Mockito does not have the concept of "register-activate" represented by "when - replay" in EasyMock. Hence, you only need to use "when" and that is all. No "replay" is required.
-
PowerMock.reset
is replaced byMockito.reset
- Mockito does not have to distinguish between "niceMock" and a normal "mock". All mocks are "nicemocks".
- Mockito can mock static methods using
mockStatic
- When
verify(mockObj).functionName()
is called, it is equivalent toverify(mockObj, times(1)).functionName()
i.e. it validates that the function in invoked on mocked object exactly once.
Code coverage
Before
After
@ableegoldman @cadonna please help review this test in the streams
domain.
Regarding "Notes to help the reviewers":
- Mockito does not have
verifyAll()
hence each mocked function call needs to be verified individually.
This depends on the strictness
level used while creating mocks.
Please check:
- https://javadoc.io/doc/org.mockito/mockito-core/latest/org/mockito/quality/Strictness.html#STRICT_STUBS
- https://javadoc.io/doc/org.mockito/mockito-core/latest/org/mockito/Mockito.html#mockito_strictness
Also, "Mockito vs EasyMock" guide encapsulates differences nicely
- https://github.com/mockito/mockito/wiki/Mockito-vs-EasyMock
@dplavcic Thank you for your review and enlightening me on better usage of Mockito. I have addressed your comments. Please take another look when you get a chance.
Just one more question. Do you maybe have jacoco
test coverage reports before and after these changes? I think jacoco
reports would be useful to confirm we are still covering (at least) the same amount of production code..
@dplavcic I have added the test coverage report to the description of this ticket. This revision is ready for your review now. Thanks again for your patience and spending time in reviewing this code. I appreciate it very much!
Looks good to me.
Heya @cadonna and @dplavcic, Divij is taking some time off, so I will aim to provide an update to the comments and the pull request in the next couple of days.
The test failures are unrelated:
Build / JDK 8 and Scala 2.12 / org.apache.kafka.controller.QuorumControllerTest.testDelayedConfigurationOperations()
Build / JDK 11 and Scala 2.13 / kafka.api.TransactionsTest.testFailureToFenceEpoch(String).quorum=kraft
Build / JDK 17 and Scala 2.13 / org.apache.kafka.controller.QuorumControllerTest.testDelayedConfigurationOperations()