Junit 5 with Spring Boot: When to use @ExtendWith Spring or Mockito?

We Are Going To Discuss About Junit 5 with Spring Boot: When to use @ExtendWith Spring or Mockito?. So lets Start this Java Article.

Junit 5 with Spring Boot: When to use @ExtendWith Spring or Mockito?

  1. Junit 5 with Spring Boot: When to use @ExtendWith Spring or Mockito?

    As it can be seen , SpringExtension implements a lot more extensions than MockitoExtension.
    Also @SpringBootTest is meta annotated with @ExtendWith(SpringExtension.class) and which means every time your tests are extended with SpringExtension.
    @MockBean is a Spring test framework annotation and used along with @ExtendWith(SpringExtension.class)

  2. Junit 5 with Spring Boot: When to use @ExtendWith Spring or Mockito?

    As it can be seen , SpringExtension implements a lot more extensions than MockitoExtension.
    Also @SpringBootTest is meta annotated with @ExtendWith(SpringExtension.class) and which means every time your tests are extended with SpringExtension.
    @MockBean is a Spring test framework annotation and used along with @ExtendWith(SpringExtension.class)

Solution 1

What is a Junit Extension

The purpose of Junit 5 extensions is to extend the behavior of test classes or methods

source

Read on Junit 5 Extension Model & @ExtendWith annotation :here

SpringExtension

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

public class SpringExtension
extends Object
implements BeforeAllCallback, AfterAllCallback, TestInstancePostProcessor, BeforeEachCallback, AfterEachCallback, BeforeTestExecutionCallback, AfterTestExecutionCallback, ParameterResolver{..}

MockitoExtension

This extension is the JUnit Jupiter equivalent of our JUnit4
MockitoJUnitRunner

public class MockitoExtension
extends java.lang.Object
implements BeforeEachCallback, AfterEachCallback, ParameterResolver{..}

As it can be seen , SpringExtension implements a lot more extensions than MockitoExtension.

Also @SpringBootTest is meta annotated with @ExtendWith(SpringExtension.class) and which means every time your tests are extended with SpringExtension.
@MockBean is a Spring test framework annotation and used along with @ExtendWith(SpringExtension.class)

To observe the difference try the following

ExtendWith only MockitoExtension

@ExtendWith(MockitoExtension.class)
class TestServiceTest {

    @MockBean
    TestService service;

    @Test
    void test() {
        assertNotNull(service); // Test will fail
    }

}

ExtendWith only SpringExtension

@ExtendWith(SpringExtension.class)
class TestServiceTest {

    @MockBean
    TestService service;

    @Test
    void test() {
        assertNotNull(service); // Test succeeds
    }

}

ExtendWith with both SpringExtension and MockitoExtension

@ExtendWith(MockitoExtension.class)
@ExtendWith(SpringExtension.class)
class TestServiceTest {

    @MockBean
    TestService service;

    @Test
    void test() {
        assertNotNull(service); // Test succeeds
    }

}

Both works in your case because of the @SpringBootTest annotation for the test class as explained.

To answer the question : When to use @ExtendWith Spring or Mockito? ,

When the test requires a Spring Test Context ( to autowire a bean / use of @MockBean ) along with JUnit 5’s Jupiter programming model use @ExtendWith(SpringExtension.class). This will support Mockito annotations as well through TestExecutionListeners.

When the test uses Mockito and needs JUnit 5’s Jupiter programming model support use @ExtendWith(MockitoExtension.class)

Hope this helps

Advertisements

Original Author Of This Content

Solution 2

When to use @ExtendWith(SpringExtension.class) or @SpringBootTest?

  • When you use Integration test [email protected] annotation- or any slice test [email protected] annotations- you don’t need @ExtendWith(SpringExtension.class) annotation since mentioned annotations include it.

  • If you test @ConfigurationProperties, @Service, @Component annotated class ( not defined in slice test cases – ref:Spring Boot Reference Document Testing/Auto-configured / SLICED Tests item-, you may use @ExtendWith(SpringExtension.class) instead of @SpringBootTest.

Observation: I expect that a test with @ExtendWith(SpringExtension.class) faster than the same test with @SpringBootTest.When I perform a test in Eclipse i observed the reverse.

Advertisements

Original Author Of This Content

Conclusion

Advertisements

So This is all About This Tutorial. Hope This Tutorial Helped You. Thank You.

Also Read,

Siddharth

I am an Information Technology Engineer. I have Completed my MCA And I have 4 Year Plus Experience, I am a web developer with knowledge of multiple back-end platforms Like PHP, Node.js, Python and frontend JavaScript frameworks Like Angular, React, and Vue.

Leave a Comment