如何测试API请求并用假数据填充类



我在Internet上找不到解决此问题的解决方案。还是我的代码很糟糕?

接口

interface GetWeatherService {
    @GET("/data/2.5/forecast?")
    fun getAllWeather(@Query("q")cityName: String, @Query("APPID")app_id: String, @Query("units")units: String="imperial"): Call<ListWeather>
    @GET("/data/2.5/weather?")
    fun getCurrentWeather(@Query("q")cityName: String, @Query("APPID")app_id: String, @Query("units")units: String="imperial"): Call<MainWeather>
    @GET("/data/2.5/weather?")
    fun getWeatherDataFromLocation(@Query("lat")lat: String, @Query("lon")lon: String, @Query("APPID") app_id: String): Call<DataFromLocation>
}

客户端

object RetrofitClientInstance {
    private var retrofit: Retrofit? = null
    private var BASE_URL = "https://api.openweathermap.org/"
    val retrofitInstance: Retrofit?
        get(){
            if(retrofit == null){
                retrofit = retrofit2.Retrofit.Builder()
                    .baseUrl(BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build()
            }
            return retrofit
        }
}

MainActivity((

class MainActivity: AppCompatActivity {
  fun getDataFromApi(cityName: String) {
        val service = RetrofitClientInstance.retrofitInstance!!.create(GetWeatherService::class.java)
        // Service 1
        service.getCurrentWeather(cityName, APP_ID).enqueue(object: Callback<MainWeather>{
            override fun onFailure(call: Call<MainWeather>, t: Throwable) {
            }
            override fun onResponse(call: Call<MainWeather>, response: Response<MainWeather>) {
                val weather = response.body()
                val currentWeather = CurrentWeather(
                    weather!!.main.temp,
                    weather.weather[0].description,
                    weather.name,
                    weather.weather[0].main
                )
                updateCurrentWeatherUI(currentWeather)
            }
        })
        service.getAllWeather(cityName, APP_ID).enqueue(object: Callback<ListWeather>{
            override fun onFailure(call: Call<ListWeather>, t: Throwable) {
            }
            override fun onResponse(call: Call<ListWeather>, response: Response<ListWeather>) {
                val weather = response.body()!!.list
                for(item in weather){
                    val weatherList = NextFiveDayWeather(
                        item.dt,
                        item.main.temp,
                        item.weather[0].description
                    )
                    weatherArray.add(weatherList)
                    updateUI(weatherArray)
                }
            }
        })
    }
}

数据类

data class MainWeather (
    var dt: Long,
    var main: MainDTO,
    var weather: List<WeatherDTO>,
    var dt_txt: String,
    var name: String
)
data class WeatherDTO (var main: String, var description: String, var icon: String)
data class ListWeather (@SerializedName("list") var list: List<MainWeather>)

我无法测试对API的请求,我无法用虚假数据填写课程?请告诉我,我该怎么办?我应该学习什么?如果您不困难,可以为未来提供建议?

您可以下载软件邮递员来测试API端点:https://www.getpostman.com/

最新更新