Skip to content

Commit 732801d

Browse files
Fix Mockito exception (#1730)
* Rewrite HapticInterfaceManagerTest.createViews() without using Mockito * Add missing final keyword * Remove unused import
1 parent 8356439 commit 732801d

1 file changed

Lines changed: 37 additions & 22 deletions

File tree

android/sdl_android/src/androidTest/java/com/smartdevicelink/managers/video/HapticInterfaceManagerTest.java

Lines changed: 37 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
**************************************************************************************************/
2222
package com.smartdevicelink.managers.video;
2323

24+
import android.content.Context;
2425
import android.view.View;
2526
import android.view.ViewGroup;
2627
import android.widget.Button;
@@ -45,9 +46,7 @@
4546
import org.mockito.ArgumentCaptor;
4647
import org.mockito.Captor;
4748
import org.mockito.Mock;
48-
import org.mockito.invocation.InvocationOnMock;
4949
import org.mockito.junit.MockitoJUnitRunner;
50-
import org.mockito.stubbing.Answer;
5150

5251
import java.util.ArrayList;
5352
import java.util.List;
@@ -56,7 +55,6 @@
5655
import static org.mockito.ArgumentMatchers.eq;
5756
import static org.mockito.ArgumentMatchers.isNull;
5857
import static org.mockito.Mockito.any;
59-
import static org.mockito.Mockito.doAnswer;
6058
import static org.mockito.Mockito.doReturn;
6159
import static org.mockito.Mockito.mock;
6260
import static org.mockito.Mockito.times;
@@ -222,36 +220,53 @@ public void testRefreshWithUserData() throws Exception {
222220
}
223221

224222
private View createViews() {
223+
Context context = InstrumentationRegistry.getInstrumentation().getContext();
225224

226-
View view = mock(View.class);
225+
final View view = new View(context) {
226+
private int count = 0;
227227

228-
ViewGroup parent1 = mock(ViewGroup.class);
229-
ViewGroup parent2 = mock(ViewGroup.class);
228+
@Override
229+
public boolean isClickable() {
230+
int curCount = count++;
231+
return (curCount >= 1) && (curCount <= 4);
232+
}
233+
};
230234

231-
when(parent1.getChildCount()).thenReturn(5);
235+
final ViewGroup parent1 = new ViewGroup(context) {
236+
@Override
237+
protected void onLayout(boolean b, int i, int i1, int i2, int i3) {}
232238

233-
when(parent1.getChildAt(0)).thenReturn(view);
234-
when(parent1.getChildAt(1)).thenReturn(view);
235-
when(parent1.getChildAt(2)).thenReturn(view);
236-
when(parent1.getChildAt(3)).thenReturn(parent2);
237-
when(parent1.getChildAt(4)).thenReturn(view);
239+
@Override
240+
public View getChildAt(int index) {
241+
return view;
242+
}
238243

239-
when(parent2.getChildCount()).thenReturn(2);
240-
when(parent2.getChildAt(0)).thenReturn(view);
241-
when(parent2.getChildAt(1)).thenReturn(view);
244+
@Override
245+
public int getChildCount() {
246+
return 2;
247+
}
248+
};
242249

250+
final ViewGroup parent2 = new ViewGroup(context) {
251+
@Override
252+
protected void onLayout(boolean b, int i, int i1, int i2, int i3) {}
243253

244-
doAnswer(new Answer<Boolean>() {
245-
private int count = 0;
254+
@Override
255+
public View getChildAt(int index) {
256+
if (index == 3) {
257+
return parent1;
258+
} else {
259+
return view;
260+
}
261+
}
246262

247263
@Override
248-
public Boolean answer(InvocationOnMock invocation) throws Throwable {
249-
int curCount = count++;
250-
return (curCount >= 1) && (curCount <= 4);
264+
public int getChildCount() {
265+
return 5;
251266
}
252-
}).when(view).isClickable();
267+
};
253268

254-
return parent1;
269+
return parent2;
255270
}
256271

257272

0 commit comments

Comments
 (0)