Skip to content

Commit

Permalink
Fix token adapter instrumented tests
Browse files Browse the repository at this point in the history
Upgrade Mockito from 2.23.0 to 2.24.0 to avoid the following issue:
org.mockito.exceptions.base.MockitoException: Mockito cannot mock this class:
class android.content.Context.  Underlying exception :
java.lang.IllegalArgumentException: On Android P, a class injection can only be
applied to BaseDexClassLoader:
net.bytebuddy.dynamic.loading.MultipleParentClassLoader@23f7dce

Looking at Mockito 2.x release notes:
https://github.com/mockito/mockito/blob/release/2.x/doc/release-notes/official.md

- Previous issue may have been fixed in later 2.23.18 published on now
  unavailable Mockito Bintray repository. Consequently, move to 2.24.0 release
available on Maven Central.
- It seems that from 2.23.13 and androidx.test support added, tests are now
  launched in a dedicated instrumentation thread that does not prepare an
Android Looper. But as Adapter class will instatiate an Android Handler, even
if not needed for the tests, the following error is encountered:
java.lang.RuntimeException: Can't create handler inside thread Thread[Instr:
androidx.test.runner.AndroidJUnitRunner,5,main] that has not called
Looper.prepare()
Consequently, force the Looper to be prepared once before tests.
  • Loading branch information
joggee-fr committed Jul 9, 2024
1 parent 0e3e9fc commit 07bff84
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
4 changes: 2 additions & 2 deletions mobile/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ dependencies {
implementation 'androidx.appcompat:appcompat:1.4.1'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
testImplementation 'junit:junit:4.12'
testImplementation 'org.mockito:mockito-core:2.23.0'
androidTestImplementation 'org.mockito:mockito-android:2.23.0'
testImplementation 'org.mockito:mockito-core:2.24.0'
androidTestImplementation 'org.mockito:mockito-android:2.24.0'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Looper;
import android.util.Log;
import android.util.Pair;

Expand All @@ -12,6 +13,7 @@
import org.json.JSONArray;
import org.json.JSONException;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
Expand Down Expand Up @@ -55,6 +57,14 @@ private int getSize() {
public TokenAdapterTest() throws Token.InvalidUriException {
}

@BeforeClass
public static void setupOnce() {
// Instrumented tests are launched in a dedicated instrumentation thread that does not prepare an Android Looper.
// But it is mandatory here as Adapter will instantiate an Android Handler.
if (Looper.myLooper() == null)
Looper.prepare();
}

@Before
public void setup() {
when(mockContext.getSharedPreferences(anyString(), anyInt()))
Expand Down

0 comments on commit 07bff84

Please sign in to comment.