dimanche 2 août 2015

Stubing methods

I have an HTTP request generator and i would like to create unit tests that avoid performing the actual calls (mostly to validate the request structure)
My class is something like

class Requestor {        
    def get (params : Map[String, String] = {
        process("GET", params)
    }
    def post(params : Map[String, String[ = {
        process("POST", params)
    }
    private def process(s: String, p : Map[String, String]) = {
        val res = createRequestAndExec(s, p)
        doStuffToReposnse(res)
    }
    private createRequestAndExec(s: String, p : Map[String, String]) = {
       // create apache HTTPBaseRequest
       ..
       // execute request using apache DefaultHttpClient
    }
}

can I somehow stubbing this method ? If I to use MockFactory (if i understand it correctly) I should create a trait to be mocked which will look like

trait A {
  def post ..
  def get ...
  def createRequestAndExec ...
}

And mock A but createRequestAndExec shouldn't be a part of the public API ..

Aucun commentaire:

Enregistrer un commentaire