[MOB-17093] Part 1 - Unit tests with mockito and core 2.x#9
[MOB-17093] Part 1 - Unit tests with mockito and core 2.x#9emdobrin merged 11 commits intoadobe:dev-v2.0.0from
Conversation
| /** | ||
| * 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) { |
There was a problem hiding this comment.
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())); |
There was a problem hiding this comment.
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 = |
There was a problem hiding this comment.
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)); |
There was a problem hiding this comment.
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());
There was a problem hiding this comment.
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.
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
Checklist: