Skip to main content

Posts

Showing posts with the label Mockito

A simple way to mock objects without using mock unit testing framework

When writing unit test, there are some cases that I have to mock objects: It makes sense to mock provided objects by libraries (APIs) such as FacesContext (JSF) because of no real environment running. It makes sense to mock a lower layer objects and it is already tested, for example: mocking Dao layer objects when testing Service layer (Service calls Dao) . At beginning I was aware of Mockito (a mocking framework) in order to overcome the issue. And currently, I am interested in another way like an alternative because it looks more simple. That is just create mock objects manually and just do anything we want. I've just known this approach from Primefaces' source code. :) Follow my simple example below and we can see what different from these 2 ways are: I have an interface Foo and a class Bar public interface Foo { String greet(); } public class Bar { public String greet(Foo foo){ return foo.greet(); } } Using Mockito example: public class M