我有一個客戶端類(在一個Android應用程式中用Kotlin撰寫),它實作了一個介面ReadyCallback(在應用程式的一個庫中用Java撰寫,應用程式依賴于這個庫)。在客戶端中我有一個createClient()方法,它將創建一個引數為ReadyCallback的客戶端。
在庫中:
//在這個庫的某個地方,我有一個邏輯,當我認為它是 "準備好了 "的時候,就呼叫`readyCallback.onReady()`。
interface ReadyCallback {
void onReady() }
}
class Manager {
private final ReadyCallback readyCallback;
public void onConnected(final boolean isConnected) {
if (isConnected) {
readyCallback.onReady()。
}
}
在應用程式中:
class ClassA internal constructor(private val clientProvider: ClassB, private val classC: ClassC, private val ClassD: ClassD) : ReadyCallback {
fun createClient() {
val client = clientProvider.create(getReadyCallback() )
}
private fun getReadyCallback() {
return ReadyCallback { onReady() }
}
override fun onReady() {
logInfo { "它已經準備好了!"。現在通過呼叫classC.otherMethod()做一些事情" }
classC.otherMethod()
}
}
在單元測驗中,我想驗證當我創建客戶端并準備好時,classC的otherMethod()將被呼叫。我試著做了以下作業,但是不正確:
import com.nhaarman.mockitokotlin2.*.
import org.junit.*.
class ClassATest {
lateinit var unitUnderTest: 級別A
lateinit var clientProviderMock: 課堂B
lateinit var classCMock: 課堂C
lateinit var clientMock: 課堂D
override fun setup() {
super.setup()
clientProviderMock = mock()
classCMock = mock()
clientMock = mock()
unitUnderTest = ClassA(clientProvider = clientProviderMock, classC = classCMock, classD = classDMock)
whenever(clientProviderMock.create(any()).thenReturn(client)
}
fun `when create client then call otherMethod`() {
unitUnderTest.createClient()
驗證(classCMock).otherMethod()
}
}
錯誤資訊顯示:
需要但沒有呼叫。
classC.otherMethod()。
實際上,與thismock的互動為零。
我認為我得到這個錯誤的原因是,如果我不呼叫getReadyCallback(),這意味著我沒有呼叫回呼,所以沒有呼叫classC.otherMethod()。但除此之外,我真的被卡住了,我不知道如何對我的愿望行為進行單元測驗(如果準備好了,classC.otherMethod()將被呼叫,如果沒有準備好,這個方法將不會被呼叫)。
我知道我不能像下面這樣做,因為 unitUnderTest 不是一個模擬物件:
我知道我不能像下面這樣做。
callbackMock = mock()
whenever(unitUnderTest.getReadyCallback()).thenReturn(callbackMock)
whenever(clientProviderMock.create(callbackMock).thenReturn(client)
誰能幫我一下嗎?
uj5u.com熱心網友回復:
我能想到的唯一方法是在回呼的onReady()方法中添加一個布爾標志。所以它將成為:
在庫中:
interface ReadyCallback {
void onReady(final boolean isReady)
}
class Manager {
private final ReadyCallback readyCallback;
public void onConnected(final boolean isConnected) {
if (isConnected) {
readyCallback.onReady(true)。
} else {
readyCallback.onReady(false)。
}
}
在應用程式中:
class ClassA internal constructor(private val clientProvider: ClassB, private val classC: ClassC, private val ClassD: ClassD) : ReadyCallback {
fun createClient() {
val client = clientProvider.create(getReadyCallback() )
}
private fun getReadyCallback() {
return ReadyCallback { isReady -> onReady(isReady) }
}
override fun onReady(isReady: Boolean) {
if (isReady) {
logInfo { "它已經準備好了!"。現在通過呼叫classC.otherMethod()做一些事情" }
classC.otherMethod()
}
}
}
在單元測驗中:
import com.nhaarman.mockitokotlin2.*.
import org.junit.*.
class ClassATest {
lateinit var unitUnderTest: 級別A
lateinit var clientProviderMock: 課堂B
lateinit var classCMock: 課堂C
lateinit var clientMock: 課堂D
@Before
override fun setup() {
super.setup()
clientProviderMock = mock()
classCMock = mock()
clientMock = mock()
unitUnderTest = ClassA(clientProvider = clientProviderMock, classC = classCMock, classD = classDMock)
whenever(clientProviderMock.create(any()).thenReturn(client)
}
@Test
fun `when create client and ready then call otherMethod`() {
unitUnderTest.onReady(true)
unitUnderTest.createClient()
驗證(classCMock).otherMethod()
}
@Test
fun `when create client and not ready then do not call otherMethod`() {
unitUnderTest.onReady(false)
unitUnderTest.createClient()
驗證零互動(classCMock)
}
}
但是我仍然不知道如何在回呼方法中不使用布爾引數進行測驗。有誰知道該怎么做嗎?
uj5u.com熱心網友回復:
我想我明白了。我不需要onReady()中的引數。
在庫中:
interface ReadyCallback {
void onReady() }
}
//決定何時 "就緒 "的地方
class Manager {
private final ReadyCallback readyCallback;
public void onConnected(final boolean isConnected) {
if (isConnected) {
readyCallback.onReady()。
}
}
在應用程式中:
class ClassA internal constructor(private val clientProvider: ClassB, private val classC: ClassC, private val ClassD: ClassD) : ReadyCallback {
fun createClient() {
val client = clientProvider.create(getReadyCallback() )
}
private fun getReadyCallback() {
return ReadyCallback { onReady() }
}
override fun onReady() {
logInfo { "它已經準備好了!"。現在通過呼叫classC.otherMethod()做一些事情" }
classC.otherMethod()
}
}
在單元測驗中:
import com.nhaarman.mockitokotlin2.*.
import org.junit.*.
class ClassATest {
lateinit var unitUnderTest: 級別A
lateinit var clientProviderMock: 課堂B
lateinit var classCMock: 課堂C
lateinit var clientMock: 課堂D
@Before
override fun setup() {
super.setup()
clientProviderMock = mock()
classCMock = mock()
clientMock = mock()
unitUnderTest = ClassA(clientProvider = clientProviderMock, classC = classCMock, classD = classDMock)
whenever(clientProviderMock.create(any()).thenReturn(client)
}
@Test
fun `when create client and ready then call otherMethod`() {
unitUnderTest.onReady()
unitUnderTest.createClient()
驗證(classCMock).otherMethod()
}
@Test
fun `when create client and not ready then do not call otherMethod`() {
unitUnderTest.createClient()
verifyZeroInteractions(classCMock)
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/310305.html
標籤:
