DEV Community

loading...

Discussion on: Why Java Interfaces Are Terrible

Collapse
goaty92 profile image
goaty92

I can list a hundred things Java that are terrible, but this one certainly isn't. If your Database class expects to work with AmazonDynamoDB interface (that is, one that has 60+ methods in it), then it makes sense to not pass in a reduced interface.

If you just want different implementations for production and mock testing, I believe you can just straight mock the interface AmazonDynamoDB itself. Most mocking frameworks are capable of this.

Collapse
forstmeier profile image
John Forstmeier Author

The reason for the reduced interface was just because I would only need those two methods. Is there a better way to get make this happen in Java? Also, is there a framework you'd recommend? I looked at Mockito but didn't find good examples for what I was trying to do.

Collapse
goaty92 profile image
goaty92

If Database only needs to work with those two method then the Java way to do is to write an implementation class for DynamoImpl which is a wrapper of your production AmazonDynamoDB, but only exposes those two methods publicly.

Mockito is a good framework that will probably have most of the features you'd want. What are you trying to do specificly? In Mockito you can do things like "create a (mock) object that implement this interface, but when method A() is called return some fake data" or stuffs like that.

Thread Thread
forstmeier profile image
John Forstmeier Author

Yes, Database only needs those two methods (right now, if I expand it later, I can add those methods); ideally, I want to be able to hit the "put" and "get" methods within the Database class without actually touching the production DynamoDB (which it wouldn't be able to do from a local run anyway without additional configuration).