Skip to content

[MOB-17093] Part 1 - Unit tests with mockito and core 2.x#9

Merged
emdobrin merged 11 commits intoadobe:dev-v2.0.0from
emdobrin:MOB-17093
Dec 16, 2022
Merged

[MOB-17093] Part 1 - Unit tests with mockito and core 2.x#9
emdobrin merged 11 commits intoadobe:dev-v2.0.0from
emdobrin:MOB-17093

Conversation

@emdobrin
Copy link
Copy Markdown
Contributor

Description

I this PR we use mockito 4.5.1 and remove dependency on PowerMock for unit tests.

Fixes all the unit tests, except EdgeExtensionTest; that class requires significant changes due to the new way events are queued and listeners implementation, so I will capture those updates in the follow-up PR.

Related Issue

Motivation and Context

How Has This Been Tested?

Local unit test run

Screenshots (if appropriate):

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist:

  • I have signed the Adobe Open Source CLA.
  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

@emdobrin emdobrin requested a review from kevinlind December 13, 2022 03:00
/**
* Asserts the Log.error API received the {@code expectedLogs} number and exact log messages.
*/
private void assertErrorLogs(final MockedStatic<Log> mockLogService, @NotNull final List<String> expectedLogs) {
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Updated the helper methods to work with the new Log service. It currently does not have support for varargs, but that may not be required unless we plan to heavily assert on log messages.
Usage example in testProcessResponseOnSuccess_WhenErrorAndWarning_logsTheTwoEvents.

Event returnedEvent = returnedEvents.get(0);
assertNotNull(returnedEvent);
assertTrue(EVENT_TYPE_EDGE.equalsIgnoreCase(returnedEvent.getType()));
assertTrue(EVENT_SOURCE_EXTENSION_ERROR_RESPONSE_CONTENT.equalsIgnoreCase(returnedEvent.getSource()));
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

In core 2.x we can now do exact matches on event type value and source.

throws InterruptedException {
CountDownLatch getSharedStateCalled = new CountDownLatch(1);
CountDownLatch createSharedStateCalled = new CountDownLatch(1);
mockSharedStateCallback =
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The functionality changed in bootupIfNeeded, now looking for the event hub shared state. I added few additional tests for the various combinations of SharedStateResult values

state.bootupIfNeeded();
assertNull(properties.getLocationHint());

assertTrue(getSharedStateCalled.await(200, TimeUnit.MILLISECONDS));
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

What are your thoughts on using mockito to mock the shared state callback? I think that way you will not need to wait for the 200ms to and also make tests simpler.

@Mock
private EdgeSharedStateCallback mockSharedStateCallback;
...

when(mockSharedStateCallback.getSharedState(EdgeConstants.SharedState.HUB, any()))
			.thenReturn(new SharedStateResult(SharedStateStatus.SET, Collections.EMPTY_MAP));		

state = new EdgeState(mockHitQueue, properties, mockSharedStateCallback)
state.bootupIfNeeded();

ArgumentCaptor<..> stateCaptor = ArgumentCaptor.forClass(...);
verify(mockSharedStateCallback).createSharedState(stateCaptor.capture(), ..);
assertTrue(stateCaptor.getValue().isEmpty());

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Seems doable, in fact I am using a similar approach when mocking the extensionApi for shared state usages in the EdgeExtensionTests in PR #10. Since I stacked some of the changes on top of this, I can take this change in a separate PR if that's ok.

@emdobrin emdobrin merged commit 1adb563 into adobe:dev-v2.0.0 Dec 16, 2022
@emdobrin emdobrin deleted the MOB-17093 branch December 16, 2022 00:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants