Skip to content

Commit b3d5cd5

Browse files
committed
Add JUnit 5 checkstyle rules
Add a rule to enforce JUnit 5 usage and conventions. Closes spring-projectsgh-17093
1 parent 2560b54 commit b3d5cd5

File tree

29 files changed

+130
-156
lines changed

29 files changed

+130
-156
lines changed

pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
</property>
2525
</activation>
2626
<properties>
27-
<spring-javaformat.version>0.0.11</spring-javaformat.version>
27+
<spring-javaformat.version>0.0.12</spring-javaformat.version>
2828
<nohttp-checkstyle.version>0.0.1.RELEASE</nohttp-checkstyle.version>
2929
</properties>
3030
<build>

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/cassandra/CassandraDataAutoConfigurationIntegrationTests.java

-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ void hasDefaultSchemaActionSet() {
7171
AutoConfigurationPackages.register(this.context, cityPackage);
7272
this.context.register(CassandraAutoConfiguration.class, CassandraDataAutoConfiguration.class);
7373
this.context.refresh();
74-
7574
CassandraSessionFactoryBean bean = this.context.getBean(CassandraSessionFactoryBean.class);
7675
assertThat(bean.getSchemaAction()).isEqualTo(SchemaAction.NONE);
7776
}

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/elasticsearch/ElasticsearchDataAutoConfigurationTests.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ void customRestTemplateShouldBeUsed() {
103103
}
104104

105105
@Test
106-
public void customReactiveRestTemplateShouldBeUsed() {
106+
void customReactiveRestTemplateShouldBeUsed() {
107107
this.contextRunner.withUserConfiguration(CustomReactiveRestTemplate.class)
108108
.run((context) -> assertThat(context).getBeanNames(ReactiveElasticsearchTemplate.class).hasSize(1)
109109
.contains("reactiveElasticsearchTemplate"));

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/rsocket/RSocketMessagingAutoConfigurationTests.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class RSocketMessagingAutoConfigurationTests {
4242
.withUserConfiguration(BaseConfiguration.class);
4343

4444
@Test
45-
public void shouldCreateDefaultBeans() {
45+
void shouldCreateDefaultBeans() {
4646
this.contextRunner.run((context) -> {
4747
assertThat(context).getBeans(MessageHandlerAcceptor.class).hasSize(1);
4848
assertThat(context.getBean(MessageHandlerAcceptor.class).getRouteMatcher())

spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/test/autoconfigure/restdocs/restassured/UserDocumentationTests.java

+3-12
Original file line numberDiff line numberDiff line change
@@ -19,33 +19,24 @@
1919
// tag::source[]
2020
import io.restassured.specification.RequestSpecification;
2121
import org.junit.jupiter.api.Test;
22-
import org.junit.jupiter.api.extension.ExtendWith;
2322

2423
import org.springframework.beans.factory.annotation.Autowired;
2524
import org.springframework.boot.test.autoconfigure.restdocs.AutoConfigureRestDocs;
2625
import org.springframework.boot.test.context.SpringBootTest;
2726
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
2827
import org.springframework.boot.web.server.LocalServerPort;
29-
import org.springframework.test.context.junit.jupiter.SpringExtension;
3028

3129
import static io.restassured.RestAssured.given;
3230
import static org.hamcrest.Matchers.is;
3331
import static org.springframework.restdocs.restassured3.RestAssuredRestDocumentation.document;
3432

35-
@ExtendWith(SpringExtension.class)
3633
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
3734
@AutoConfigureRestDocs
38-
public class UserDocumentationTests {
39-
40-
@LocalServerPort
41-
private int port;
42-
43-
@Autowired
44-
private RequestSpecification documentationSpec;
35+
class UserDocumentationTests {
4536

4637
@Test
47-
public void listUsers() {
48-
given(this.documentationSpec).filter(document("list-users")).when().port(this.port).get("/").then().assertThat()
38+
void listUsers(@Autowired RequestSpecification documentationSpec, @LocalServerPort int port) {
39+
given(documentationSpec).filter(document("list-users")).when().port(port).get("/").then().assertThat()
4940
.statusCode(is(200));
5041
}
5142

spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/test/context/ApplicationArgumentsExampleTests.java

+4-10
Original file line numberDiff line numberDiff line change
@@ -17,27 +17,21 @@
1717
package org.springframework.boot.docs.test.context;
1818

1919
import org.junit.jupiter.api.Test;
20-
import org.junit.jupiter.api.extension.ExtendWith;
2120

2221
import org.springframework.beans.factory.annotation.Autowired;
2322
import org.springframework.boot.ApplicationArguments;
2423
import org.springframework.boot.test.context.SpringBootTest;
25-
import org.springframework.test.context.junit.jupiter.SpringExtension;
2624

2725
import static org.assertj.core.api.Assertions.assertThat;
2826

2927
// tag::example[]
30-
@ExtendWith(SpringExtension.class)
3128
@SpringBootTest(args = "--app.test=one")
32-
public class ApplicationArgumentsExampleTests {
33-
34-
@Autowired
35-
private ApplicationArguments args;
29+
class ApplicationArgumentsExampleTests {
3630

3731
@Test
38-
public void applicationArgumentsPopulated() {
39-
assertThat(this.args.getOptionNames()).containsOnly("app.test");
40-
assertThat(this.args.getOptionValues("app.test")).containsOnly("one");
32+
void applicationArgumentsPopulated(@Autowired ApplicationArguments args) {
33+
assertThat(args.getOptionNames()).containsOnly("app.test");
34+
assertThat(args.getOptionValues("app.test")).containsOnly("one");
4135
}
4236

4337
}

spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/test/web/MockMvcExampleTests.java

+3-10
Original file line numberDiff line numberDiff line change
@@ -19,29 +19,22 @@
1919
// tag::test-mock-mvc[]
2020

2121
import org.junit.jupiter.api.Test;
22-
import org.junit.jupiter.api.extension.ExtendWith;
2322

24-
import org.springframework.beans.factory.annotation.Autowired;
2523
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
2624
import org.springframework.boot.test.context.SpringBootTest;
27-
import org.springframework.test.context.junit.jupiter.SpringExtension;
2825
import org.springframework.test.web.servlet.MockMvc;
2926

3027
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
3128
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
3229
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
3330

34-
@ExtendWith(SpringExtension.class)
3531
@SpringBootTest
3632
@AutoConfigureMockMvc
37-
public class MockMvcExampleTests {
38-
39-
@Autowired
40-
private MockMvc mvc;
33+
class MockMvcExampleTests {
4134

4235
@Test
43-
public void exampleTest() throws Exception {
44-
this.mvc.perform(get("/")).andExpect(status().isOk()).andExpect(content().string("Hello World"));
36+
void exampleTest(MockMvc mvc) throws Exception {
37+
mvc.perform(get("/")).andExpect(status().isOk()).andExpect(content().string("Hello World"));
4538
}
4639

4740
}

spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/test/web/MockWebTestClientExampleTests.java

+3-10
Original file line numberDiff line numberDiff line change
@@ -19,26 +19,19 @@
1919
// tag::test-mock-web-test-client[]
2020

2121
import org.junit.jupiter.api.Test;
22-
import org.junit.jupiter.api.extension.ExtendWith;
2322

2423
import org.springframework.beans.factory.annotation.Autowired;
2524
import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient;
2625
import org.springframework.boot.test.context.SpringBootTest;
27-
import org.springframework.test.context.junit.jupiter.SpringExtension;
2826
import org.springframework.test.web.reactive.server.WebTestClient;
2927

30-
@ExtendWith(SpringExtension.class)
3128
@SpringBootTest
3229
@AutoConfigureWebTestClient
33-
public class MockWebTestClientExampleTests {
34-
35-
@Autowired
36-
private WebTestClient webClient;
30+
class MockWebTestClientExampleTests {
3731

3832
@Test
39-
public void exampleTest() {
40-
this.webClient.get().uri("/").exchange().expectStatus().isOk().expectBody(String.class)
41-
.isEqualTo("Hello World");
33+
void exampleTest(@Autowired WebTestClient webClient) {
34+
webClient.get().uri("/").exchange().expectStatus().isOk().expectBody(String.class).isEqualTo("Hello World");
4235
}
4336

4437
}

spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/test/web/RandomPortTestRestTemplateExampleTests.java

+3-9
Original file line numberDiff line numberDiff line change
@@ -19,26 +19,20 @@
1919
// tag::test-random-port[]
2020

2121
import org.junit.jupiter.api.Test;
22-
import org.junit.jupiter.api.extension.ExtendWith;
2322

2423
import org.springframework.beans.factory.annotation.Autowired;
2524
import org.springframework.boot.test.context.SpringBootTest;
2625
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
2726
import org.springframework.boot.test.web.client.TestRestTemplate;
28-
import org.springframework.test.context.junit.jupiter.SpringExtension;
2927

3028
import static org.assertj.core.api.Assertions.assertThat;
3129

32-
@ExtendWith(SpringExtension.class)
3330
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
34-
public class RandomPortTestRestTemplateExampleTests {
35-
36-
@Autowired
37-
private TestRestTemplate restTemplate;
31+
class RandomPortTestRestTemplateExampleTests {
3832

3933
@Test
40-
public void exampleTest() {
41-
String body = this.restTemplate.getForObject("/", String.class);
34+
void exampleTest(@Autowired TestRestTemplate restTemplate) {
35+
String body = restTemplate.getForObject("/", String.class);
4236
assertThat(body).isEqualTo("Hello World");
4337
}
4438

spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/test/web/RandomPortWebTestClientExampleTests.java

+2-9
Original file line numberDiff line numberDiff line change
@@ -19,25 +19,18 @@
1919
// tag::test-random-port[]
2020

2121
import org.junit.jupiter.api.Test;
22-
import org.junit.jupiter.api.extension.ExtendWith;
2322

2423
import org.springframework.beans.factory.annotation.Autowired;
2524
import org.springframework.boot.test.context.SpringBootTest;
2625
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
27-
import org.springframework.test.context.junit.jupiter.SpringExtension;
2826
import org.springframework.test.web.reactive.server.WebTestClient;
2927

30-
@ExtendWith(SpringExtension.class)
3128
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
3229
public class RandomPortWebTestClientExampleTests {
3330

34-
@Autowired
35-
private WebTestClient webClient;
36-
3731
@Test
38-
public void exampleTest() {
39-
this.webClient.get().uri("/").exchange().expectStatus().isOk().expectBody(String.class)
40-
.isEqualTo("Hello World");
32+
void exampleTest(@Autowired WebTestClient webClient) {
33+
webClient.get().uri("/").exchange().expectStatus().isOk().expectBody(String.class).isEqualTo("Hello World");
4134
}
4235

4336
}

spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/client/RestClientTestWithComponentIntegrationTests.java

+3
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@
3737
@RestClientTest(ExampleRestClient.class)
3838
public class RestClientTestWithComponentIntegrationTests {
3939

40+
// JUnit 4 because RestClientTestWithoutJacksonIntegrationTests uses
41+
// ModifiedClassPathRunner
42+
4043
@Autowired
4144
private MockRestServiceServer server;
4245

spring-boot-project/spring-boot-tools/spring-boot-configuration-docs/src/test/java/org/springframework/boot/configurationdocs/CompoundConfigurationTableEntryTests.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@
2727
*
2828
* @author Brian Clozel
2929
*/
30-
public class CompoundConfigurationTableEntryTests {
30+
class CompoundConfigurationTableEntryTests {
3131

3232
private static String NEWLINE = System.lineSeparator();
3333

3434
@Test
35-
public void simpleProperty() {
35+
void simpleProperty() {
3636
ConfigurationMetadataProperty firstProp = new ConfigurationMetadataProperty();
3737
firstProp.setId("spring.test.first");
3838
firstProp.setType("java.lang.String");

spring-boot-project/spring-boot-tools/spring-boot-configuration-docs/src/test/java/org/springframework/boot/configurationdocs/ConfigurationTableTests.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@
2727
*
2828
* @author Brian Clozel
2929
*/
30-
public class ConfigurationTableTests {
30+
class ConfigurationTableTests {
3131

3232
private static String NEWLINE = System.lineSeparator();
3333

3434
@Test
35-
public void simpleTable() {
35+
void simpleTable() {
3636
ConfigurationTable table = new ConfigurationTable("test");
3737
ConfigurationMetadataProperty first = new ConfigurationMetadataProperty();
3838
first.setId("spring.test.prop");

spring-boot-project/spring-boot-tools/spring-boot-configuration-docs/src/test/java/org/springframework/boot/configurationdocs/SingleConfigurationTableEntryTests.java

+7-7
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@
2727
*
2828
* @author Brian Clozel
2929
*/
30-
public class SingleConfigurationTableEntryTests {
30+
class SingleConfigurationTableEntryTests {
3131

3232
private static String NEWLINE = System.lineSeparator();
3333

3434
@Test
35-
public void simpleProperty() {
35+
void simpleProperty() {
3636
ConfigurationMetadataProperty property = new ConfigurationMetadataProperty();
3737
property.setId("spring.test.prop");
3838
property.setDefaultValue("something");
@@ -46,7 +46,7 @@ public void simpleProperty() {
4646
}
4747

4848
@Test
49-
public void noDefaultValue() {
49+
void noDefaultValue() {
5050
ConfigurationMetadataProperty property = new ConfigurationMetadataProperty();
5151
property.setId("spring.test.prop");
5252
property.setDescription("This is a description.");
@@ -59,7 +59,7 @@ public void noDefaultValue() {
5959
}
6060

6161
@Test
62-
public void defaultValueWithPipes() {
62+
void defaultValueWithPipes() {
6363
ConfigurationMetadataProperty property = new ConfigurationMetadataProperty();
6464
property.setId("spring.test.prop");
6565
property.setDefaultValue("first|second");
@@ -73,7 +73,7 @@ public void defaultValueWithPipes() {
7373
}
7474

7575
@Test
76-
public void defaultValueWithBackslash() {
76+
void defaultValueWithBackslash() {
7777
ConfigurationMetadataProperty property = new ConfigurationMetadataProperty();
7878
property.setId("spring.test.prop");
7979
property.setDefaultValue("first\\second");
@@ -87,7 +87,7 @@ public void defaultValueWithBackslash() {
8787
}
8888

8989
@Test
90-
public void mapProperty() {
90+
void mapProperty() {
9191
ConfigurationMetadataProperty property = new ConfigurationMetadataProperty();
9292
property.setId("spring.test.prop");
9393
property.setDescription("This is a description.");
@@ -100,7 +100,7 @@ public void mapProperty() {
100100
}
101101

102102
@Test
103-
public void listProperty() {
103+
void listProperty() {
104104
String[] defaultValue = new String[] { "first", "second", "third" };
105105
ConfigurationMetadataProperty property = new ConfigurationMetadataProperty();
106106
property.setId("spring.test.prop");

spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/dsl/BuildInfoDslIntegrationTests.java

+7-7
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,12 @@
3838
* @author Andy Wilkinson
3939
*/
4040
@ExtendWith(GradleBuildExtension.class)
41-
public class BuildInfoDslIntegrationTests {
41+
class BuildInfoDslIntegrationTests {
4242

4343
final GradleBuild gradleBuild = new GradleBuild();
4444

4545
@Test
46-
public void basicJar() throws IOException {
46+
void basicJar() throws IOException {
4747
assertThat(this.gradleBuild.build("bootBuildInfo", "--stacktrace").task(":bootBuildInfo").getOutcome())
4848
.isEqualTo(TaskOutcome.SUCCESS);
4949
Properties properties = buildInfoProperties();
@@ -54,7 +54,7 @@ public void basicJar() throws IOException {
5454
}
5555

5656
@Test
57-
public void jarWithCustomName() throws IOException {
57+
void jarWithCustomName() throws IOException {
5858
assertThat(this.gradleBuild.build("bootBuildInfo", "--stacktrace").task(":bootBuildInfo").getOutcome())
5959
.isEqualTo(TaskOutcome.SUCCESS);
6060
Properties properties = buildInfoProperties();
@@ -65,7 +65,7 @@ public void jarWithCustomName() throws IOException {
6565
}
6666

6767
@Test
68-
public void basicWar() throws IOException {
68+
void basicWar() throws IOException {
6969
assertThat(this.gradleBuild.build("bootBuildInfo", "--stacktrace").task(":bootBuildInfo").getOutcome())
7070
.isEqualTo(TaskOutcome.SUCCESS);
7171
Properties properties = buildInfoProperties();
@@ -76,7 +76,7 @@ public void basicWar() throws IOException {
7676
}
7777

7878
@Test
79-
public void warWithCustomName() throws IOException {
79+
void warWithCustomName() throws IOException {
8080
assertThat(this.gradleBuild.build("bootBuildInfo", "--stacktrace").task(":bootBuildInfo").getOutcome())
8181
.isEqualTo(TaskOutcome.SUCCESS);
8282
Properties properties = buildInfoProperties();
@@ -87,7 +87,7 @@ public void warWithCustomName() throws IOException {
8787
}
8888

8989
@Test
90-
public void additionalProperties() throws IOException {
90+
void additionalProperties() throws IOException {
9191
assertThat(this.gradleBuild.build("bootBuildInfo", "--stacktrace").task(":bootBuildInfo").getOutcome())
9292
.isEqualTo(TaskOutcome.SUCCESS);
9393
Properties properties = buildInfoProperties();
@@ -100,7 +100,7 @@ public void additionalProperties() throws IOException {
100100
}
101101

102102
@Test
103-
public void classesDependency() throws IOException {
103+
void classesDependency() throws IOException {
104104
assertThat(this.gradleBuild.build("classes", "--stacktrace").task(":bootBuildInfo").getOutcome())
105105
.isEqualTo(TaskOutcome.SUCCESS);
106106
}

0 commit comments

Comments
 (0)