0

I have entity:

data class BaseItem(
    val id: Long = 0,
    val name: String = "",
    val description: String = "",
    val uri: Uri = Uri.EMPTY,
    val active: Boolean = true,
    val done: Boolean = false,
    val date: LocalDate? = null,
    val plantId: Long = 0
)

and viewModel method I would like to test:

fun getEmptyUiState(): Pair<BaseItem, List<BaseItem>> {
    return Pair(BaseItem(), listOf<BaseItem>())
}

Test method:

@Test
fun getEmptyUiState() {
    val emptyUiState = viewModel.getEmptyUiState()
    assertThat(emptyUiState).isEqualTo(Pair(BaseItem(), listOf<BaseItem>()))
}

I always get: EMPTY must not be null java.lang.NullPointerException: EMPTY must not be null at com.rachapps.uielements.dto.BaseItem.(BaseItem.kt:11)

RCH
  • 1,247
  • 1
  • 11
  • 16

1 Answers1

0

Unfortunately, Uri is part of the Android SDK and can't be easily tested using Unit Tests.

The only solution is to use PowerMock, as explained here: https://stackoverflow.com/a/34152256/2310221

Markus
  • 1,649
  • 1
  • 22
  • 41
  • Thx. I annotated class with : @RunWith( RobolectricTestRunner::class ) and it also helped – RCH Nov 26 '22 at 14:26