I have a very basic question, I'm new to unit testing and I have a test class in which I'm testing Spring WebClient using MockWebServer. All the tests run fine but why don't we need a runner when using MockWebServer? When using Mockito we annotate the class with @RunWith(MockitoJUnitRunner.class).
A JUnit 4 Runner
or Rule
or JUnit Jupiter Extension
conveniently takes care (among other things) of the test setup, e.g. starting and stopping the MockWebServer
for you in the background.
You don't have to use a Runner
/Rule
or Extension
for your tests and can start/stop your test infrastructure manually as part of @BeforeAll
/@AfterAll
or inside the test.
However, this adds boilerplate code to every test that requires this kind of infrastructure and you can delegate this task to a Runner
/Rule
or Extension
if your test library provides one.
In the particular example for MockWebServer
, you can use their JUnit 4 rule, their JUnit 5 integration, or perform the start/stop operations for the server on your own.