0

我正在尝试模拟学生班的最后一个领域

data class Student(
    val id: Int,
    val name: String,
    val marks: Marks
)

data class Marks(
    val subject1: String
)

这是我的测试课

class StudentTest {

    @Test
    fun testStudentMarks() {
        val student = mock(Student::class.java)
        assertNotNull(student.id)
        assertNotNull(student.marks)
    }

}

在运行测试时,它通过了 student.id,但在 student.marks 上失败,并出现以下错误

junit.framework.AssertionFailedError
    at junit.framework.Assert.fail(Assert.java:55)
    at junit.framework.Assert.assertTrue(Assert.java:22)
    at junit.framework.Assert.assertNotNull(Assert.java:256)
    at junit.framework.Assert.assertNotNull(Assert.java:248)
    at com.example.mockitotest.StudentTest.testStudentMarks(StudentTest.kt:16)

我如何模拟标记字段

4

1 回答 1

1

既然是一Kotlin堂课,你就可以

Mockito.when(student.id).thenReturn(0)

或者

val m = Mockito.mock(Marks::class.java)
Mockito.when(student.marks).thenReturn(m)
于 2020-07-10T12:59:12.523 回答