hishamss
hishamss

Reputation: 51

Junit 5: Difference between @SpringBootTest and @ExtendWith(SpringExtension.class)

Testing with Junit5, Can I use only @ExtendWith(SpringExtension.class) without @SpringBootTest ? Some resources say that @SpringBootTest already include @ExtendWith(SpringExtension.class), is this correct ? When I run the test with @ExtendWith(SpringExtension.class) only, I notice that the test is very light and doesn't load the entire application context, but I'm not sure if I don't use @SpringBootTest my unit test is considered complete or correct ?

Upvotes: 5

Views: 2547

Answers (1)

Timur Celikel
Timur Celikel

Reputation: 133

Yes, you can definitely use @ExtendWith(SpringExtension.class) without loading the entire application context that is included with @SpringBootTest. As you say it's a lighter weight approach if you just want to mock beans with @MockBean. @SpringBootTest does in fact include @ExtendWith(SpringExtension.class) but it may be overkill for what you are trying to achieve.

Upvotes: 1

Related Questions