Kotlin Mockito NullPointerException
Kotlin Mockito NullPointerException
有class个:
@Singleton
class Exchange(
@Client("${exchange.rest.url}") @Inject val httpClient: RxHttpClient
) : Exchange {
override suspend fun getSymbols(): List<String> {
val response = httpClient.retrieve(GET<String>("/someurl/symbols"), ExchangeInfo::class.java).awaitFirst()
return response.data
.map { it.symbol }.toList()
}
}
@Introspected
@JsonIgnoreProperties(ignoreUnknown = true)
class ExchangeInfo(
val data: List<Symbol>
)
@Introspected
@JsonIgnoreProperties(ignoreUnknown = true)
data class Symbol(
val symbol: String
)
我想测试功能getSymbols()
我正在写测试class:
import new.project.ExchangeInfo
import new.project.SpotSymbol
import io.micronaut.http.HttpRequest
import io.micronaut.http.client.RxHttpClient
import io.reactivex.Emitter
import io.reactivex.Flowable
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.reactive.awaitFirst
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withContext
import okhttp3.internal.immutableListOf
import org.junit.jupiter.api.Test
import org.mockito.Mockito.mock
import org.mockito.kotlin.whenever
class ExchangeTest {
@Test
fun getSymbolsTest() = runBlocking {
//the answer I want to receive
val response: Flowable<ExchangeInfo> = Flowable.generate<ExchangeInfo, String>(
java.util.concurrent.Callable<String> { -> "symbol" },
io.reactivex.functions.BiConsumer<String, Emitter<ExchangeInfo>> { t1, t2 -> }
)
val httpClient = mock(RxHttpClient::class.java)
whenever(httpClient.retrieve(HttpRequest.GET<String>("/someurl/symbols"), ExchangeInfo::class.java))
.thenReturn(response)
val exchange = Exchange(httpClient)
//calling the tested method
val result = exchange.getSymbols()
assert(immutableListOf("symbol") == result)
}
}
当运行测试时,我得到:
java.lang.NullPointerException: httpClient.retrieve (GET <… ExchangeInfo :: class.java) must not be null
你能告诉我如何正确模拟 httpClient 吗?
有必要这样做:doReturn(Flowable.just(ExchangeInfo)).when(httpClient).retrieve(any<HttpRequest<String>>(), any<Class<Any>>())
有class个:
@Singleton
class Exchange(
@Client("${exchange.rest.url}") @Inject val httpClient: RxHttpClient
) : Exchange {
override suspend fun getSymbols(): List<String> {
val response = httpClient.retrieve(GET<String>("/someurl/symbols"), ExchangeInfo::class.java).awaitFirst()
return response.data
.map { it.symbol }.toList()
}
}
@Introspected
@JsonIgnoreProperties(ignoreUnknown = true)
class ExchangeInfo(
val data: List<Symbol>
)
@Introspected
@JsonIgnoreProperties(ignoreUnknown = true)
data class Symbol(
val symbol: String
)
我想测试功能getSymbols()
我正在写测试class:
import new.project.ExchangeInfo
import new.project.SpotSymbol
import io.micronaut.http.HttpRequest
import io.micronaut.http.client.RxHttpClient
import io.reactivex.Emitter
import io.reactivex.Flowable
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.reactive.awaitFirst
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withContext
import okhttp3.internal.immutableListOf
import org.junit.jupiter.api.Test
import org.mockito.Mockito.mock
import org.mockito.kotlin.whenever
class ExchangeTest {
@Test
fun getSymbolsTest() = runBlocking {
//the answer I want to receive
val response: Flowable<ExchangeInfo> = Flowable.generate<ExchangeInfo, String>(
java.util.concurrent.Callable<String> { -> "symbol" },
io.reactivex.functions.BiConsumer<String, Emitter<ExchangeInfo>> { t1, t2 -> }
)
val httpClient = mock(RxHttpClient::class.java)
whenever(httpClient.retrieve(HttpRequest.GET<String>("/someurl/symbols"), ExchangeInfo::class.java))
.thenReturn(response)
val exchange = Exchange(httpClient)
//calling the tested method
val result = exchange.getSymbols()
assert(immutableListOf("symbol") == result)
}
}
当运行测试时,我得到:
java.lang.NullPointerException: httpClient.retrieve (GET <… ExchangeInfo :: class.java) must not be null
你能告诉我如何正确模拟 httpClient 吗?
有必要这样做:doReturn(Flowable.just(ExchangeInfo)).when(httpClient).retrieve(any<HttpRequest<String>>(), any<Class<Any>>())