'SmsMessage.calculateLength() returns null reference in Junit test app

I was writing test cases for a class where an SMS message length is calculated

class A {

   public int getLength() {
       int[] size = SmsMessage.calculateLength(" ", false);
       return size[1] + size[2];
   }
}

Test code

@Test
public void Should_equal_When_called_calculateLength() {
   A obj = new A();
   assert(160, obj.getLength());
}

I'm getting the "size" in "getLength()" method null reference of an array where if I run my app it never returns null reference

What could be the reason?



Solution 1:[1]

When I added the following above my test class test method passed - no exception thrown for null reference

@RunWith(RobolectricTestRunner.class)
@Config(sdk = Build.VERSION_CODES.R)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Tajuddin Khandaker