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?