Skip to content

Commit

Permalink
Fix test failures with Robolectric 4.9
Browse files Browse the repository at this point in the history
1. Use legacy LooperMode for tests explicitly, because recent
   Robolectric releases switch to use PAUSED mode default. Before these
   tests migrate to PAUSED mode, they use LEGACY mode to pass tests.
2. Migrate Assert.assertThat to Truth.assertThat to avoid using removed
   APIs.
3. All build.gradle use the same robolectricVersion.
4. Remove unused exclude protobuf-java from Robolectric.
5. Add necessary protobuf-lite dependency on classpath for
   some ktx modules' tests.

Signed-off-by: utzcoz <utzcoz@outlook.com>
  • Loading branch information
utzcoz committed Oct 1, 2022
1 parent 10cd4ad commit 0724457
Show file tree
Hide file tree
Showing 23 changed files with 61 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,12 @@
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;
import org.robolectric.annotation.LooperMode;

/** Tests for {@link DebugAppCheckProvider}. */
@RunWith(RobolectricTestRunner.class)
@Config(manifest = Config.NONE)
@LooperMode(LooperMode.Mode.LEGACY)
public class DebugAppCheckProviderTest {

private static final String DEBUG_SECRET = "debugSecret";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,12 @@
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;
import org.robolectric.annotation.LooperMode;

/** Tests for {@link PlayIntegrityAppCheckProvider}. */
@RunWith(RobolectricTestRunner.class)
@Config(manifest = Config.NONE)
@LooperMode(LooperMode.Mode.LEGACY)
public class PlayIntegrityAppCheckProviderTest {

private static final String PROJECT_NUMBER = "123456";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,12 @@
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;
import org.robolectric.annotation.LooperMode;

/** Tests for {@link SafetyNetAppCheckProvider}. */
@RunWith(RobolectricTestRunner.class)
@Config(manifest = Config.NONE)
@LooperMode(LooperMode.Mode.LEGACY)
public class SafetyNetAppCheckProviderTest {

private static final String API_KEY = "apiKey";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,12 @@
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;
import org.robolectric.annotation.LooperMode;

/** Tests for {@link DefaultFirebaseAppCheck}. */
@RunWith(RobolectricTestRunner.class)
@Config(manifest = Config.NONE)
@LooperMode(LooperMode.Mode.LEGACY)
public class DefaultFirebaseAppCheckTest {

private static final String EXCEPTION_TEXT = "exceptionText";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.LooperMode;

@RunWith(RobolectricTestRunner.class)
@LooperMode(LooperMode.Mode.LEGACY)
public class DefaultTokenRefresherTest {

private static final long TIME_TO_REFRESH_MILLIS = 1000L;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@
import org.junit.runner.RunWith;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.LooperMode;

@RunWith(RobolectricTestRunner.class)
@LooperMode(LooperMode.Mode.LEGACY)
public class FirebaseAppDistributionProxyTest {

private FirebaseAppDistribution firebaseAppDistribution;
Expand Down
2 changes: 1 addition & 1 deletion firebase-appdistribution/firebase-appdistribution.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ dependencies {
runtimeOnly project(':firebase-installations')

testImplementation 'junit:junit:4.13.2'
testImplementation "org.robolectric:robolectric:4.9"
testImplementation "org.robolectric:robolectric:$robolectricVersion"
testImplementation "com.google.truth:truth:$googleTruthVersion"
testImplementation 'org.mockito:mockito-inline:3.4.0'
androidTestImplementation "org.mockito:mockito-android:3.4.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.annotation.Config;
import org.robolectric.annotation.LooperMode;

@RunWith(AndroidJUnit4.class)
@Config(sdk = 25)
@LooperMode(LooperMode.Mode.LEGACY)
public class DataCollectionPostNDefaultDisabledTest {

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.annotation.Config;
import org.robolectric.annotation.LooperMode;

@RunWith(AndroidJUnit4.class)
@Config(sdk = 19)
Expand Down Expand Up @@ -81,6 +82,7 @@ public void setDataCollectionDefaultEnabledTrue_shouldUpdateSharedPrefs() {
}

@Test
@LooperMode(LooperMode.Mode.LEGACY)
public void setDataCollectionDefaultEnabledTrue_shouldEmitEvents() {
withApp(
app -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;
import org.robolectric.annotation.LooperMode;
import org.skyscreamer.jsonassert.JSONAssert;

/**
Expand All @@ -93,6 +94,7 @@
@Config(
manifest = Config.NONE,
shadows = {ShadowPreconditions.class})
@LooperMode(LooperMode.Mode.LEGACY)
public final class FirebaseRemoteConfigTest {
private static final String APP_ID = "1:14368190084:android:09cb977358c6f241";
private static final String API_KEY = "AIzaSyabcdefghijklmnopqrstuvwxyz1234567";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;
import org.robolectric.annotation.LooperMode;

/**
* Unit tests for the Firebase Remote Config Component.
Expand Down Expand Up @@ -92,6 +93,7 @@ public void setUp() {
}

@Test
@LooperMode(LooperMode.Mode.LEGACY)
public void frc2p_doesNotCallAbt() throws Exception {

FirebaseRemoteConfig fireperfFrc =
Expand All @@ -106,6 +108,7 @@ public void frc2p_doesNotCallAbt() throws Exception {
}

@Test
@LooperMode(LooperMode.Mode.LEGACY)
public void frcNonMainFirebaseApp_doesNotCallAbt() throws Exception {

when(mockFirebaseApp.getName()).thenReturn("secondary");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;
import org.robolectric.annotation.LooperMode;
import org.skyscreamer.jsonassert.JSONAssert;

/**
Expand All @@ -99,6 +100,7 @@
*/
@RunWith(RobolectricTestRunner.class)
@Config(manifest = Config.NONE)
@LooperMode(LooperMode.Mode.LEGACY)
public class ConfigFetchHandlerTest {
private static final String INSTALLATION_ID = "'fL71_VyL3uo9jNMWu1L60S";
private static final String INSTALLATION_AUTH_TOKEN =
Expand Down
2 changes: 1 addition & 1 deletion firebase-crashlytics/firebase-crashlytics.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ dependencies {

testImplementation 'androidx.test:runner:1.4.0'
testImplementation 'androidx.test:core:1.4.0'
testImplementation "org.robolectric:robolectric:4.9"
testImplementation "org.robolectric:robolectric:$robolectricVersion"
testImplementation 'junit:junit:4.13.2'
testImplementation 'org.mockito:mockito-core:3.4.3'

Expand Down
4 changes: 1 addition & 3 deletions firebase-firestore/firebase-firestore.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,7 @@ dependencies {
testImplementation 'androidx.test:core:1.2.0'
testImplementation "org.hamcrest:hamcrest-junit:2.0.0.0"
testImplementation 'org.mockito:mockito-core:2.25.0'
testImplementation ("org.robolectric:robolectric:4.9") {
exclude group: 'com.google.protobuf', module: 'protobuf-java'
}
testImplementation "org.robolectric:robolectric:$robolectricVersion"
testImplementation "com.google.truth:truth:$googleTruthVersion"
testImplementation 'com.fasterxml.jackson.core:jackson-databind:2.9.8'
testImplementation 'com.google.guava:guava-testlib:12.0-rc2'
Expand Down
1 change: 1 addition & 0 deletions firebase-firestore/ktx/ktx.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ dependencies {
implementation 'androidx.annotation:annotation:1.1.0'
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutinesVersion"
implementation 'com.google.android.gms:play-services-basement:18.1.0'
testCompileOnly "com.google.protobuf:protobuf-java:$protocVersion"
testImplementation project(':firebase-database-collection')
testImplementation 'org.mockito:mockito-core:2.25.0'
testImplementation 'com.fasterxml.jackson.core:jackson-databind:2.9.8'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,13 @@
import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;
import org.robolectric.annotation.LooperMode;
import org.robolectric.shadows.ShadowActivity;
import org.robolectric.shadows.ShadowPackageManager;

@RunWith(RobolectricTestRunner.class)
@Config(sdk = 21, qualifiers = "port")
@LooperMode(LooperMode.Mode.LEGACY)
public class FirebaseInAppMessagingDisplayTest {

private com.google.firebase.inappmessaging.display.FirebaseInAppMessagingDisplay fiamUI;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,12 @@
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;
import org.robolectric.annotation.LooperMode;

// TODO: Refactor and clean this logic
@RunWith(RobolectricTestRunner.class)
@Config(manifest = Config.NONE)
@LooperMode(LooperMode.Mode.LEGACY)
public class InAppMessageStreamManagerTest {
private static final long PAST = 1000000;
private static final long NOW = PAST + 100000;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,11 @@
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;
import org.robolectric.annotation.LooperMode;

@RunWith(RobolectricTestRunner.class)
@Config(manifest = Config.NONE)
@LooperMode(LooperMode.Mode.LEGACY)
public class MetricsLoggerClientTest {
private static final long PAST = 1000000;
private static final long NOW = PAST + 100000;
Expand Down
Loading

0 comments on commit 0724457

Please sign in to comment.