Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: extracting interfaces just for testing

I want to test my code but I am noticing that some business logic classes demand on alot of objects with alot of state. But actually the communication between these classes is limited to 1 or 2 methods.

Is it good style if I create an interface with these 2 methods and in my test just create a mock object which does nothing?

The code will then have alot more interfaces which are not really needed, that's why I'm not sure if it's a good idea?

like image 330
Franz Kafka Avatar asked Jan 09 '12 11:01

Franz Kafka


1 Answers

Although having interfaces is a good idea, you don't need interfaces to do mocking. EasyMock, Mockito and PowerMock all allow mocking of concrete classes. So given that, you can leave the code as is and use Mockito (my personal favorite) to mock the injected classes.

FYI, the mocks shouldn't "does nothing". They should be tested with each possible return value and each possible exception thrown.

like image 80
John B Avatar answered Oct 15 '22 05:10

John B