Brief Introduction of Mockito

Review images

Let’s start with the following example:

Review images

As you can see from the figure above, if we want to test A, we need to build the entire dependency tree, which is an instance of BCDE.

An alternative is to use mocks:

Review images

It is clear from the figure that mock objects are used as substitutes for real objects during debugging. A mock test is a method of replacing a test with a dummy object for an object that is not easy to build.

Now that you know what a mock test is, let’s take a look at the mock framework — Mockito.

Mockito is a powerful simulation testing framework for Java development based on MIT protocol. Others include EasyMock, JMock, MockCreator, Mockrunner, MockMaker, and PowerMock.

Mockito differs from other simulation frameworks primarily in allowing developers to verify the behavior of the system under test without establishing “expectations.” One comment on mock objects is that the test code for the test system is highly coupled, with a decrease in coupling as Mockito tries to remove the expect-run-verify pattern by removing the “expectation specification.” The result is simplified test code, making it easier to read and modify.

Mock key points

When it comes to mocks, there are only three things you need to care about: setting up test data, setting up expected results, and verifying results. Some unit test scenarios don’t involve this at all, some involve setting up test data, and some involve setting up expected results and validation.

Stubbing 

Stubbing is telling fake what course of action to perform when interacting with it. Often it can be used to provide public properties (like getters and Setters) and public methods that those tests need.

When it comes to stubbing methods, you usually have a range of options. Perhaps you want to return a particular value, throw an error, or fire an event. In addition, you might want to indicate how the method behaves when it is called (that is, by passing matching types or arguments to the method).

This sounds like a lot of work at first, but it usually isn’t. One of the most important features of many mocking frameworks is that you do not need to provide stub entity methods or stub unused methods or attributes during test execution.

Set expectations

A key feature of Fake is that you can tell it what you expect when you run mock tests with it. For example, you can require a particular function to be called exactly three times, or not at all, or at least twice but no more than five times, or to satisfy certain types of arguments, certain values, and any combination of the above. The possibilities are endless.

Tell fake what you expect to happen by setting desired results. Because it was a mock test, nothing really happened. However, for the class being tested, it does not distinguish this case. So fake can call the function and let it do what it does.

It is worth noting that most simulation frameworks can create mock tests for public classes in addition to mock tests for interfaces.

Verify expected results

Setting expectations and verifying expectations go hand in hand. The setting expectation is done before the test class’s function is called, and the validation expectation is done after it. So first you set your expectations, and then you verify that your expectations are correct.

In a unit test, if the expectations you set are not met, the unit test has failed. For example, if you set the expected result to be that the iloginservice.login function must be called once with a specific username and password, but in the test it was not called, and the fake was not authenticated, so the test failed.

The benefits of mocks

Create tests ahead of time; TDD (Test Driven Development)

That’s the biggest benefit. If you create a Mock, you can write service Tests before the service interface is created so that you can add Tests to your automated test environment during development. In other words, simulation enables you to use test-driven development.

Teams can work in parallel

This is similar to the point up here; Create tests for code that doesn’t exist. But whereas developers write tests, here the test team creates them. How can a test team create tests when there’s nothing to test yet? Simulation and test against simulation! This means that the QA team already has a complete set of test components in place when the Service needs to be tested. There is no one team waiting for the other team to finish. This makes simulation particularly useful.

You can create a validation or demo program

Because Mocks is so efficient, Mocks can be used to create a proof of concept, as a schematic, or as a demo of a project you are considering building. This gives you a strong basis for deciding whether or not to proceed with the project, but most importantly provides you with practical design decisions.

Write tests for inaccessible resources

This benefit is not a practical benefit, but as a “life buoy” when necessary. Have you ever come across such a situation? When you want to test a service interface, but the service needs to be accessed through a firewall, the firewall cannot be opened for you or you need authentication to access it. When this happens, you can use MockService where you can access it instead, which is a “life buoy” feature.

Mocks can be distributed to users

In some cases, for some reason you need to allow outside sources to access your test system, such as partners or customers. For these reasons, your sensitive information can be accessed by others, and you may only want to allow access to part of the test environment. In this case, how to provide a test system to a partner or customer for development or testing? The easiest thing to do is provide a mock, whether it’s from your network or your client’s network. SoapUI Mock is easy to configure and can be run on soapUI or published to your Java server as a WAR package.

Isolation system

Sometimes, you want to test a single part of the system without the influence of the rest of the system. Other system parts will interfere with the test data and affect the test conclusion based on data collection. Using mocks you can remove the simulation of system dependencies that need to be tested. When these mocks are isolated, mocks become very simple, reliable, and fast and predictable. This gives you a test environment that removes random behavior, has repetitive patterns, and can monitor specific systems.

The development of resources

Official website: mockito.org/ open source address: github.com/mockito/moc…