Mock VS Stub

I believe the biggest distinction is that a stub you have already written with predetermined behavior. So you would have a class that implements the dependency (abstract class or interface most likely) you are faking for testing purposes and the methods would just be stubbed out with set responses. They wouldn’t do anything fancy and you would have already written the stubbed code for it outside of your test.
A mock is something that as part of your test you have to setup with your expectations. A mock is not setup in a predetermined way so you have code that does it in your test. Mocks in a way are determined at runtime since the code that sets the expectations has to run before they do anything.
Tests written with mocks usually follow an initialize -> set expectations -> exercise -> verify pattern to testing. While the pre-written stub would follow an initialize -> exercise -> verify. The purpose of both is to eliminate testing all the dependencies of a class or function so your tests are more focused and simpler in what they are trying to prove.

Mocks vs Stubs = Behavioral testing vs State testing
According to the principle of Test only one thing per test, there may be several stubs in one test, but generally there is only one mock.
Test lifecycle with stubs:
Setup – Prepare object that is being tested and its stubs collaborators.
Exercise – Test the functionality.
Verify state – Use asserts to check object’s state.
Teardown – Clean up resources.
Test lifecycle with mocks:
Setup data – Prepare object that is being tested.
Setup expectations – Prepare expectations in mock that is being used by primary object.
Exercise – Test the functionality.
Verify expectations – Verify that correct methods has been invoked in mock.
Verify state – Use asserts to check object’s state.
Teardown – Clean up resources.
Both mocks and stubs testing give an answer for the question: What is the result?
Testing with mocks are also interested in: How the result has been achieved?

Stub is simple fake object. It just makes sure test runs smoothly.
Mock is smarter stub. You verify Your test passes through it.

For replacing a method with code that returns a specified result.
A stub with an assertion that the method gets called.

Stubs don’t fail your tests, mock can.

Stub is like making sure a method returns the correct value
Mock is like actually stepping into the method and making sure everything inside is correct before returning the correct value.

A Stub is an object that implements an interface of a component, but instead of returning what the component would return when called, the stub can be configured to return a value that suits the test. Using stubs a unit test can test if a unit can handle various return values from its collaborator. Using a stub instead of a real collaborator in a unit test could be expressed like this:
unit test –> stub
unit test –> unit –> stub
unit test asserts on results and state of unit
First the unit test creates the stub and configures its return values. Then the unit test creates the unit and sets the stub on it. Now the unit test calls the unit which in turn calls the stub. Finally the unit test makes assertions about the results of the method calls on the unit.
A Mock is like a stub, only it also has methods that make it possible determine what methods where called on the Mock. Using a mock it is thus possible to both test if the unit can handle various return values correctly, and also if the unit uses the collaborator correctly. For instance, you cannot see by the value returned from a dao object whether the data was read from the database using a Statement or a PreparedStatement. Nor can you see if the connection.close() method was called before returning the value. This is possible with mocks. In other words, mocks makes it possible to test a units complete interaction with a collaborator. Not just the collaborator methods that return values used by the unit. Using a mock in a unit test could be expressed like this:
unit test –> mock
unit test –> unit –> mock
unit test asserts on result and state of unit
unit test asserts on the methods called on mock

Every class or object created is a Fake. It is a Mock if you verify calls against it. Otherwise its a stub.

Stub: a dummy piece of code that lets the test run, but you don’t care what happens to it.
Mock: a dummy piece of code, that you VERIFY is called correctly as part of the test.

According to

Mock VS Stub

Unit Test Terms

The “system under test” is short for “whatever thing we are testing” and is always defined from the perspective of the test. When we are writing unit tests the system under test (SUT) is whatever class (a.k.a. CUT), object (a.k.a. OUT) or method(s) (a.k.a. MUT) we are testing; when we are writing customer tests, the SUT is probably the entire application (a.k.a. AUT) or at least a major subsystem of it. The parts of the application that we are not verifying in this particular test may still be involved as a depended-on component (DOC).
The term SUT means also a stage of maturity of the software, because a system test is the successor of integration test in the testing cycle.

There are very few classes that operate entirely in isolation. Usually they need other classes or objects in order to function, whether injected via the constructor or passed in as method parameters. These are known as Collaborators.

Test Double
The generic term he uses is a Test Double (think stunt double). Test Double is a generic term for any case where you replace a production object for testing purposes.
There are five types of TDs, which are dummy, fake, mock, stub, spy.

It is used when a parameter is needed for the tested method but without actually needing to use the parameter.
Dummy objects are passed around but never actually used. Usually they are just used to fill parameter lists.
It’s not intended to be used in your tests and will have no effect on the behaviour.

It is used for providing the tested code with “indirect input”.
Stubs provide canned answers to calls made during the test, usually not responding at all to anything outside what’s programmed in for the test.
Think of a Stub as a step up from a Dummy. It implements a required Interface, but instead of missing method bodies, it usually returns canned responses in order for you to make assertions on the output of the SUT.

It is used for verifying “indirect output” of the tested code, by asserting the expectations afterwards, without having defined the expectations before the tested code is executed.
Spies are stubs that also record some information based on how they were called. One form of this might be an email service that records how many messages it was sent.
A Stub could also be used to maintain state, and make assertions, when a Stub does this it is also known as a Test Spy.

It is used as a simpler implementation, e.g. using an in-memory database in the tests instead of doing real database access.
Fake objects actually have working implementations, but usually take some shortcut which makes them not suitable for production (an InMemoryTestDatabase is a good example).
The simplest way to think of a Fake is as a step up from a Stub. This means not only does it return values, but it also works just as a real Collaborator would.

It is used for verifying “indirect output” of the tested code, by first defining the expectations before the tested code is executed.
Mocks are pre-programmed with expectations which form a specification of the calls they are expected to receive. They can throw an exception if they receive a call they don’t expect and are checked during verification to ensure they got all the calls they were expecting.
Whilst all of the other types of Test Double build on each other, gradually adding more functionality, Mocks are something totally different.
Until now, all of our test doubles have made assertions on state. Mocks are test doubles that make assertions on behaviour.
Typically, using a Mock will consist of three phases: Creating the instance, Defining the behaviour, Asserting the calls.

Classical vs Mockist
There are two schools of thought on Test Doubles: Classical, and Mockist. Put simply, people who use the Classical style only use Test Doubles when there is a compelling reason to do so, such as external dependencies, whilst a Mockist would mock everything, all the time.
The advantage of using the Classical style is that you don’t have to worry about internal behaviour of your objects, which is what OOP is all about. When you change an object and it’s collaborators your tests will still pass, but this comes at the sacrifice of speed and isolation.
The advantage of using the Mockist style is that your tests are isolated and can run much faster, but at the sacrifice of coupling your tests to the internal behaviour of your objects and their collaborators.



Unit Test Terms

brew install nginx with error: cannot run C compiled programs

After upgrading Mac OS X to Sierra, I tried to ‘brew install nginx’, however an error occurred unexpectedly.

nginx checking whether we are cross compiling... configure: error: in `/private/tmp/makedepend--xproto-20161031-51743-16mag9r/xproto-7.0.25': configure: error: cannot run C compiled programs.

Firstly appearing around my head is this is caused by Sierra incompatibility, I tried to get solution from Google, but got nothing. Next I put attention on brew, reinstalled brew by using official code as below:

ruby -e "$(curl -fsSL

After that, I ran ‘brew install nginx’ again, it didn’t work and prompted ‘Warning: xcode is outdated…’, and I found xcode got crashed then, suddenly I realized that it may be caused by xcode. So I got rid of xcode and nginx was installed successfully.

However, I got another tiny issue after nginx was installed as shown below.

xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools), missing xcrun at: /Library/Developer/CommandLineTools/usr/bin/xcrun

It could be fixed by following code

xcode-select --install

To sum up, I did wrong to Sierra, it was xcode’s fault.

brew install nginx with error: cannot run C compiled programs