Search code examples
javaspring-bootunit-testingspring-mvcmockito

Where I use @ExtendsWith(MockitoExtension.class) and @SpringBootTest anyone clarify me with example


I'm beginner in Spring boot till now I have complete the REST Operations(GET,POST,PUT,DELETE) with MYSQL as a database and I want to write the unit test cases for Controller class and Service class I use Mockito framework for writing test cases and I don't know when I use.

I'm stuck with

  1. where I use @SpringBootTest and @ExtendsWith(MockitoExtension.class) ?
  2. @SpringBootTest itself enough?

Solution

  • @SpringBootTest used to integrate test environment, enables you to test controllers, services, other repositories.

    if you're writing unit tests for your controller and service classes using Mockito, you'll typically use @ExtendWith(MockitoExtension.class) to enable Mockito and then mock any dependencies of the class you're testing.