gpt4 book ai didi

java - Junit期望值异常

转载 作者:行者123 更新时间:2023-12-02 08:58:50 26 4
gpt4 key购买 nike

我正在编写一个测试来测试我的存储库。我注意到返回的期望值与实际值匹配,只是实际值包含在“<>”中。

我不知道为什么会发生这种情况。

@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))

}

}

错误

java.lang.AssertionError: 
Expected :[com.darotapp.cornflix.data.local.database.MovieEntity@3843fe46, com.darotapp.cornflix.data.local.database.MovieEntity@1b40b010]
Actual :<[com.darotapp.cornflix.data.local.database.MovieEntity@3843fe46, com.darotapp.cornflix.data.local.database.MovieEntity@1b40b010]>

表的数据类如下

表格

@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? = ""


}

最佳答案

我终于明白了。问题在于使用 IsEqual。我可以使用下面的代码纠正这个问题。


val movies = moviesRepository.getMovies(false, ApplicationProvider.getApplicationContext())

assertEquals(movies?.value, Matchers.equalTo(localTasks))

关于java - Junit期望值异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60325990/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com