# How to stub an interface in test-case?

  1. Include scalamock library dependency i.e. "org.scalamock" %% "scalamock-scalatest-support" in the project's custom.sbt

  2. Extend the test-suite to implement MockFactory interface.

  3. grab the stub using keyword stub




     



    import org.scalamock.scalatest.MockFactory
    
    class CreateCommandTestSuite extends FunSuite with SmilePerTest with MockFactory {
      val lotteryDomainStub = stub[LotteryDomainLogic]
    	...
    }
    
  4. set the Binder for the test-case by binding the stub to the testcase name

     




    setBinderFor("create first lottery", bind[LotteryDomainLogic].toInstance(lotteryDomainStub))
      test("create first lottery", Tag("create"), ptest) {
      ...
      }
    
  5. Inside the testcase provide the stub implementation:

    For example, in order to make the api isOpenToRun return true when you pass lottery name as "TestLottery" :


     



    test("create first lottery", Tag("create"), ptest) {
        (lotteryDomainStub.isOpenToRun _).when("TestLottery").returns(true)
        ...
    }
    

    Note: You can also use wildcards like * inside when