我是Kotlin的新手,对Java没有什么经验,所以请耐心听我说。从我所能研究的(例如here)来看,mockFoo
应该可以从外部类访问,但是IntelliJ给了我一个Unresolved Reference
错误。
@ExtendWith(MockKExtension::class)
class GetBazTest {
@MockK
private lateinit var mockFoo: FooClient
@MockK
private lateinit var mockBar: BarClient
private lateinit var getBaz: GetBaz
@BeforeEach
fun setup() {
getBaz = GetBaz(
mockFoo,
mockBar,
)
}
@Test
fun `it should do a thing`() {
// some code to create expectedResult
coEvery {
mockFoo.getSomething(any())
} answers {
expectedResult
}
}
@Nested
@DisplayName("Tests for Xyz")
class XyzTest {
@Test
fun `it should do a different thing`() {
// some code to create expectedResult
coEvery {
mockFoo.getAThing(any()) // mockFoo gives Unresolved Reference error
} answers {
expectedResult
}
}
}
}
在内部类中再次需要setUp
mockFoo
,这似乎不太对。我如何访问外部变量?
1条答案
按热度按时间rhfm7lfc1#
只需使XyzTest成为一个
inner
类,以授予他访问外部类成员的权限