Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

KAFKA-13133 Replace EasyMock and PowerMock with Mockito for AbstractHerderTest #11137

Closed
wants to merge 5 commits into from

Conversation

wycccccc
Copy link
Contributor

Development of EasyMock and PowerMock has stagnated while Mockito continues to be actively developed. With the new Java cadence, it's a problem to depend on libraries that do bytecode generation and are not actively maintained. In addition, Mockito is also easier to use.KAFKA-7438

Committer Checklist (excluded from commit message)

  • Verify design and implementation
  • Verify test coverage and CI build status
  • Verify documentation (including upgrade notes)

EasyMock.expect(herder.rawConfig(connector)).andReturn(null);
EasyMock.expect(configStore.snapshot()).andReturn(SNAPSHOT);
replayAll();
// AbstractHerder herder = partialMockBuilder(AbstractHerder.class)
Copy link
Contributor

@cmccabe cmccabe Aug 4, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it seems like you intended to remove this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I have removd.thx remind.

remove redundant code
@ijuma
Copy link
Contributor

ijuma commented Sep 6, 2021

@kkonstantine @rhauch It would be great to have a review from one of you two so that we can get this merged.

final private Plugins plugins = mock(Plugins.class);
final private ClassLoader classLoader = mock(ClassLoader.class);
final private ConfigBackingStore configStore = mock(ConfigBackingStore.class);
final private StatusBackingStore statusStore = mock(StatusBackingStore.class);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: we usually write private final.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for reminding,it has been modified and test also remove in build.gradle.

@ijuma
Copy link
Contributor

ijuma commented Sep 6, 2021

We should also remove this test from https://github.com/apache/kafka/blob/trunk/build.gradle#L381 before we merge this PR.

Copy link
Member

@mimaison mimaison left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR, it's a nice cleanup!
I've left a few comments with further cleanup suggestions.

new TaskStatus(taskId, AbstractStatus.State.UNASSIGNED, workerId, generation)));

replayAll();
ConnectorStateInfo csi = herder.connectorStatus(connector);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we assert anything on csi? Otherwise, this test does not assert anything.

Do we actually need this test? It seems the test below is basically the same but with asserts?

.defaultAnswer(CALLS_REAL_METHODS));

when(herder.generation()).thenReturn(generation);
when(herder.rawConfig(connector)).thenReturn(null);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we actually have to mock generation() and rawConfig() for this test? Looking at connector(), it looks like it only relies on the snapshot.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right @mimaison here and in your other comments about extra stubbing. When we run the test with @RunWith(MockitoJUnitRunner.class) which implicitly verifies that each mock is being invoked, the test fails.

Unnecessary stubbings detected in test class: AbstractHerderTest
Clean & maintainable test code requires zero unnecessary code.
Following stubbings are unnecessary (click to navigate to relevant line of code):
  1. -> at org.apache.kafka.connect.runtime.AbstractHerderTest.createConfigValidationHerder(AbstractHerderTest.java:880)
  2. -> at org.apache.kafka.connect.runtime.AbstractHerderTest.testConnectors(AbstractHerderTest.java:165)
  3. -> at org.apache.kafka.connect.runtime.AbstractHerderTest.testConnectors(AbstractHerderTest.java:166)
  4. -> at org.apache.kafka.connect.runtime.AbstractHerderTest.testBuildRestartPlanForUnknownConnector(AbstractHerderTest.java:255)
  5. -> at org.apache.kafka.connect.runtime.AbstractHerderTest.testConnectorStatus(AbstractHerderTest.java:179)
  6. -> at org.apache.kafka.connect.runtime.AbstractHerderTest.connectorStatus(AbstractHerderTest.java:198)
  7. -> at org.apache.kafka.connect.runtime.AbstractHerderTest.connectorStatus(AbstractHerderTest.java:207)
  8. -> at org.apache.kafka.connect.runtime.AbstractHerderTest.testBuildRestartPlanForConnectorAndTasks(AbstractHerderTest.java:278)
  9. -> at org.apache.kafka.connect.runtime.AbstractHerderTest.testBuildRestartPlanForConnectorAndTasks(AbstractHerderTest.java:286)
  10. -> at org.apache.kafka.connect.runtime.AbstractHerderTest.testBuildRestartPlanForNoRestart(AbstractHerderTest.java:314)
  11. -> at org.apache.kafka.connect.runtime.AbstractHerderTest.testBuildRestartPlanForNoRestart(AbstractHerderTest.java:322)
Please remove unnecessary stubbings or use 'lenient' strictness. More info: javadoc for UnnecessaryStubbingException class.
org.mockito.exceptions.misusing.UnnecessaryStubbingException: 
Unnecessary stubbings detected in test class: AbstractHerderTest
Clean & maintainable test code requires zero unnecessary code.
Following stubbings are unnecessary (click to navigate to relevant line of code):
  1. -> at org.apache.kafka.connect.runtime.AbstractHerderTest.createConfigValidationHerder(AbstractHerderTest.java:880)
  2. -> at org.apache.kafka.connect.runtime.AbstractHerderTest.testConnectors(AbstractHerderTest.java:165)
  3. -> at org.apache.kafka.connect.runtime.AbstractHerderTest.testConnectors(AbstractHerderTest.java:166)
  4. -> at org.apache.kafka.connect.runtime.AbstractHerderTest.testBuildRestartPlanForUnknownConnector(AbstractHerderTest.java:255)
  5. -> at org.apache.kafka.connect.runtime.AbstractHerderTest.testConnectorStatus(AbstractHerderTest.java:179)
  6. -> at org.apache.kafka.connect.runtime.AbstractHerderTest.connectorStatus(AbstractHerderTest.java:198)
  7. -> at org.apache.kafka.connect.runtime.AbstractHerderTest.connectorStatus(AbstractHerderTest.java:207)
  8. -> at org.apache.kafka.connect.runtime.AbstractHerderTest.testBuildRestartPlanForConnectorAndTasks(AbstractHerderTest.java:278)
  9. -> at org.apache.kafka.connect.runtime.AbstractHerderTest.testBuildRestartPlanForConnectorAndTasks(AbstractHerderTest.java:286)
  10. -> at org.apache.kafka.connect.runtime.AbstractHerderTest.testBuildRestartPlanForNoRestart(AbstractHerderTest.java:314)
  11. -> at org.apache.kafka.connect.runtime.AbstractHerderTest.testBuildRestartPlanForNoRestart(AbstractHerderTest.java:322)
Please remove unnecessary stubbings or use 'lenient' strictness. More info: javadoc for UnnecessaryStubbingException class.
	at app//org.mockito.internal.runners.StrictRunner.run(StrictRunner.java:53)
	at app//org.mockito.junit.MockitoJUnitRunner.run(MockitoJUnitRunner.java:163)
	at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecutor.runTestClass(JUnitTestClassExecutor.java:110)
	at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecutor.execute(JUnitTestClassExecutor.java:58)
	at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecutor.execute(JUnitTestClassExecutor.java:38)
	at org.gradle.api.internal.tasks.testing.junit.AbstractJUnitTestClassProcessor.processTestClass(AbstractJUnitTestClassProcessor.java:62)
	at org.gradle.api.internal.tasks.testing.SuiteTestClassProcessor.processTestClass(SuiteTestClassProcessor.java:51)
	at java.base@11.0.14/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base@11.0.14/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at java.base@11.0.14/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base@11.0.14/java.lang.reflect.Method.invoke(Method.java:566)
	at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:36)
	at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
	at org.gradle.internal.dispatch.ContextClassLoaderDispatch.dispatch(ContextClassLoaderDispatch.java:33)
	at org.gradle.internal.dispatch.ProxyDispatchAdapter$DispatchingInvocationHandler.invoke(ProxyDispatchAdapter.java:94)
	at com.sun.proxy.$Proxy2.processTestClass(Unknown Source)
	at org.gradle.api.internal.tasks.testing.worker.TestWorker$2.run(TestWorker.java:176)
	at org.gradle.api.internal.tasks.testing.worker.TestWorker.executeAndMaintainThreadName(TestWorker.java:129)
	at org.gradle.api.internal.tasks.testing.worker.TestWorker.execute(TestWorker.java:100)
	at org.gradle.api.internal.tasks.testing.worker.TestWorker.execute(TestWorker.java:60)
	at org.gradle.process.internal.worker.child.ActionExecutionWorker.execute(ActionExecutionWorker.java:56)
	at org.gradle.process.internal.worker.child.SystemApplicationClassLoaderWorker.call(SystemApplicationClassLoaderWorker.java:133)
	at org.gradle.process.internal.worker.child.SystemApplicationClassLoaderWorker.call(SystemApplicationClassLoaderWorker.java:71)
	at app//worker.org.gradle.process.internal.worker.GradleWorkerMain.run(GradleWorkerMain.java:69)
	at app//worker.org.gradle.process.internal.worker.GradleWorkerMain.main(GradleWorkerMain.java:74)

I am submitting a new PR containing commits from this PR. I will ensure that your comments are addressed there.

.useConstructor(worker, workerId, kafkaClusterId, statusStore, configStore, noneConnectorClientConfigOverridePolicy)
.defaultAnswer(CALLS_REAL_METHODS));

when(herder.generation()).thenReturn(generation);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need to mock generation() in this test.


EasyMock.expect(herder.generation()).andStubReturn(generation);
EasyMock.expect(herder.rawConfig(connector)).andReturn(null);
when(herder.generation()).thenReturn(generation);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same, I don't think it's needed in this test. I'm not going to comment on the other tests but let's remove the mocks we don't need.

EasyMock.expect(worker.getPlugins()).andStubReturn(plugins);
when(statusStore.getAll(connector))
.thenReturn(taskStatuses);
when(worker.getPlugins()).thenReturn(plugins);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this mock is needed. Same below.


verifyAll();
}

@Test()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can drop the parenthesis here. Same below

EasyMock.expect(plugins.transformations()).andReturn(transformations).times(2);

replayAll();
when(plugins.transformations()).thenReturn(transformations);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about inlining transformations and having something like:

when(plugins.transformations()).thenReturn(Collections.singleton(transformationPluginDesc()));

public void testConfigValidationTransformsExtendResults() throws Throwable {
AbstractHerder herder = createConfigValidationHerder(TestSourceConnector.class, noneConnectorClientConfigOverridePolicy);
public void testConfigValidationTransformsExtendResults() {
final Class<? extends Connector> connectorClass = TestSourceConnector.class;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have this variable in many tests, should instead have a field in this class?


// 2 transform aliases defined -> 2 plugin lookups
Set<PluginDesc<Transformation<?>>> transformations = new HashSet<>();
transformations.add(transformationPluginDesc());
EasyMock.expect(plugins.transformations()).andReturn(transformations).times(1);
when(plugins.transformations()).thenReturn(transformations);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as mentioned above, because these Set instances only have 1 element, we can maybe inline these variables. WDYT?

@ijuma
Copy link
Contributor

ijuma commented Feb 5, 2022

@wycccccc are you planning to address the comments?

@ijuma
Copy link
Contributor

ijuma commented Aug 10, 2022

Closing in favor of #12473.

@ijuma ijuma closed this Aug 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
6 participants