I am trying to perform a unit test and mock a retrofit call without success. When I run my test, I get only end printed. I should receive onResponse() printed as well.
The code works fine when I run my app, only the test does not call the mocked API call.
Method in ViewModel:
fun loadSensors() {
CoroutineScope(Dispatchers.IO).launch {
sensorsService.getUserSensors(getUserToken(), getUserId())
.enqueue(object : Callback<List<Long>> {
override fun onResponse(
call: Call<List<Long>>,
response: Response<List<Long>>
) {
println("onResponse()")
}
override fun onFailure(call: Call<List<Long>>, t: Throwable) {
println("onFailure()")
}
})
}
println("end")
}
Interface:
#GET("/sensors")
fun getUserSensors(): Call<List<Long>>
App module:
#Provides
#Singleton
fun provideRetrofitFactory(gsonConverterFactory: GsonConverterFactory): Retrofit {
val client = OkHttpClient.Builder().build()
return Retrofit.Builder()
.baseUrl("http://<url>")
.addConverterFactory(gsonConverterFactory)
.client(client)
.build()
}
Test:
#OptIn(DelicateCoroutinesApi::class)
private val mainThreadSurrogate = newSingleThreadContext("UI thread")
#OptIn(ExperimentalCoroutinesApi::class)
#BeforeAll
fun beforeAll() {
Dispatchers.setMain(mainThreadSurrogate)
}
#Test
fun loadSensors() {
val mockedCall = mockk<retrofit2.Call<List<Long>>>()
every { mockedCall.enqueue(any()) } answers {
val callback = args[0] as retrofit2.Callback<List<Long>>
val response = retrofit2.Response.success(200, listOf(1L, 2L, 3L))
callback.onResponse(mockedCall, response)
}
every { sensorsService.getUserSensors(any(), any()) } answers {
mockedCall
}
}
I recommended that you see MockWebServer I am sure with use it you can do anything you have in your mind.
Related
The question about post requests in android has been asked before, but all the solutions I've tried have not worked properly. On top of that, a lot of them seem to be overly complicated as well. All I wish to do is make a post to a specific sight with a few body parameters. Is there any simple way to do that?
Let me explain my request calling structure using Retrofit.
build.gradle(app)
// Retrofit + GSON
implementation 'com.squareup.okhttp3:logging-interceptor:4.4.0'
implementation "com.squareup.retrofit2:retrofit:2.9.0"
implementation "com.squareup.retrofit2:converter-gson:2.9.0"
ApiClient.kt
object ApiClient {
private const val baseUrl = ApiInterface.BASE_URL
private var retrofit: Retrofit? = null
private val dispatcher = Dispatcher()
fun getClient(): Retrofit? {
val logging = HttpLoggingInterceptor()
if (BuildConfig.DEBUG)
logging.level = HttpLoggingInterceptor.Level.BODY
else
logging.level = HttpLoggingInterceptor.Level.NONE
if (retrofit == null) {
retrofit = Retrofit.Builder()
.client(OkHttpClient().newBuilder().readTimeout(120, TimeUnit.SECONDS)
.connectTimeout(120, TimeUnit.SECONDS).retryOnConnectionFailure(false)
.dispatcher(
dispatcher
).addInterceptor(Interceptor { chain: Interceptor.Chain? ->
val newRequest = chain?.request()!!.newBuilder()
return#Interceptor chain.proceed(newRequest.build())
}).addInterceptor(logging).build()
)
.baseUrl(baseUrl)
.addConverterFactory(GsonConverterFactory.create())
.build()
}
return retrofit
}
}
ApiClient will be used to initialize Retrofit singleton object, also initialize logging interceptors so you can keep track of the requests and responses in the logcat by using the keyword 'okhttp'.
SingleEnqueueCall.kt
object SingleEnqueueCall {
var retryCount = 0
lateinit var snackbar: Snackbar
fun <T> callRetrofit(
activity: Activity,
call: Call<T>,
apiName: String,
isLoaderShown: Boolean,
apiListener: IGenericCallBack
) {
snackbar = Snackbar.make(
activity.findViewById(android.R.id.content),
Constants.CONST_NO_INTERNET_CONNECTION, Snackbar.LENGTH_INDEFINITE
)
if (isLoaderShown)
activity.showAppLoader()
snackbar.dismiss()
call.enqueue(object : Callback<T> {
override fun onResponse(call: Call<T>, response: Response<T>) {
hideAppLoader()
if (response.isSuccessful) {
retryCount = 0
apiListener.success(apiName, response.body())
} else {
when {
response.errorBody() != null -> try {
val json = JSONObject(response.errorBody()!!.string())
Log.e("TEGD", "JSON==> " + response.errorBody())
Log.e("TEGD", "Response Code==> " + response.code())
val error = json.get("message") as String
apiListener.failure(apiName, error)
} catch (e: Exception) {
e.printStackTrace()
Log.e("TGED", "JSON==> " + e.message)
Log.e("TGED", "Response Code==> " + response.code())
apiListener.failure(apiName, Constants.CONST_SERVER_NOT_RESPONDING)
}
else -> {
apiListener.failure(apiName, Constants.CONST_SERVER_NOT_RESPONDING)
return
}
}
}
}
override fun onFailure(call: Call<T>, t: Throwable) {
hideAppLoader()
val callBack = this
if (t.message != "Canceled") {
Log.e("TGED", "Fail==> " + t.localizedMessage)
if (t is UnknownHostException || t is IOException) {
snackbar.setAction("Retry") {
snackbar.dismiss()
enqueueWithRetry(activity, call, callBack, isLoaderShown)
}
snackbar.show()
apiListener.failure(apiName, Constants.CONST_NO_INTERNET_CONNECTION)
} else {
retryCount = 0
apiListener.failure(apiName, t.toString())
}
} else {
retryCount = 0
}
}
})
}
fun <T> enqueueWithRetry(
activity: Activity,
call: Call<T>,
callback: Callback<T>,
isLoaderShown: Boolean
) {
activity.showAppLoader()
call.clone().enqueue(callback)
}
}
SingleEnqueueCall will be used for calling the retrofit, it is quite versatile, written with onFailure() functions and by passing Call to it, we can call an API along with ApiName parameter so this function can be used for any possible calls and by ApiName, we can distinguish in the response that which API the result came from.
Constants.kt
object Constants {
const val CONST_NO_INTERNET_CONNECTION = "Please check your internet
connection"
const val CONST_SERVER_NOT_RESPONDING = "Server not responding!
Please try again later"
const val USER_REGISTER = "/api/User/register"
}
ApiInterface.kt
interface ApiInterface {
companion object {
const val BASE_URL = "URL_LINK"
}
#POST(Constants.USER_REGISTER)
fun userRegister(#Body userRegisterRequest: UserRegisterRequest):
Call<UserRegisterResponse>
}
UserRegisterRequest.kt
data class UserRegisterRequest(
val Email: String,
val Password: String
)
UserRegisterResponse.kt
data class UserRegisterResponse(
val Message: String,
val Code: Int
)
IGenericCallBack.kt
interface IGenericCallBack {
fun success(apiName: String, response: Any?)
fun failure(apiName: String, message: String?)
}
MyApplication.kt
class MyApplication : Application() {
companion object {
lateinit var apiService: ApiInterface
}
override fun onCreate() {
super.onCreate()
apiService = ApiClient.getClient()!!.create(ApiInterface::class.java)
}
}
MyApplication is the application class to initialize Retrofit at the launch of the app.
AndroidManifest.xml
android:name=".MyApplication"
You have to write above tag in AndroidManifest inside Application tag.
MainActivity.kt
class MainActivity : AppCompatActivity(), IGenericCallBack {
private lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
val call = MyApplication.apiService.userRegister(UserRegisterRequest(email, password))
SingleEnqueueCall.callRetrofit(this, call, Constants.USER_REGISTER, true, this)
}
override fun success(apiName: String, response: Any?) {
val model = response as UserRegisterResponse
}
override fun failure(apiName: String, message: String?) {
if (message != null) {
showToastMessage(message)
}
}
}
Firstly, we create a call object by using the API defined in ApiInterface and passing the parameters (if any). Then using SingleEnqueueCall, we pass the call to the retrofit along with ApiName and the interface listener IGenericCallBack by using this. Remember to implement it to respective activity or fragment as above.
Secondly, you will have the response of the API whether in success() or failure() function overriden by IGenericCallBack
P.S: You can differentiate which API got the response by using the ApiName parameter inside success() function.
override fun success(apiName: String, response: Any?) {
when(ApiName) {
Constants.USER_REGISTER -> {
val model = response as UserRegisterResponse
}
}
}
The whole concept is to focus on reusability, now every API call has to create a call variable by using the API's inside ApiInterface then call that API by SingleEnqueueCall and get the response inside success() or failure() functions.
I'm making cryptocurrency information viewer using Retrofit. Everything is okay, but have a problem.
One exchange demands me execute retrofit twice.
https://api.upbit.com/v1/market/all
https://api.upbit.com/v1/ticker?markets=KRW-BTC,KRW-ETH,KRW-BTG,KRW-BSV,KRW-ETC ..., KRW-(Some cryptocurrency)
Base url is "https://api.upbit.com/v1/". And I have to get 'markets' from first api, and get cryptocurrency's information by using 'markets' as query value.
But there are problem. I thought first and second execution. Get markets from first execution, and get infomations by using markets. But Retrofit basically runs asynchronously, So usually skip first execution and there are no markets in second executions. (markets = "")
How can I run Retrofit in Retrofit? Did I approach wrong? Or there are solution for this problem?
This is Retrofit parts in my code.
var markets = ""
val publishSubject: PublishSubject<String> = PublishSubject.create()
init {
publishSubject.subscribe {
markets = it
}
}
fun getData(exchange: String) {
// var markets = "Basic"
val url = when(exchange) {
coinone -> "https://api.coinone.co.kr/"
bithumb -> "https://api.bithumb.com/"
upbit -> {
getMarketsUpbit()
"https://api.upbit.com/v1/"
}
else -> "https://api-cloud.huobi.co.kr/"
}
val parser = DataParser()
val builder = Retrofit.Builder()
.baseUrl(url)
.addConverterFactory(GsonConverterFactory.create())
.build()
.create(RetrofitService::class.java)
val call: retrofit2.Call<Any> =
when (exchange) {
coinone ->
builder.getTickersCoinone("all")
bithumb ->
builder.getTickersBithumb()
upbit ->
builder.getTickersUpbit(markets)
else ->
builder.getTickersHuobi()
}
call.enqueue(object : retrofit2.Callback<Any> {
override fun onResponse(call: retrofit2.Call<Any>, response: retrofit2.Response<Any>) {
coinInfos.value = parser.getParsedData(
if (exchange != "upbit") exchange
else markets
,
response.body().toString()
)
}
override fun onFailure(call: retrofit2.Call<Any>, t: Throwable) {
println("Retrofit process is failed.")
}
})
}
private fun getMarketsUpbit() {
val parser = DataParser()
var markets = ""
val builder = Retrofit.Builder()
.baseUrl("https://api.upbit.com/v1/")
.addConverterFactory(GsonConverterFactory.create())
.build()
.create(RetrofitService::class.java)
val call: retrofit2.Call<Any> = builder.getMarketsUpbit()
call.enqueue(object : retrofit2.Callback<Any> {
override fun onResponse(call: retrofit2.Call<Any>, response: retrofit2.Response<Any>) {
publishSubject.onNext(parser.parseUpbitMarkets(response.body().toString()))
}
override fun onFailure(call: retrofit2.Call<Any>, t: Throwable) {
println("Retrofit process is failed.")
}
})
}
I'm trying to implement application, which will work with websocket. So I choose scarlet. I can see in logs a response from a server, but I cant consume a data in my viewModel. How to do that? I am using Koin + viewModel + coroutine
Module for Koin
val networkModule = module {
single { createScarlet() }
single <ChatSocketRepository> {
ChatSocketRepositoryImpl(get())
}
}
private fun createScarlet(): ChatSocketApi {
val client = OkHttpClient.Builder()
.readTimeout(DataProviderImplementation.TIMEOUT, TimeUnit.SECONDS)
.writeTimeout(DataProviderImplementation.TIMEOUT, TimeUnit.SECONDS)
.connectTimeout(DataProviderImplementation.TIMEOUT, TimeUnit.SECONDS)
.addInterceptor(HttpLoggingInterceptor().apply {
level = HttpLoggingInterceptor.Level.BODY
})
.build()
return Scarlet.Builder()
.webSocketFactory(client.newWebSocketFactory("wss://demos.kaazing.com/echo"))
.addMessageAdapterFactory(GsonMessageAdapter.Factory())
.addStreamAdapterFactory(CoroutinesStreamAdapterFactory())
.build()
.create()
}
ChatSocketApi
interface ChatSocketApi {
#Receive
fun observeText(): ReceiveChannel<String>
}
ChatSocketRepository
interface ChatSocketRepository {
fun observeTest(): ReceiveChannel<String>
}
ChatSocketRepositoryImpl:
class ChatSocketRepositoryImpl(private val api: ChatSocketApi) : ChatSocketRepository {
override fun observeTest(): ReceiveChannel<String> {
return api.observeText()
}
}
ViewModel
class MyViewModel(private val chatSocketRepository: ChatSocketRepository) : BaseViewModel() {
init {
viewModelScope.launch {
val text = chatSocketRepository.observeTest().consumeEach {
Log.d("SOCKET", it.toString())
}
}
}
you already get the data, and your data is String because you declare ReceiveChannel<String> you can use the received data and put it in a LiveData. change the value of livedata in the .consumeEach{} method instead.
I am trying to write an Android Chess client using websockets. I chose the okhttp3 library. I can make a successful connection and can send data and receive. However, I am not sure how to return the data to LiveData for the ViewModel. I am somewhat familiar with Kotlin coroutines but I am not sure how I would get the data out of the listener.
I have tried trying to return from the function but as it is an overridden function I cannot return from it.
Here is the current WebSocketListener:
class EchoWebSocketListener : WebSocketListener() {
private val NORMAL_CLOSURE_STATUS = 1000
override fun onOpen(webSocket: WebSocket, response: Response) {
super.onOpen(webSocket, response)
webSocket.send("Hello It is me")
webSocket.send("test 3!")
}
override fun onMessage(webSocket: WebSocket, text: String){
super.onMessage(webSocket, text)
outputData("Receiving $text")
}
override fun onMessage(webSocket: WebSocket, bytes: ByteString) {
super.onMessage(webSocket, bytes)
outputData("Receiving bytes : " + bytes.hex())
}
override fun onClosing(webSocket: WebSocket, code: Int, reason: String) {
super.onClosing(webSocket, code, reason)
outputData("$code $reason")
}
private fun outputData(outputString: String) {
d("web socket", outputString)
}
}
And here is the setup code in the repository
fun startChat() {
httpClient = OkHttpClient()
val request = Request.Builder()
.url("ws://echo.websocket.org")
.build()
val listener = EchoWebSocketListener()
val webSocket = httpClient.newWebSocket(request, listener)
//webSocket.
httpClient.dispatcher.executorService.shutdown()
}
I would like to be able to run the repository with a Kotlin coroutine and return LiveData for the fragment to consume.
In your EchoWebSocketistener you could create a private MutableLiveData like so
class EchoWebSocketListener : WebSocketListener() {
private val _liveData = MutableLiveData<String>()
val liveData: LiveData<String> get() = _liveData
// Overridden methods
private fun outputData(string: String) {
_liveData.postValue(string)
}
}
Then you return the live data from the listener like so in a Coroutine
fun startChat(): LiveData<String> {
val listener = EchoWebSocketListener()
GlobalScope.launch(Dispatchers.IO) {
httpClient = OkHttpClient()
val request = Request.Builder()
.url("ws://echo.websocket.org")
.build()
val webSocket = httpClient.newWebSocket(request, listener)
//webSocket.
httpClient.dispatcher.executorService.shutdown()
}
return listener.liveData
}
fun loadTweets(uid: Int) : Observable<ArrayList<Tweet>> {
var tweets: ArrayList<Tweet> = ArrayList<Tweet>()
val builder: Retrofit.Builder = Retrofit.Builder()
.baseUrl(NetworkContract.SERVER_URL)
.addConverterFactory(GsonConverterFactory.create())
val retrofit: Retrofit = builder.build()
val userService: UserService = retrofit.create(UserService::class.java)
val call: Call<List<Tweet>> = userService.loadTweets(uid)
call.enqueue(object: Callback<List<Tweet>>{
override fun onResponse(call: Call<List<Tweet>>?, response: Response<List<Tweet>>?) {
if(response!!.isSuccessful){
val tweetsTemp: ArrayList<Tweet> = ArrayList<Tweet>()
tweetsTemp.add(Tweet("Test!", "Tom"))
mTweets = tweetsTemp
}
}
override fun onFailure(call: Call<List<Tweet>>?, t: Throwable?) {
}
})
return Observable.just(this.mTweets)
}
Hey guys, I want to create a Twitter Clone but I can't return the changed mTweets variable. I get a response from my server and in the onResponse method mTweets is changed but my function does return an empty string.(Btw. tweetsTemp is just for testing. I will change this later).
You can't return mTweets because the network call is running asynchronically.
Read more about synchronous and asynchronous requests here: https://futurestud.io/tutorials/retrofit-synchronous-and-asynchronous-requests