Skip to content

[FR]: Migrate from JUnit assertions to Kotlin's test framework #419

@SimonMarquis

Description

@SimonMarquis

Is there an existing issue for this?

  • I have searched the existing issues

Describe the problem

JUnit assertions have not been designed with Kotlin in mind.

Describe the solution

On the other hand, Kotlin's test framework make use of Kotlin's strenghts and we could benefit from it.

  • better error messages by default

     Assert.assertTrue(item is TopicUiState.Success)
     // java.lang.AssertionError

    vs

     kotlin.test.assertIs<TopicUiState.Success>(item)
     // Expected value to be of type <com.google.samples.apps.nowinandroid.feature.topic.TopicUiState.Success>, actual <class com.google.samples.apps.nowinandroid.feature.topic.TopicUiState$Error>.
  • better type inference

     // This code would produce an error because `getCurrentFollowedTopics` return type is `Set<String>?`
     kotlin.test.assertEquals(emptySet<Int>(), userDataRepository.getCurrentFollowedTopics())
     // Instead, let the type inference do it's job by omiting the type altogether
     assertEquals(emptySet(), userDataRepository.getCurrentFollowedTopics())
  • smart cast

     val item = viewModel.topicUiState.value
     assertIs<TopicUiState.Success>(item)
     // Now item can be used as a TopicUiState.Success

Additional context

No response

Code of Conduct

  • I agree to follow this project's Code of Conduct

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions