Skip to content

Commit decb53b

Browse files
committed
Document JUnit 5 before JUnit 4
Closes gh-864
1 parent 16a096f commit decb53b

File tree

1 file changed

+44
-43
lines changed

1 file changed

+44
-43
lines changed

Diff for: docs/src/docs/asciidoc/getting-started.adoc

+44-43
Original file line numberDiff line numberDiff line change
@@ -201,24 +201,33 @@ It then produces documentation snippets for the request and the resulting respon
201201
==== Setting up Your Tests
202202

203203
Exactly how you set up your tests depends on the test framework that you use.
204-
Spring REST Docs provides first-class support for JUnit 4 and JUnit 5.
204+
Spring REST Docs provides first-class support for JUnit 5 and JUnit 4.
205+
JUnit 5 is recommended.
205206
Other frameworks, such as TestNG, are also supported, although slightly more setup is required.
206207

207208

208209

209-
[[getting-started-documentation-snippets-setup-junit]]
210-
===== Setting up Your JUnit 4 Tests
210+
[[getting-started-documentation-snippets-setup-junit-5]]
211+
===== Setting up Your JUnit 5 Tests
211212

212-
When using JUnit 4, the first step in generating documentation snippets is to declare a `public` `JUnitRestDocumentation` field that is annotated as a JUnit `@Rule`.
213+
When using JUnit 5, the first step in generating documentation snippets is to apply the `RestDocumentationExtension` to your test class.
213214
The following example shows how to do so:
214215

215216
[source,java,indent=0]
216217
----
217-
@Rule
218-
public JUnitRestDocumentation restDocumentation = new JUnitRestDocumentation();
218+
@ExtendWith(RestDocumentationExtension.class)
219+
public class JUnit5ExampleTests {
219220
----
220221

221-
By default, the `JUnitRestDocumentation` rule is automatically configured with an output directory based on your project's build tool:
222+
When testing a typical Spring application, you should also apply the `SpringExtension`:
223+
224+
[source,java,indent=0]
225+
----
226+
@ExtendWith({RestDocumentationExtension.class, SpringExtension.class})
227+
public class JUnit5ExampleTests {
228+
----
229+
230+
The `RestDocumentationExtension` is automatically configured with an output directory based on your project's build tool:
222231

223232
[cols="2,5"]
224233
|===
@@ -231,38 +240,42 @@ By default, the `JUnitRestDocumentation` rule is automatically configured with a
231240
| `build/generated-snippets`
232241
|===
233242

234-
You can override the default by providing an output directory when you create the `JUnitRestDocumentation` instance.
243+
If you are using JUnit 5.1, you can override the default by registering the extension as a field in your test class and providing an output directory when creating it.
235244
The following example shows how to do so:
236245

237246
[source,java,indent=0]
238247
----
239-
@Rule
240-
public JUnitRestDocumentation restDocumentation = new JUnitRestDocumentation("custom");
248+
public class JUnit5ExampleTests {
249+
250+
@RegisterExtension
251+
final RestDocumentationExtension restDocumentation = new RestDocumentationExtension ("custom");
252+
253+
}
241254
----
242255

243-
Next, you must provide an `@Before` method to configure MockMvc or WebTestClient, or REST Assured.
244-
The following examples show how to do so:
256+
Next, you must provide a `@BeforeEach` method to configure MockMvc or WebTestClient, or REST Assured.
257+
The following listings show how to do so:
245258

246259
[source,java,indent=0,role="primary"]
247260
.MockMvc
248261
----
249-
include::{examples-dir}/com/example/mockmvc/ExampleApplicationTests.java[tags=setup]
262+
include::{examples-dir}/com/example/mockmvc/ExampleApplicationJUnit5Tests.java[tags=setup]
250263
----
251264
<1> The `MockMvc` instance is configured by using a `MockMvcRestDocumentationConfigurer`.
252265
You can obtain an instance of this class from the static `documentationConfiguration()` method on `org.springframework.restdocs.mockmvc.MockMvcRestDocumentation`.
253266

254267
[source,java,indent=0,role="secondary"]
255268
.WebTestClient
256269
----
257-
include::{examples-dir}/com/example/webtestclient/ExampleApplicationTests.java[tags=setup]
270+
include::{examples-dir}/com/example/webtestclient/ExampleApplicationJUnit5Tests.java[tags=setup]
258271
----
259-
<1> The `WebTestClient` instance is configured by adding a `WebTestclientRestDocumentationConfigurer` as an `ExchangeFilterFunction`.
272+
<1> The `WebTestClient` instance is configured by adding a `WebTestClientRestDocumentationConfigurer` as an `ExchangeFilterFunction`.
260273
You can obtain an instance of this class from the static `documentationConfiguration()` method on `org.springframework.restdocs.webtestclient.WebTestClientRestDocumentation`.
261274

262275
[source,java,indent=0,role="secondary"]
263276
.REST Assured
264277
----
265-
include::{examples-dir}/com/example/restassured/ExampleApplicationTests.java[tags=setup]
278+
include::{examples-dir}/com/example/restassured/ExampleApplicationJUnit5Tests.java[tags=setup]
266279
----
267280
<1> REST Assured is configured by adding a `RestAssuredRestDocumentationConfigurer` as a `Filter`.
268281
You can obtain an instance of this class from the static `documentationConfiguration()` method on `RestAssuredRestDocumentation` in the `org.springframework.restdocs.restassured` package.
@@ -272,27 +285,19 @@ See the <<configuration, configuration section>> for more information.
272285

273286

274287

275-
[[getting-started-documentation-snippets-setup-junit-5]]
276-
===== Setting up Your JUnit 5 Tests
288+
[[getting-started-documentation-snippets-setup-junit]]
289+
===== Setting up Your JUnit 4 Tests
277290

278-
When using JUnit 5, the first step in generating documentation snippets is to apply the `RestDocumentationExtension` to your test class.
291+
When using JUnit 4, the first step in generating documentation snippets is to declare a `public` `JUnitRestDocumentation` field that is annotated as a JUnit `@Rule`.
279292
The following example shows how to do so:
280293

281294
[source,java,indent=0]
282295
----
283-
@ExtendWith(RestDocumentationExtension.class)
284-
public class JUnit5ExampleTests {
285-
----
286-
287-
When testing a typical Spring application, you should also apply the `SpringExtension`:
288-
289-
[source,java,indent=0]
290-
----
291-
@ExtendWith({RestDocumentationExtension.class, SpringExtension.class})
292-
public class JUnit5ExampleTests {
296+
@Rule
297+
public JUnitRestDocumentation restDocumentation = new JUnitRestDocumentation();
293298
----
294299

295-
The `RestDocumentationExtension` is automatically configured with an output directory based on your project's build tool:
300+
By default, the `JUnitRestDocumentation` rule is automatically configured with an output directory based on your project's build tool:
296301

297302
[cols="2,5"]
298303
|===
@@ -305,42 +310,38 @@ The `RestDocumentationExtension` is automatically configured with an output dire
305310
| `build/generated-snippets`
306311
|===
307312

308-
If you are using JUnit 5.1, you can override the default by registering the extension as a field in your test class and providing an output directory when creating it.
313+
You can override the default by providing an output directory when you create the `JUnitRestDocumentation` instance.
309314
The following example shows how to do so:
310315

311316
[source,java,indent=0]
312317
----
313-
public class JUnit5ExampleTests {
314-
315-
@RegisterExtension
316-
final RestDocumentationExtension restDocumentation = new RestDocumentationExtension ("custom");
317-
318-
}
318+
@Rule
319+
public JUnitRestDocumentation restDocumentation = new JUnitRestDocumentation("custom");
319320
----
320321

321-
Next, you must provide a `@BeforeEach` method to configure MockMvc or WebTestClient, or REST Assured.
322-
The following listings show how to do so:
322+
Next, you must provide an `@Before` method to configure MockMvc or WebTestClient, or REST Assured.
323+
The following examples show how to do so:
323324

324325
[source,java,indent=0,role="primary"]
325326
.MockMvc
326327
----
327-
include::{examples-dir}/com/example/mockmvc/ExampleApplicationJUnit5Tests.java[tags=setup]
328+
include::{examples-dir}/com/example/mockmvc/ExampleApplicationTests.java[tags=setup]
328329
----
329330
<1> The `MockMvc` instance is configured by using a `MockMvcRestDocumentationConfigurer`.
330331
You can obtain an instance of this class from the static `documentationConfiguration()` method on `org.springframework.restdocs.mockmvc.MockMvcRestDocumentation`.
331332

332333
[source,java,indent=0,role="secondary"]
333334
.WebTestClient
334335
----
335-
include::{examples-dir}/com/example/webtestclient/ExampleApplicationJUnit5Tests.java[tags=setup]
336+
include::{examples-dir}/com/example/webtestclient/ExampleApplicationTests.java[tags=setup]
336337
----
337-
<1> The `WebTestClient` instance is configured by adding a `WebTestClientRestDocumentationConfigurer` as an `ExchangeFilterFunction`.
338+
<1> The `WebTestClient` instance is configured by adding a `WebTestclientRestDocumentationConfigurer` as an `ExchangeFilterFunction`.
338339
You can obtain an instance of this class from the static `documentationConfiguration()` method on `org.springframework.restdocs.webtestclient.WebTestClientRestDocumentation`.
339340

340341
[source,java,indent=0,role="secondary"]
341342
.REST Assured
342343
----
343-
include::{examples-dir}/com/example/restassured/ExampleApplicationJUnit5Tests.java[tags=setup]
344+
include::{examples-dir}/com/example/restassured/ExampleApplicationTests.java[tags=setup]
344345
----
345346
<1> REST Assured is configured by adding a `RestAssuredRestDocumentationConfigurer` as a `Filter`.
346347
You can obtain an instance of this class from the static `documentationConfiguration()` method on `RestAssuredRestDocumentation` in the `org.springframework.restdocs.restassured` package.

0 commit comments

Comments
 (0)