Skip to content

Commit b82451e

Browse files
committed
Remove support for JUnit 4
Closes gh-958
1 parent c7bde71 commit b82451e

32 files changed

+244
-554
lines changed

docs/build.gradle

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ dependencies {
1818
testImplementation(project(":spring-restdocs-webtestclient"))
1919
testImplementation("jakarta.servlet:jakarta.servlet-api")
2020
testImplementation("jakarta.validation:jakarta.validation-api")
21-
testImplementation("junit:junit")
2221
testImplementation("org.testng:testng:6.9.10")
2322
testImplementation("org.junit.jupiter:junit-jupiter-api")
2423
}

docs/src/docs/asciidoc/getting-started.adoc

Lines changed: 7 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -203,8 +203,7 @@ It then produces documentation snippets for the request and the resulting respon
203203
==== Setting up Your Tests
204204

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

210209

@@ -258,72 +257,6 @@ public class JUnit5ExampleTests {
258257
Next, you must provide a `@BeforeEach` method to configure MockMvc or WebTestClient, or REST Assured.
259258
The following listings show how to do so:
260259

261-
[source,java,indent=0,role="primary"]
262-
.MockMvc
263-
----
264-
include::{examples-dir}/com/example/mockmvc/ExampleApplicationJUnit5Tests.java[tags=setup]
265-
----
266-
<1> The `MockMvc` instance is configured by using a `MockMvcRestDocumentationConfigurer`.
267-
You can obtain an instance of this class from the static `documentationConfiguration()` method on `org.springframework.restdocs.mockmvc.MockMvcRestDocumentation`.
268-
269-
[source,java,indent=0,role="secondary"]
270-
.WebTestClient
271-
----
272-
include::{examples-dir}/com/example/webtestclient/ExampleApplicationJUnit5Tests.java[tags=setup]
273-
----
274-
<1> The `WebTestClient` instance is configured by adding a `WebTestClientRestDocumentationConfigurer` as an `ExchangeFilterFunction`.
275-
You can obtain an instance of this class from the static `documentationConfiguration()` method on `org.springframework.restdocs.webtestclient.WebTestClientRestDocumentation`.
276-
277-
[source,java,indent=0,role="secondary"]
278-
.REST Assured
279-
----
280-
include::{examples-dir}/com/example/restassured/ExampleApplicationJUnit5Tests.java[tags=setup]
281-
----
282-
<1> REST Assured is configured by adding a `RestAssuredRestDocumentationConfigurer` as a `Filter`.
283-
You can obtain an instance of this class from the static `documentationConfiguration()` method on `RestAssuredRestDocumentation` in the `org.springframework.restdocs.restassured` package.
284-
285-
The configurer applies sensible defaults and also provides an API for customizing the configuration.
286-
See the <<configuration, configuration section>> for more information.
287-
288-
289-
290-
[[getting-started-documentation-snippets-setup-junit]]
291-
===== Setting up Your JUnit 4 Tests
292-
293-
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`.
294-
The following example shows how to do so:
295-
296-
[source,java,indent=0]
297-
----
298-
@Rule
299-
public JUnitRestDocumentation restDocumentation = new JUnitRestDocumentation();
300-
----
301-
302-
By default, the `JUnitRestDocumentation` rule is automatically configured with an output directory based on your project's build tool:
303-
304-
[cols="2,5"]
305-
|===
306-
| Build tool | Output directory
307-
308-
| Maven
309-
| `target/generated-snippets`
310-
311-
| Gradle
312-
| `build/generated-snippets`
313-
|===
314-
315-
You can override the default by providing an output directory when you create the `JUnitRestDocumentation` instance.
316-
The following example shows how to do so:
317-
318-
[source,java,indent=0]
319-
----
320-
@Rule
321-
public JUnitRestDocumentation restDocumentation = new JUnitRestDocumentation("custom");
322-
----
323-
324-
Next, you must provide an `@Before` method to configure MockMvc or WebTestClient, or REST Assured.
325-
The following examples show how to do so:
326-
327260
[source,java,indent=0,role="primary"]
328261
.MockMvc
329262
----
@@ -337,7 +270,7 @@ You can obtain an instance of this class from the static `documentationConfigura
337270
----
338271
include::{examples-dir}/com/example/webtestclient/ExampleApplicationTests.java[tags=setup]
339272
----
340-
<1> The `WebTestClient` instance is configured by adding a `WebTestclientRestDocumentationConfigurer` as an `ExchangeFilterFunction`.
273+
<1> The `WebTestClient` instance is configured by adding a `WebTestClientRestDocumentationConfigurer` as an `ExchangeFilterFunction`.
341274
You can obtain an instance of this class from the static `documentationConfiguration()` method on `org.springframework.restdocs.webtestclient.WebTestClientRestDocumentation`.
342275

343276
[source,java,indent=0,role="secondary"]
@@ -353,16 +286,15 @@ See the <<configuration, configuration section>> for more information.
353286

354287

355288

289+
356290
[[getting-started-documentation-snippets-setup-manual]]
357291
===== Setting up your tests without JUnit
358292

359-
The configuration when JUnit is not being used is largely similar to when it is being used.
360-
This section describes the key differences.
361-
The {samples}/testng[TestNG sample] also illustrates the approach.
293+
The configuration when JUnit is not being used is a little more involved as the test class must perform some lifecycle management.
294+
The {samples}/testng[TestNG sample] illustrates the approach.
362295

363-
The first difference is that you should use `ManualRestDocumentation` in place of `JUnitRestDocumentation`.
364-
Also, you do not need the `@Rule` annotation.
365-
The following example shows how to use `ManualRestDocumentation`:
296+
First, you need a `ManualRestDocumentation` field.
297+
The following example shows how to define it:
366298

367299
[source,java,indent=0]
368300
----

docs/src/test/java/com/example/mockmvc/CustomDefaultOperationPreprocessors.java

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2023 the original author or authors.
2+
* Copyright 2014-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,10 +16,11 @@
1616

1717
package com.example.mockmvc;
1818

19-
import org.junit.Before;
20-
import org.junit.Rule;
19+
import org.junit.jupiter.api.BeforeEach;
20+
import org.junit.jupiter.api.extension.ExtendWith;
2121

22-
import org.springframework.restdocs.JUnitRestDocumentation;
22+
import org.springframework.restdocs.RestDocumentationContextProvider;
23+
import org.springframework.restdocs.RestDocumentationExtension;
2324
import org.springframework.test.web.servlet.MockMvc;
2425
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
2526
import org.springframework.web.context.WebApplicationContext;
@@ -28,21 +29,19 @@
2829
import static org.springframework.restdocs.operation.preprocess.Preprocessors.modifyHeaders;
2930
import static org.springframework.restdocs.operation.preprocess.Preprocessors.prettyPrint;
3031

31-
public class CustomDefaultOperationPreprocessors {
32-
33-
@Rule
34-
public final JUnitRestDocumentation restDocumentation = new JUnitRestDocumentation();
32+
@ExtendWith(RestDocumentationExtension.class)
33+
class CustomDefaultOperationPreprocessors {
3534

3635
private WebApplicationContext context;
3736

3837
@SuppressWarnings("unused")
3938
private MockMvc mockMvc;
4039

41-
@Before
42-
public void setup() {
40+
@BeforeEach
41+
void setup(RestDocumentationContextProvider restDocumentation) {
4342
// tag::custom-default-operation-preprocessors[]
4443
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.context)
45-
.apply(documentationConfiguration(this.restDocumentation).operationPreprocessors()
44+
.apply(documentationConfiguration(restDocumentation).operationPreprocessors()
4645
.withRequestDefaults(modifyHeaders().remove("Foo")) // <1>
4746
.withResponseDefaults(prettyPrint())) // <2>
4847
.build();

docs/src/test/java/com/example/mockmvc/CustomDefaultSnippets.java

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2023 the original author or authors.
2+
* Copyright 2014-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,34 +16,33 @@
1616

1717
package com.example.mockmvc;
1818

19-
import org.junit.Before;
20-
import org.junit.Rule;
19+
import org.junit.jupiter.api.BeforeEach;
20+
import org.junit.jupiter.api.extension.ExtendWith;
2121

2222
import org.springframework.beans.factory.annotation.Autowired;
23-
import org.springframework.restdocs.JUnitRestDocumentation;
23+
import org.springframework.restdocs.RestDocumentationContextProvider;
24+
import org.springframework.restdocs.RestDocumentationExtension;
2425
import org.springframework.test.web.servlet.MockMvc;
2526
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
2627
import org.springframework.web.context.WebApplicationContext;
2728

2829
import static org.springframework.restdocs.cli.CliDocumentation.curlRequest;
2930
import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.documentationConfiguration;
3031

31-
public class CustomDefaultSnippets {
32-
33-
@Rule
34-
public final JUnitRestDocumentation restDocumentation = new JUnitRestDocumentation();
32+
@ExtendWith(RestDocumentationExtension.class)
33+
class CustomDefaultSnippets {
3534

3635
@Autowired
3736
private WebApplicationContext context;
3837

3938
@SuppressWarnings("unused")
4039
private MockMvc mockMvc;
4140

42-
@Before
43-
public void setUp() {
41+
@BeforeEach
42+
void setUp(RestDocumentationContextProvider restDocumentation) {
4443
// tag::custom-default-snippets[]
4544
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.context)
46-
.apply(documentationConfiguration(this.restDocumentation).snippets().withDefaults(curlRequest()))
45+
.apply(documentationConfiguration(restDocumentation).snippets().withDefaults(curlRequest()))
4746
.build();
4847
// end::custom-default-snippets[]
4948
}

docs/src/test/java/com/example/mockmvc/CustomEncoding.java

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2023 the original author or authors.
2+
* Copyright 2014-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,33 +16,32 @@
1616

1717
package com.example.mockmvc;
1818

19-
import org.junit.Before;
20-
import org.junit.Rule;
19+
import org.junit.jupiter.api.BeforeEach;
20+
import org.junit.jupiter.api.extension.ExtendWith;
2121

2222
import org.springframework.beans.factory.annotation.Autowired;
23-
import org.springframework.restdocs.JUnitRestDocumentation;
23+
import org.springframework.restdocs.RestDocumentationContextProvider;
24+
import org.springframework.restdocs.RestDocumentationExtension;
2425
import org.springframework.test.web.servlet.MockMvc;
2526
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
2627
import org.springframework.web.context.WebApplicationContext;
2728

2829
import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.documentationConfiguration;
2930

30-
public class CustomEncoding {
31-
32-
@Rule
33-
public final JUnitRestDocumentation restDocumentation = new JUnitRestDocumentation();
31+
@ExtendWith(RestDocumentationExtension.class)
32+
class CustomEncoding {
3433

3534
@Autowired
3635
private WebApplicationContext context;
3736

3837
@SuppressWarnings("unused")
3938
private MockMvc mockMvc;
4039

41-
@Before
42-
public void setUp() {
40+
@BeforeEach
41+
void setUp(RestDocumentationContextProvider restDocumentation) {
4342
// tag::custom-encoding[]
4443
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.context)
45-
.apply(documentationConfiguration(this.restDocumentation).snippets().withEncoding("ISO-8859-1"))
44+
.apply(documentationConfiguration(restDocumentation).snippets().withEncoding("ISO-8859-1"))
4645
.build();
4746
// end::custom-encoding[]
4847
}

docs/src/test/java/com/example/mockmvc/CustomFormat.java

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2023 the original author or authors.
2+
* Copyright 2014-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,34 +16,33 @@
1616

1717
package com.example.mockmvc;
1818

19-
import org.junit.Before;
20-
import org.junit.Rule;
19+
import org.junit.jupiter.api.BeforeEach;
20+
import org.junit.jupiter.api.extension.ExtendWith;
2121

2222
import org.springframework.beans.factory.annotation.Autowired;
23-
import org.springframework.restdocs.JUnitRestDocumentation;
23+
import org.springframework.restdocs.RestDocumentationContextProvider;
24+
import org.springframework.restdocs.RestDocumentationExtension;
2425
import org.springframework.restdocs.templates.TemplateFormats;
2526
import org.springframework.test.web.servlet.MockMvc;
2627
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
2728
import org.springframework.web.context.WebApplicationContext;
2829

2930
import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.documentationConfiguration;
3031

31-
public class CustomFormat {
32-
33-
@Rule
34-
public final JUnitRestDocumentation restDocumentation = new JUnitRestDocumentation();
32+
@ExtendWith(RestDocumentationExtension.class)
33+
class CustomFormat {
3534

3635
@Autowired
3736
private WebApplicationContext context;
3837

3938
@SuppressWarnings("unused")
4039
private MockMvc mockMvc;
4140

42-
@Before
43-
public void setUp() {
41+
@BeforeEach
42+
void setUp(RestDocumentationContextProvider restDocumentation) {
4443
// tag::custom-format[]
4544
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.context)
46-
.apply(documentationConfiguration(this.restDocumentation).snippets()
45+
.apply(documentationConfiguration(restDocumentation).snippets()
4746
.withTemplateFormat(TemplateFormats.markdown()))
4847
.build();
4948
// end::custom-format[]

docs/src/test/java/com/example/mockmvc/CustomUriConfiguration.java

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2023 the original author or authors.
2+
* Copyright 2014-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,33 +16,32 @@
1616

1717
package com.example.mockmvc;
1818

19-
import org.junit.Before;
20-
import org.junit.Rule;
19+
import org.junit.jupiter.api.BeforeEach;
20+
import org.junit.jupiter.api.extension.ExtendWith;
2121

2222
import org.springframework.beans.factory.annotation.Autowired;
23-
import org.springframework.restdocs.JUnitRestDocumentation;
23+
import org.springframework.restdocs.RestDocumentationContextProvider;
24+
import org.springframework.restdocs.RestDocumentationExtension;
2425
import org.springframework.test.web.servlet.MockMvc;
2526
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
2627
import org.springframework.web.context.WebApplicationContext;
2728

2829
import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.documentationConfiguration;
2930

30-
public class CustomUriConfiguration {
31-
32-
@Rule
33-
public final JUnitRestDocumentation restDocumentation = new JUnitRestDocumentation();
31+
@ExtendWith(RestDocumentationExtension.class)
32+
class CustomUriConfiguration {
3433

3534
@Autowired
3635
private WebApplicationContext context;
3736

3837
@SuppressWarnings("unused")
3938
private MockMvc mockMvc;
4039

41-
@Before
42-
public void setUp() {
40+
@BeforeEach
41+
void setUp(RestDocumentationContextProvider restDocumentation) {
4342
// tag::custom-uri-configuration[]
4443
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.context)
45-
.apply(documentationConfiguration(this.restDocumentation).uris()
44+
.apply(documentationConfiguration(restDocumentation).uris()
4645
.withScheme("https")
4746
.withHost("example.com")
4847
.withPort(443))

0 commit comments

Comments
 (0)