Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between @ExtendWith(SpringExtension.class) and @ExtendWith(MockitoExtension.class)?

I was using @RunWith(MockitoJUnitRunner.class) for my junit test with mockito. But now I am working with spring-boot and JUnit 5.
What's the difference between the two annotations ?
Can I use only @ExtendWith(SpringExtension.class) to mock my objects ?

like image 648
user11705123 Avatar asked Feb 19 '20 20:02

user11705123


People also ask

What is @ExtendWith @RunWith and their difference?

@RunWith is an old annotation from JUnit 4 to use test runners. If you're using JUnit 5 (Jupiter), you should use @ExtendWith to use JUnit extensions.

What is the use of @ExtendWith SpringExtension class?

Class SpringExtension. 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.

What is the use of @ExtendWith?

@ExtendWith is a repeatable annotation that is used to register extensions for the annotated test class, test interface, test method, parameter, or field.

What is the use of @extendwith in spring test framework?

If you want to use Spring test framework features in your tests like for example @MockBean, then you have to use @ExtendWith (SpringExtension.class). It replaces the deprecated JUnit4 @RunWith (SpringJUnit4ClassRunner.class)

What is the difference between mockitoextension and springextension?

public class MockitoExtension extends java.lang.Object implements BeforeEachCallback, AfterEachCallback, ParameterResolver {..} As it can be seen , SpringExtension implements a lot more extensions than MockitoExtension.

When to use @extendwith (mockitoextension)?

When the test uses Mockito and needs JUnit 5's Jupiter programming model support use @ExtendWith (MockitoExtension.class) Show activity on this post.

When to use @extendwith vs @mockbean In JUnit?

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.


1 Answers

When involving Spring:

If you want to use Spring test framework features in your tests like for example @MockBean, then you have to use @ExtendWith(SpringExtension.class). It replaces the deprecated JUnit4 @RunWith(SpringJUnit4ClassRunner.class)

When NOT involving Spring:

If you just want to involve Mockito and don't have to involve Spring, for example, when you just want to use the @Mock / @InjectMocks annotations, then you want to use @ExtendWith(MockitoExtension.class), as it doesn't load in a bunch of unneeded Spring stuff. It replaces the deprecated JUnit4 @RunWith(MockitoJUnitRunner.class).

To answer your question:

Yes you can just use @ExtendWith(SpringExtension.class), but if you're not involving Spring test framework features in your tests, then you probably want to just use @ExtendWith(MockitoExtension.class).

like image 115
Troley Avatar answered Oct 23 '22 07:10

Troley