Home > Software engineering >  Unit test - Uri is always null
Unit test - Uri is always null

Time:11-27

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)

CodePudding user response:

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

  • Related