How to mock a Scala singleton object?

后端 未结 4 416
我在风中等你
我在风中等你 2020-12-09 03:53

I am trying to mock a Scala singleton object. In particular, I need to mock the object play.api.libs.ws.WS used inside a service component (class under test). U

相关标签:
4条回答
  • 2020-12-09 04:34

    Why you don't use the cake pattern?

    It's a little verbose but it will solve your mocking problem.

    trait GeolocationServiceComponent {
       val geolocationService:GeolocationService
       trait GeolocationService {      
          def wsClient
          def getPath(origin:Location, destination: Location): Future[Route]   
       }
    }
    
    trait GeolocationServiceComponentImpl {
       val geolocationService = new GeolocationService {
          override def wsClient = WS
       }
    }
    
    
    class DefaultGeolocationService extends GeolocationServiceComponent ...
    
    //Defined into your test class
    trait MockGeolocationServiceComponent {
           val geolocationService = Mock[GeolocationService]
    //Here you define your mock logic       
    }
    

    You can also use monads to do this, but I have never implemented, it's described here: Scala dependency injection: alternatives to implicit parameters

    0 讨论(0)
  • 2020-12-09 04:39

    I don't like to change the design to accommodate this kind of limitation. basically what I can get is all to change the design and then use the mock framework

      //since mocking frameworks on SCALA is not support mocking singleton
      //hacks are either required to change the design or ...
      //I'd rather putting the mocking logic here by myself
      var testUser:Option[User] = None;
    
      def getCurrentUser(request: Request[Object]) = {
        if( testUser != None ){
        testUser;
      }
    

    hope it helps.

    0 讨论(0)
  • 2020-12-09 04:44

    Mocking singleton objects using ScalaMock 3 is not possible, however Paul Butcher expects to reintroduce this feature in ScalaMock 4 (see http://paulbutcher.com/2014/04/15/scalamock-status-report/)

    0 讨论(0)
  • 2020-12-09 04:44

    Don't mock the singleton. Instead of WS, make your service component depend on a thin facade hiding it:

    trait GeolocationService {
      def ws: (String, Seq[String]) => Promise[Response] = { (url, params) =>
        wsClient.url(serviceProviderEndpoint.get).withQueryString(params: _*).get()
      }
      def getPath(origin: Location, destination: Location): Future[Route]
    }
    

    and in your test, just override ws method with a mock, which is now easy to create:

    val mockedWs = mock[(String, Seq[String]) => Promise[Response]]
    // TODO specify mock's behavior here
    val service = new DefaultGeolocationService() {
      override def ws = mockedWs
    }
    
    0 讨论(0)
提交回复
热议问题