How it works...

There are a few magical things happening here. Let's start with the annotations that we put into the MockPublisherRepositoryTests class:

  • The @SpringBootTest annotation's webEnvironment attribute was replaced with WebEnvironment.NONE. This is to inform Spring Boot that we don't want a full application web server to be initialized for this test, since we will only be interacting with the repository object, without making calls to controllers or using any part of the WebMvc stack. We did this to save test startup time, and if one is curious to see the difference, just simply switching it back to the WebEnvironment.RANDOM_PORT value and rerunning the test would show that the time has almost doubled. (On my beefy MacBook Pro, it increased from 5 seconds to almost 9.)

With the application changes examined, let's now look at what we did in the MockPublisherRepositoryTests class itself:

  • The @MockBean annotation instructs Spring that this dependency is not a real instance, but a mock object currently backed by the Mockito framework. This has an interesting effect in that it actually replaces our PublisherRepository bean instance in the context with the mock one, so, everywhere within the context, all dependencies for PublisherRepository get wired with the mocked version instead of a real, database-backed one.

Now that we know how the mocked instance of PublisherRepository gets injected into our tests, let's take a look at the newly-created test setup methods. The two methods of particular interest are setupPublisherRepositoryMock() and resetPublisherRepositoryMock(). They are described as follows:

  • The setupPublisherRepositoryMock() method is annotated with @Before, which tells JUnit to execute this method before running every @Test method in the class. We will use the Mockito framework in order to configure the behavior of our mocked instance. We configure it such, that when the repository.count() method is called, it will return 5 as a result. The Mockito, Junit, and Hamcrest libraries provide us with many convenient DLS-like methods, which we can use to define such rules with an English-like, easy-to-read style.
  • The resetPublisherRepositoryMock() method is annotated with @After, which tells JUnit to execute this method after running every @Test method in the class. At the end of every test, we will need to reset the mocked behavior, so we will use the reset(...) method call to clear out all of our settings and get the mock ready for the next test, which can be used in another test suite altogether.
Ideally, there is no need to reset the mock object at the end of the test run, as each test class gets its own context spawned up, so between test classes the instance of a mock is not shared. It is considered good practice to create many smaller tests instead of a single large one. There are, however, some use cases that warrant resetting the mock when it is being managed by a container injection, so I thought it was worth mentioning. For the best practices on using reset(...), see https://github.com/mockito/mockito/wiki/FAQ#can-i-reset-a-mock.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.17.46