Java Junit预期值异常
我正在编写一个测试来测试我的存储库。我注意到返回的预期值与实际值匹配,只是实际值被包装在“”中 我不知道为什么会这样Java Junit预期值异常,java,android,kotlin,junit5,hamcrest,Java,Android,Kotlin,Junit5,Hamcrest,我正在编写一个测试来测试我的存储库。我注意到返回的预期值与实际值匹配,只是实际值被包装在“”中 我不知道为什么会这样 @ExperimentalCoroutinesApi @RunWith(AndroidJUnit4::class) @Config(sdk = [Build.VERSION_CODES.O_MR1]) class MovieRepositoryTest{ @get:Rule var instantExecutorRule = InstantTaskExecutor
@ExperimentalCoroutinesApi
@RunWith(AndroidJUnit4::class)
@Config(sdk = [Build.VERSION_CODES.O_MR1])
class MovieRepositoryTest{
@get:Rule
var instantExecutorRule = InstantTaskExecutorRule()
private val movie1 = MovieEntity("Title1", "https://movie1.jpg", 3, "Movie1Overview", "Jan 2021")
private val movie2 = MovieEntity("Title2", "https://movie2.jpg", 3, "Movie2Overview", "Jan 2022")
private val movie3 = MovieEntity("Title3", "https://movie3.jpg", 3, "Movie2Overview", "Jan 2023")
private val remoteTasks = mutableListOf(movie3).sortedBy { it.id }
private val localTasks = mutableListOf(movie1, movie2).sortedBy { it.id }
private lateinit var tasksRemoteDataSource: FakeDataSource<MovieEntity>
private lateinit var tasksLocalDataSource: FakeDataSource<MovieEntity>
private lateinit var moviesRepository: MoviesRepoInterface
@Before
fun createRepository() {
tasksRemoteDataSource = FakeDataSource(remoteTasks.toMutableList())
tasksLocalDataSource = FakeDataSource(localTasks.toMutableList())
moviesRepository = MovieRepository(tasksRemoteDataSource, tasksLocalDataSource)
}
@Test
fun getRemoteMovies_RequestAllMovie()= runBlockingTest{
val movies = moviesRepository.getMovies(false, ApplicationProvider.getApplicationContext())
assertEquals(movies.getOrAwaitValue(), IsEqual<List<MovieEntity>>(localTasks))
}
}
我终于明白了。问题是使用等质量。我可以用下面的代码更正这个问题
val movies = moviesRepository.getMovies(false, ApplicationProvider.getApplicationContext())
assertEquals(movies?.value, Matchers.equalTo(localTasks))
你能为电影实体发布你的equals和hashcode吗?或者它是一个数据类?是的,它是一个数据类。你能发布它的定义吗?已经编辑并包含了数据类定义。你能显示ˋmovies.getOrAwaitValue()ˋ和ˋlocalTasksˋ的类型签名吗?我猜一个是列表,另一个是其他东西。使用
代码>因为在这个测试中你总是希望看到电影电影!!。值
@Entity
data class MovieEntity(
var title: String?,
var movieImage:String?,
var rating:Int?,
var overView:String?,
var releaseDate:String?
): Serializable {
operator fun component1(): String? = title
operator fun component2(): String? = movieImage
operator fun component3(): Int? = rating
operator fun component4(): String? = overView
operator fun component5(): String? = releaseDate
@PrimaryKey()
var id: Int = 0
var favourite:Boolean = false
var movieId:String? = ""
}
val movies = moviesRepository.getMovies(false, ApplicationProvider.getApplicationContext())
assertEquals(movies?.value, Matchers.equalTo(localTasks))