How to use scalamock for the awssdk

230 Views Asked by At

I have a function that has the following type signature:

def post(target: String, partition: String, payload: String): IO[Boolean]

In this function, a call is placed to a third party to post this data to a service through the awssdk. For testing, I don't want these calls actually placed, which has brought me to scalamock, but am not sure how to do this in looking at the documentation.

Within the post function I have leverage my private instance of aws's client for this service after setting credentials. There is a particular method on this instance that is responsible for the call to aws.

How can scalamock be leveraged to mock the awssdk to test the code I have surrounding this and not make the actual call?

1

There are 1 best solutions below

3
Thilo On

Do you have a trait that defines this method? Then you can use Scalamock to get an instance of it and make it respond to post calls with whatever value you want it to.

https://scastie.scala-lang.org/KgJ0kptnRaiteNTWPBzrow

trait MyService {
  def post(target: String, partition: String, payload: String): Future[Boolean]
}

  
val mockedService = mock[MyService]
  
(mockedService.post _)
  .expects("a", "b","c")
  .returning(Future.successful(true))
  .once()

mockedService.post("a", "b", "c")