Skip to content

Add null check to workaround NPE in unit tests with Mockito/PowerMock - #14006

Merged
lhotari merged 1 commit into
apache:masterfrom
lhotari:lh-mock-NPE-workaround
Jan 28, 2022
Merged

Add null check to workaround NPE in unit tests with Mockito/PowerMock#14006
lhotari merged 1 commit into
apache:masterfrom
lhotari:lh-mock-NPE-workaround

Conversation

@lhotari

@lhotari lhotari commented Jan 28, 2022

Copy link
Copy Markdown
Member

Motivation

Workaround for a NPE issue that happens with Mockito/PowerMock:

org.mockito.exceptions.base.MockitoException: Unable to create mock instance of type 'ServerCnx'
	at org.apache.pulsar.broker.BrokerTestUtil.spyWithClassAndConstructorArgs(BrokerTestUtil.java:43)
	at 
	... 
	... 
	... 
Caused by: java.lang.reflect.InvocationTargetException
	at org.mockito.internal.creation.bytebuddy.ByteBuddyMockMaker.createMock(ByteBuddyMockMaker.java:43)
	at org.powermock.api.mockito.mockmaker.PowerMockMaker.createMock(PowerMockMaker.java:41)
	at org.mockito.internal.util.MockUtil.createMock(MockUtil.java:53)
	at org.mockito.internal.MockitoCore.mock(MockitoCore.java:84)
	at org.mockito.Mockito.mock(Mockito.java:1964)
	at org.apache.pulsar.broker.BrokerTestUtil.spyWithClassAndConstructorArgs(BrokerTestUtil.java:43)
	at org.apache.pulsar.broker.service.PersistentTopicTest.setup(PersistentTopicTest.java:211)
	at org.apache.pulsar.broker.service.persistent.PersistentTopicStreamingDispatcherTest.setup(PersistentTopicStreamingDispatcherTest.java:34)
	at jdk.internal.reflect.GeneratedMethodAccessor332.invoke(Unknown Source)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:566)
	... 34 more
Caused by: java.lang.NullPointerException
	at org.apache.pulsar.broker.service.ServerCnx.<init>(ServerCnx.java:234)
	at org.apache.pulsar.broker.service.ServerCnx.<init>(ServerCnx.java:230)
	at org.apache.pulsar.broker.service.ServerCnx$MockitoMock$985617261.<init>(Unknown Source)
	... 55 more

Modification

Add a null check to prevent the NPE.

@github-actions

Copy link
Copy Markdown

@lhotari:Thanks for your contribution. For this PR, do we need to update docs?
(The PR template contains info about doc, which helps others know more about the changes. Can you provide doc-related info in this and future PR descriptions? Thanks)

@lhotari lhotari added the doc-not-needed Your PR changes do not impact docs label Jan 28, 2022
@github-actions

Copy link
Copy Markdown

@lhotari:Thanks for providing doc info!

@michaeljmarshall

Copy link
Copy Markdown
Member

I believe this would also fix #13808.

@michaeljmarshall michaeljmarshall left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM. If/when we remove the mocking that is responsible for these flaky tests, it'd be good to revert this change.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/broker cherry-picked/branch-2.9 Archived: 2.9 is end of life doc-not-needed Your PR changes do not impact docs release/2.9.2 type/flaky-tests

Projects

None yet

5 participants