title: How To Testing (Unit and Instrumented) of Okhttp
body: <p>I'm doing self-learning of android (mostly), and I'm can't grasp a concept about Testing. I tried googling and youtube but still don't get it, so I really need a sample to test my code below</p>
  
  <p>Could anyone show me how to create unit test request data to server from this code?</p>
  
  <pre><code>    fun fetchJSON() {
  
      val url =baseurl + prevnext  + idliga
  
      val request = Request.Builder().url(url).build()
  
      val client = OkHttpClient()
  
      client.newCall(request).enqueue(object: Callback {
          override fun onFailure(call: Call, e: IOException) {
  
          }
  
          override fun onResponse(call: Call, response: Response) {
              val body = response.body()?.string()
              val gson = GsonBuilder().create()
  
              val DataMatch= gson.fromJson(body, DataPertandingan::class.java)
  
              runOnUiThread {
  
                   rvPrevMatch.adapter= PrevAdapter(DataMatch)
              }
          }
      })
  
  }
  </code></pre>
  
  <p>And about the instrumented test, how do I test adding something to SQLite.</p>
  
  <p>here is my activity code of adding data to SQLite.</p>
  
  <pre><code>private fun addToFavorite() {
      try {
          database.use {
              insert(
                  Favorite.DATA_FAVORITE,
                 Favorite.ID_EVENT to id_event,
                  Favorite.DATE to tanggaltandingdet.text,
  
                  // home team
                 Favorite.HOME_ID to idhome,
                  Favorite.HOME_TEAM to timkandangdet.text,
                  Favorite.HOME_SCORE to skorkandangdet.text,
                  Favorite.HOME_GOAL_DETAILS to cetakgolkandang.text,
                  Favorite.HOME_LINEUP_GOALKEEPER to kiperkandang.text,
                  Favorite.HOME_LINEUP_DEFENSE to bekkandang.text,
                  Favorite.HOME_LINEUP_MIDFIELD to midkandang.text,
                  Favorite.HOME_LINEUP_FORWARD to strikerkandang.text,
                  Favorite.HOME_LINEUP_SUBSTITUTES to cadangankandang.text,
              //    Favorite.HOME_TEAM_BADGE to urllogokandang.text,
  
                  // away team
                  Favorite.AWAY_ID to idaway,
                  Favorite.AWAY_TEAM to timtandangdet.text,
                  Favorite.AWAY_SCORE to skortandangdet.text,
                  Favorite.AWAY_GOAL_DETAILS to cetakgoltandang.text,
                  Favorite.AWAY_LINEUP_GOALKEEPER to kipertandang.text,
                  Favorite.AWAY_LINEUP_DEFENSE to bektandang.text,
                  Favorite.AWAY_LINEUP_MIDFIELD to midtandang.text,
                  Favorite.AWAY_LINEUP_FORWARD to strikertandang.text,
                  Favorite.AWAY_LINEUP_SUBSTITUTES to cadangantandang.text
              //    Favorite.AWAY_TEAM_BADGE to urllogotandang.text
  
              )
          }
  
          toast ("Data Telah Di Simpan"  )
      } catch (e: SQLiteConstraintException) {
          toast("Error: ${e.message}")
      }
  }
  </code></pre>