3

I am using UrbanAirship for receiving push and i can successfully receive notifications. I ve decided to add test to my project using robolectric and created a simple test as shown below.

@Config(emulateSdk = 18)
@RunWith(RobolectricTestRunner.class)
public class MyActivityTest {

    @Test
    public void test1(){
            assertTrue(true);
    }
}

and get

test1 FAILED java.lang.RuntimeException
Caused by: java.lang.IllegalStateException

When I run my test with debug i ve noticed a strange error.

java.lang.IllegalStateException: Unable to resolve UrbanAirshipProvider. Please check that the provider is defined in your AndroidManifest.xml, and that the authority string is set to "YOUR_PACKAGENAME.urbanairship.provider"

I checked my manifest but it looks like nothing is wrong.

 <provider android:name="com.urbanairship.UrbanAirshipProvider"
        android:authorities="com.test.myapp.app.urbanairship.provider"
        android:permission="com.test.myapp.app.permission.UA_DATA"
        android:exported="true"
        android:multiprocess="true" />

I can run my tests without error if i comment out lines related to urbanairship. I am using AndroidStudio and version of the urbanairship jar is 4.0.2. Is there any way to fix this ?

Barışcan Kayaoğlu
  • 1,294
  • 3
  • 14
  • 35
barisemreefe
  • 434
  • 4
  • 11

3 Answers3

1

The problem is Robolectric does not implement the methods used by Urban Airship to determine if the provider is in the manifest properly. This is only checked when "inProduction" is disabled in the airship config options. You may be able to get away with setting it to true for the tests.

You can provide a test application class that can be used in place of your application where you implemented takeoff. You can then either not call takeoff or try the doing the "inProduction" change noted above. Here is a link that I quickly found http://robolectric.blogspot.com/2013/04/the-test-lifecycle-in-20.html. Hopefully its still relevant.

If you decide to not call takeoff then you may run into NPE when testing code that calls into the Urban Airship library. A quick solution would be to inject mocked version of UAirship and its components into classes you are trying to test. Some static calls may require takeoff as well and would need have a wrapper that is able to be mocked or stubbed. This is not the most elegant solution to the problem but it should allow you to start testing. We will definitely investigate solutions to make testing easier in the future.

ralepinski
  • 1,756
  • 8
  • 15
0

You can try setting the manifest in your Config to see if that helps:

@Config(emulateSdk = 18, manifest = "/path/to/your/AndroidManifest.xml")
@RunWith(RobolectricTestRunner.class)
public class MyActivityTest {
...
}
Suraj C
  • 452
  • 4
  • 10
0

You can wrap your Urban Airship TakeOff code in a try catch in your application class. That way it does not cause test cases to crash when its not found.

sirFunkenstine
  • 8,135
  • 6
  • 40
  • 57