0

I am running Android instrumentation tests on an emulator on Travis CI. The following test case invokes a helper method per method reference:

@Test
public void testGetLowEmissionZones_worksAtAll() {

    // ...

    lowEmissionZone.childZones.forEach(this::testChildZone);

    // ...

}

private void testChildZone(@NonNull ChildZone childZone) {
    // ...
}

When Travis CI executes this test it fails with a NoClassDefFoundError:

ContentProviderTest > testGetLowEmissionZones_worksAtAll[test(AVD) - 4.3.1] FAILED 
    java.lang.NoClassDefFoundError: -$$Lambda$ContentProviderTest$He_xH9TsDaN0tZU8EqFP1CuQyAM
    at ContentProviderTest.testLowEmissionZone(ContentProviderTest.java:151)

If I change the method invocation then there is no error:

@Test
public void testGetLowEmissionZones_worksAtAll() {

    // ...

    for (ChildZone childZone : lowEmissionZone.childZones) {
            testChildZone(childZone);
    }

    // ...

}

I tried both openjdk8 and oraclejdk8, both fail.

How can I continue using method references?

JJD
  • 50,076
  • 60
  • 203
  • 339

1 Answers1

2

Probably, you are facing this issue because forEach(Consumer<?> consumer) is not available in Jelly Bean.

As you can see, the test is failling on 4.3.1. Ensure that this is related to API levels of the AVD. Ensure the code is working properly on API level 24 onwards.

Nikola Despotoski
  • 49,966
  • 15
  • 119
  • 148
  • Good point. My understanding was that the [desugar step](https://developer.android.com/studio/write/java8-support) of the Android toolchain transforms any language constructs back to their counterparts in lower Java versions. Is this not true for `forEach`? - Why do you especially mention API level 24 (7.0) here? – JJD Mar 01 '19 at 09:52
  • @JJD `forEach` method has been added in that version. – Nikola Despotoski Mar 01 '19 at 11:45