kafka icon indicating copy to clipboard operation
kafka copied to clipboard

KAFKA-13036: Replace EasyMock and PowerMock with Mockito for RocksDBMetricsRecorderTest

Open divijvaidya opened this issue 2 years ago • 6 comments

Changes

  1. Migrate to Mockito
  2. Add more assertive checks using verify
  3. Minor indentation fixes

Testing

./gradlew streams:unitTest --tests RocksDBMetricsRecorderTest is successful

Notes to help the reviewers

  1. Mockito uses MockitoJUnitRunner.StrictStubs.class to assert that all stubs are verified.
  2. 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.
  3. PowerMock.reset is replaced by Mockito.reset
  4. Mockito does not have to distinguish between "niceMock" and a normal "mock". All mocks are "nicemocks".
  5. Mockito can mock static methods using mockStatic
  6. When verify(mockObj).functionName() is called, it is equivalent to verify(mockObj, times(1)).functionName() i.e. it validates that the function in invoked on mocked object exactly once.

Code coverage

Before

Screenshot 2022-08-02 at 18 14 26

After

Screenshot 2022-08-02 at 18 14 41

divijvaidya avatar Jul 29 '22 17:07 divijvaidya

@ableegoldman @cadonna please help review this test in the streams domain.

divijvaidya avatar Jul 30 '22 00:07 divijvaidya

Regarding "Notes to help the reviewers":

  1. 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 avatar Jul 30 '22 07:07 dplavcic

@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.

divijvaidya avatar Aug 01 '22 12:08 divijvaidya

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 avatar Aug 01 '22 21:08 dplavcic

@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!

divijvaidya avatar Aug 02 '22 16:08 divijvaidya

Looks good to me.

dplavcic avatar Aug 03 '22 18:08 dplavcic

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.

clolov avatar Aug 15 '22 18:08 clolov

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()

cadonna avatar Aug 30 '22 17:08 cadonna