Skip to content
This repository has been archived by the owner on Nov 16, 2023. It is now read-only.

Commit

Permalink
chore: Workaround for mockito #2860 (#199)
Browse files Browse the repository at this point in the history
#### Details

Due to mockito/mockito#2860, Dependbot has
been unable to update to any version of `mockito-inline` since version
4.6.1. The failed attempts are as follows:

Version | Failed PR
--- | ---
4.7.0 | #169 
4.8.0 | #174
4.8.1 | #176
4.9.0 | #182
4.11.0 | #198 

In response to mockito/mockito#2860, a
suggestion was made to use an `ArgumentCaptor` to capture the arguments
and the `RETURNS_SELF` parameter for the builder. This PR makes that
change, so we're now capturing arguments using the construct that is
intended for that purpose. The mockito team has not committed to fixing
this issue and may even be leaning toward not fixing it, since a more
officially supported option exists to do what we're doing.

I've applied these changes locally to #198 and confirmed that the tests
complete as expected.

##### Motivation

Allow newer versions of mockito

##### Context

<!-- Are there any parts that you've intentionally left out-of-scope for
a later PR to handle? -->

<!-- Were there any alternative approaches you considered? What
tradeoffs did you consider? -->

#### Pull request checklist
<!-- If a checklist item is not applicable to this change, write "n/a"
in the checkbox -->

- [n/a] Addresses an existing issue:
- [n/a] Added/updated relevant unit test(s)
- [x] Ran `./gradlew fastpass` from
`AccessibilityInsightsForAndroidService`
- [x] PR title _AND_ final merge commit title both start with a semantic
tag (`fix:`, `chore:`, `feat(feature-name):`, `refactor:`).
  • Loading branch information
DaveTryon committed Jan 26, 2023
1 parent 07cbc6a commit 1ba9639
Showing 1 changed file with 20 additions and 40 deletions.
Expand Up @@ -3,9 +3,9 @@

package com.microsoft.accessibilityinsightsforandroidservice;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.RETURNS_SELF;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
Expand All @@ -22,7 +22,6 @@
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializer;
import java.lang.reflect.Field;
import java.lang.reflect.Type;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
Expand All @@ -31,18 +30,19 @@
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.AdditionalAnswers;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;

@RunWith(MockitoJUnitRunner.class)
public class ATFAResultsSerializerTest {

@Mock GsonBuilder gsonBuilder;
GsonBuilder gsonBuilder = mock(GsonBuilder.class, RETURNS_SELF);
@Mock Gson gson;
FieldNamingStrategy fieldNamingStrategy;
ExclusionStrategy exclusionStrategy;
JsonSerializer<Class> jsonSerializer;
@Captor ArgumentCaptor<FieldNamingStrategy> fieldNamingStrategy;
@Captor ArgumentCaptor<ExclusionStrategy> exclusionStrategy;
@Captor ArgumentCaptor<JsonSerializer<Class>> jsonSerializer;
ATFAResultsSerializer testSubject;

class TestClass {
Expand All @@ -51,36 +51,13 @@ class TestClass {

@Before
public void prepare() {
doAnswer(
AdditionalAnswers.answer(
(FieldNamingStrategy strategy) -> {
fieldNamingStrategy = strategy;
return gsonBuilder;
}))
.when(gsonBuilder)
.setFieldNamingStrategy(any());

doAnswer(
AdditionalAnswers.answer(
(ExclusionStrategy strategy) -> {
exclusionStrategy = strategy;
return gsonBuilder;
}))
.when(gsonBuilder)
.setExclusionStrategies(any());

doAnswer(
AdditionalAnswers.answer(
(Type type, JsonSerializer<Class> serializer) -> {
jsonSerializer = serializer;
return gsonBuilder;
}))
.when(gsonBuilder)
.registerTypeAdapter(eq(Class.class), any());

when(gsonBuilder.create()).thenReturn(gson);

testSubject = new ATFAResultsSerializer(gsonBuilder);

verify(gsonBuilder).setExclusionStrategies(exclusionStrategy.capture());
verify(gsonBuilder).setFieldNamingStrategy(fieldNamingStrategy.capture());
verify(gsonBuilder).registerTypeAdapter(eq(Class.class), jsonSerializer.capture());
}

@Test
Expand All @@ -90,8 +67,8 @@ class ExtendingClass extends TestClass {
}
Field[] testFields = ExtendingClass.class.getFields();

String testFieldExtendingClass = fieldNamingStrategy.translateName(testFields[0]);
String testFieldBaseClass = fieldNamingStrategy.translateName(testFields[1]);
String testFieldExtendingClass = fieldNamingStrategy.getValue().translateName(testFields[0]);
String testFieldBaseClass = fieldNamingStrategy.getValue().translateName(testFields[1]);

Assert.assertEquals("TestClass.testField", testFieldBaseClass);
Assert.assertEquals("ExtendingClass.testField", testFieldExtendingClass);
Expand All @@ -107,15 +84,18 @@ public void exclusionStrategyExcludesWindowHierarchyElements() {
.filter(c -> !classesToExclude.contains(c))
.collect(Collectors.toList());

classesToExclude.forEach(c -> Assert.assertTrue(exclusionStrategy.shouldSkipClass(c)));
classesToInclude.forEach(c -> Assert.assertFalse(exclusionStrategy.shouldSkipClass(c)));
classesToExclude.forEach(
c -> Assert.assertTrue(exclusionStrategy.getValue().shouldSkipClass(c)));
classesToInclude.forEach(
c -> Assert.assertFalse(exclusionStrategy.getValue().shouldSkipClass(c)));
}

@Test
public void jsonSerializerSerializesClassName() {
JsonPrimitive expectedJson = new JsonPrimitive(TestClass.class.getSimpleName());

JsonElement jsonElement = jsonSerializer.serialize(TestClass.class, Class.class, null);
JsonElement jsonElement =
jsonSerializer.getValue().serialize(TestClass.class, Class.class, null);

Assert.assertEquals(expectedJson, jsonElement);
}
Expand Down

0 comments on commit 1ba9639

Please sign in to comment.