0

What exactly SpringExtension do here? Even without that, the test case executes below as expected.

As per the doc doc

SpringExtension integrates the Spring TestContext Framework into JUnit 5's Jupiter programming model.

To use this extension, simply annotate a JUnit Jupiter based test class with @ExtendWith(SpringExtension.class), @SpringJUnitConfig, or @SpringJUnitWebConfig.

Unit Test with SpringExtension

@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = SellCarService.class)
class SellCarServiceTest {

    @Autowired
    private SellCarService carService;

    @Test
    public void testGetCarId() {
        String actual = carService.getCarDetails("1234");
        String expected = "PRIUS";
        Assertions.assertEquals(expected, actual);
    }
}

Simple Unit Test

class SellCarServiceTest {

    private final SellCarService carService = new SellCarService();

    @Test
    public void testGetCarId() {
        String actual = carService.getCarDetails("1234");
        String expected = "PRIUS";
        Assertions.assertEquals(expected, actual);
    }
}

If we want to use @Autowired in test classes, then we can go for the first approach or else we can use the simple approach as mentioned later. Is this understanding correct?

Or what is the advantage we get if we use SpringExtension or MockitoExtension?

saravanan
  • 151
  • 2
  • 15
  • This [article](https://rieckpil.de/what-the-heck-is-the-springextension-used-for/) may help to understand the usages of this JUnit Jupiter extension – rieckpil Apr 04 '22 at 04:28

1 Answers1

1

You get no advantage of using the SpringExtension in this test class as you are not using any Mockito mocks.

You add @ExtendWith(SpringExtension.class) to the test class when you want to use the Mockito annotations such as @Mock, @Mockbean etc.

You are correct regarding @Autowired, there are also other ways to make autowiring work such as using the @SpringBootTest annotation.

Johan Nordlinder
  • 1,672
  • 1
  • 13
  • 17
  • So only in Mocking use case, SrpringExtension will be useful. How SpringExtension differs from MockitoExtension ? When to use both? – saravanan Apr 03 '22 at 14:06
  • See this question: https://stackoverflow.com/questions/60308578/what-is-the-difference-between-extendwithspringextension-class-and-extendwit – Johan Nordlinder Apr 03 '22 at 14:49
  • please share me any other doc about SpringExtension if any.thank you. – saravanan Apr 03 '22 at 15:14
  • https://docs.spring.io/spring-framework/docs/current/reference/html/testing.html#testcontext-junit-jupiter-extension – Johan Nordlinder Apr 03 '22 at 15:21
  • There's a big difference between [@Mock and @MockBean](https://rieckpil.de/difference-between-mock-and-mockbean-spring-boot-applications/). You don't need the `SpringExtension` for `@Mock` and rather the `MockitoExtension`. – rieckpil Apr 04 '22 at 04:30
  • Yes but SpringExtension works for both `@Mock` and `@Mockbean`, see the first question I linked to, even though it's overkill for simple `@Mock` where the MockitoExtension is sufficient. – Johan Nordlinder Apr 04 '22 at 04:42