2

I'm trying to test my AWS Lambda function but I can't figure out how to mock the 2.0 SDK with Mockito. Basically, all I want is to create a couple DBSnapshot mocks and set a bit of test information on them (snapshot name and creation time would be enough for my purposes).

If I make a new DBSnapshot with the 'new' operator, I can't seem to set any parameters on it, or even mock a builder with a request to create one.

In the 1.0 SDK, I could mock a DBSnapshot and set various ".withBlah' params like below:

DBSnapshot testSnapshot = new DBSnapshot().withSnapshotCreateTime("2020-01-01")[...]

but it doesn't seem possible here since the 2.0 rewrite to force everything through a builder, and I'm not sure how to mock it now. Googling hasn't turned up any code examples for the 2.0 SDK/RDS in particular.

Any ideas?

Rome_Leader
  • 2,518
  • 9
  • 42
  • 73

1 Answers1

1

Try this:

DBSnapshot testSnapshot = DBSnapshot
    .builder()
    .snapshotCreateTime(Instant.now())
    .build();
madhead
  • 31,729
  • 16
  • 153
  • 201
  • If I wanted to truly mock it though, I guess I would first need to mock the builder somehow? I know this is in line with the example I gave above, but it still represents actually reaching out to the AWS API. – Rome_Leader Jun 13 '20 at 21:51
  • 1
    @Rome_Leader, no need to mocking the builder in order to mock its subject. Just mock the `DBSnapshot`. It's a final class, so there may be some nuances in doing so. It really depends on the mocking framework you use. It's pretty easy to do in mockito: [Mock Final Classes and Methods with Mockito](https://www.baeldung.com/mockito-final#final-class). – madhead Jun 13 '20 at 21:55