Spring Boot Mockito Support: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
 
(10 intermediate revisions by the same user not shown)
Line 5: Line 5:
=Internal=
=Internal=
* [[Spring_Boot_Testing_Concepts#Spring_Boot_Mockito_Support|Spring Boot Testing Concepts]]
* [[Spring_Boot_Testing_Concepts#Spring_Boot_Mockito_Support|Spring Boot Testing Concepts]]
* [[Mockito]]


=Overview=
=Overview=


Spring Boot has built-in Mockito support and no additional dependencies are required in the Gradle <code>build.gradle</code> file to use Mockito mocks. This article describes the minimal amount of steps to use Mockito mocks in tests, alongside whatever components and real JPA repositories the application uses. Note that a H2-based JPA repository is better than a Mockito mock.
Spring Boot has built-in Mockito support and no additional dependencies are required in the Gradle <code>build.gradle</code> file to use Mockito mocks. This article describes the minimal amount of steps to inject Mockito mocks in tests, alongside whatever components and real JPA repositories the application uses. Note that a H2-based JPA repository is better than a Mockito mock, because it tests the real persistence logic.


=<tt>@MockBean</tt>=


The <code>org.springframework.boot.test.mock.mockito.MockBean</code> annotation injects Mockito mocks built around whatever component is specified in the test. Using <tt>@MockBean</tt> is the key to easily using mocks from Spring Boot unit tests.


[[@MockBean]]
Note that mocks will be automatically reset across tests.


Mocks will be automatically reset across tests.
==Dependencies==
 
=Dependencies=
<syntaxhighlight lang='groovy'>
<syntaxhighlight lang='groovy'>
dependencies {
dependencies {
   ...
   ...
   testImplementation 'org.springframework.boot:spring-boot-starter-test'
   testImplementation 'org.springframework.boot:spring-boot-starter-test' // no special Mockito dependency is necessary
  testImplementation 'org.mockito:mockito-core:3.12.4'
}
}
</syntaxhighlight>
</syntaxhighlight>
=Testing with <tt>@MockBean</tt>=


=Example=
Annotate the test with <code>@SpringBootTest</code>. This will automatically allow injections of all real components from the application, '''including''' the JPA repositories. This is important, because a H2-based JPA repository is better than a mock.


<syntaxhighlight lang='java'>
<syntaxhighlight lang='java'>
@RunWith(SpringRunner.class)
@SpringBootTest
@SpringBootTest
public class SomeTest {
public class SomeControllerTest {
 
    @Autowired
    private TestRestTemplate restTemplate;


    @MockBean
  //
    private SomeService someService;
  // this is the real component to test
  //
  @Autowired
  private SomeController someController


    @Before
  //
    public void setup() {
  // SpringBoot will automatically wrap the component in a Mockito proxy
  // and the mock component will be injected into the tested component
  //
  @MockBean
  private SomeDependencyComponent someDependencyComponent;


        // this stubs behavior
  @Before
        given(someService.getSomething("blah")).willReturn(new Something("blah"));
  public void configureMocks() {
    }
      //
      // if mock configuration is shared among tests, the configuration can be done in a
      // @Before method; the mocks will be reset after each test
      //
      when(someDependencyComponent.someMethod()).thenReturn("something");
  }


    @Test
  @Test
    public void test() {
  public void test() {
      //
      // configure the mock in each test, the mocks will be reset after each test
      //
      when(someDependencyComponent.someOtherMethod()).thenReturn("something else");


        restTemplate.getForEntity("/{username}/something, String.class, "blue");
      //
    }
      // test
      //
      result = someController.someMethod(...);
      assertEquals(..., result.someStuff());
  }
}
}
</syntaxhighlight>
</syntaxhighlight>

Latest revision as of 02:39, 3 October 2021

External

Internal

Overview

Spring Boot has built-in Mockito support and no additional dependencies are required in the Gradle build.gradle file to use Mockito mocks. This article describes the minimal amount of steps to inject Mockito mocks in tests, alongside whatever components and real JPA repositories the application uses. Note that a H2-based JPA repository is better than a Mockito mock, because it tests the real persistence logic.

@MockBean

The org.springframework.boot.test.mock.mockito.MockBean annotation injects Mockito mocks built around whatever component is specified in the test. Using @MockBean is the key to easily using mocks from Spring Boot unit tests.

Note that mocks will be automatically reset across tests.

Dependencies

dependencies {
  ...
  testImplementation 'org.springframework.boot:spring-boot-starter-test' // no special Mockito dependency is necessary
}

Testing with @MockBean

Annotate the test with @SpringBootTest. This will automatically allow injections of all real components from the application, including the JPA repositories. This is important, because a H2-based JPA repository is better than a mock.

@SpringBootTest
public class SomeControllerTest {

   //
   // this is the real component to test
   // 
   @Autowired
   private SomeController someController

   //
   // SpringBoot will automatically wrap the component in a Mockito proxy
   // and the mock component will be injected into the tested component
   // 
   @MockBean
   private SomeDependencyComponent someDependencyComponent;

   @Before
   public void configureMocks() {
      //
      // if mock configuration is shared among tests, the configuration can be done in a 
      // @Before method; the mocks will be reset after each test
      //
      when(someDependencyComponent.someMethod()).thenReturn("something");
   }

   @Test
   public void test() {
      //
      // configure the mock in each test, the mocks will be reset after each test
      //
      when(someDependencyComponent.someOtherMethod()).thenReturn("something else");

      //
      // test
      // 
      result = someController.someMethod(...);
      assertEquals(..., result.someStuff());
   }
}