21

I have worked with JUnit and Mocks but I'm wondering, what are the differences between Mocks and Stubs in JUnit and how to use Stubs in JUnit, Java? And as Mocks that have EasyMock, Mockito and so on, what does Stubs uses in Java?

Please give some example code for Stubs in Java.

halfer
  • 19,824
  • 17
  • 99
  • 186
Wooopsa
  • 320
  • 1
  • 6
  • 22

3 Answers3

44

It doesn't matter the framework or technology in my opinion. Mocks and stubs could be defined as follows.

A stub is a controllable replacement for an existing dependency (or collaborator) in the system. By using a stub, you can test your code without dealing with the dependency directly.

A mock object is a fake object in the system that decides whether the unit test has passed or failed. It does so by verifying whether the object under test interacted as expected with the fake object.

Perhaps these images can clarify the interactions between a stub and a mock.

Stub Stub

Mock Mock

Michael Baker
  • 3,338
  • 26
  • 29
28

To use stubs in junit you don't need any frameworks.

If you want to stub some interface just implement it:

interface Service {
    String doSomething();
}

class ServiceStub implements Service {
    public String doSomething(){
        return "my stubbed return";
    }
}

Then create new stub object and inject it to tested object.

If you want to stub a concrete class, create subclass and override stubbed methods:

class Service {
    public String doSomething(){
        // interact with external service
        // make some heavy computation
        return "real result";
    }
}

class ServiceStub extends Service {
    @Override
    public String doSomething(){
        return "stubbed result";
    }
}
halfer
  • 19,824
  • 17
  • 99
  • 186
Sergii Lagutin
  • 10,561
  • 1
  • 34
  • 43
  • you mean i just write some code again, but this time more simple and write a testcase for that? an interface, a class and a testclass? Thats it? – Wooopsa Aug 08 '15 at 08:06
  • 1
    @Hamid yes, you're right. If you wanna use mock, I suppose some classes need to interact with external services or databases. To avoid these interactions you may write stub with easier implementations than original classes. – Sergii Lagutin Aug 08 '15 at 08:08
  • would you mind give me an example on that? How it is easier? – Wooopsa Aug 08 '15 at 08:09
  • @Hamid because you may write few lines to implement stub class. When you write unit tests you are focused on target class and don't wanna test target class's dependencies. So you should understand why you want to use mocks, stubs etc. – Sergii Lagutin Aug 08 '15 at 08:13
  • to reduce work to implement the stubs you still can use a library. https://anystub-guides.github.io/ has classes to stub http traffic and db-access – feech May 06 '19 at 08:51
1

In general - Mock means implement some behavior, stubs - just supply some data. in other words preferable use the word mock when you need to demonstrate that it changes/keeps some state

use the word stub when your classes only expose the internal state. indeed you can use mock everywhere, and stub is just subset of mock

feech
  • 404
  • 4
  • 15