Mocking frameworks are good for system testing, mocking these system dependencies; you would not use a fake framework for the layout or stub add if you are testing add . Let's break it down a bit further:
Testing add
The mocking framework is not suitable for testing your add method above. There are no dependencies except the very stable and extremely well tested JVM and JRE.
public int add(int a, int b) {return a+b}
However, it may be useful to check your add method if it should interact with another object as follows:
public int add(int a, int b, AdditionLogger additionLogger) { int total = a + b; additionLogger.log(a, b, total); return total; }
If AdditionLogger is not yet written, or if it was written to communicate with a real server or other external process, then a fake structure will be absolutely useful: it will help you come up with a fake implementation of AdditionLogger so that you can check your real interactions with it.
@Test public void yourTest() { assertEquals(5, yourObject.add(2, 3, mockAdditionLogger)); verify(mockAdditionLogger).log(2, 3, 5); }
Testing add consumers
By the way, the mocking structure is also hardly good for testing the consumers of your method above. After all, there is nothing particularly dangerous about calling add , so assuming it exists, you can probably name the real one in an external test. 2 + 3 will always be equal to 5, and there will be no side effects from your calculations, so very little can be obtained through ridicule or verification.
However, give your object another method that adds two numbers with a bit of random noise:
public int addWithNoise(int a, int b) { int offset = new Random().nextInt(11) - 5;
However, it can be very difficult for you to write a reliable assert style test against this method; in the end, the result will be somewhat random! Instead, it is easier to run the assert style test, perhaps we can drown out addWithNoise to make some of these more predictable.
@Test public void yourTest() { when(yourObjectMock.addWithNoise(2, 3)).thenReturn(6);
Finally
It is easier to explain the mocking and syntax layout when interacting with well-known operations like add or with well-known interfaces like List , but these examples are usually not realistic cases when mocks are needed. Remember that bullying is really only useful for modeling dependencies around your system test when you cannot use real ones.