Adopt Spring Java Format's JUnit 5 check

Closes gh-941
This commit is contained in:
Andy Wilkinson
2019-07-04 09:31:24 +01:00
parent 66e871f2c9
commit deed4863fb
10 changed files with 28 additions and 27 deletions

View File

@@ -56,7 +56,7 @@ class MainControllerStatsIntegrationTests extends AbstractFullStackInitializrInt
private ProjectGenerationStatPublisher projectGenerationStatPublisher; private ProjectGenerationStatPublisher projectGenerationStatPublisher;
@BeforeEach @BeforeEach
public void setup() { void setup() {
this.statsMockController.stats.clear(); this.statsMockController.stats.clear();
// Make sure our mock is going to be invoked with the stats // Make sure our mock is going to be invoked with the stats
this.projectGenerationStatPublisher this.projectGenerationStatPublisher

View File

@@ -76,7 +76,7 @@ class ProjectGenerationStatPublisherTests {
private MockRestServiceServer mockServer; private MockRestServiceServer mockServer;
@BeforeEach @BeforeEach
public void setUp() { void setUp() {
configureService(createProperties()); configureService(createProperties());
} }

View File

@@ -48,6 +48,11 @@
<optional>true</optional> <optional>true</optional>
</dependency> </dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId> <artifactId>spring-boot-starter-web</artifactId>

View File

@@ -31,15 +31,14 @@ A convenient way to consume the stubs in your project is to add a test dependenc
and then pull the stubs from the classpath. In a Spring Boot application, using and then pull the stubs from the classpath. In a Spring Boot application, using
Spring Cloud Contract, you can start a WireMock server and register all the stubs Spring Cloud Contract, you can start a WireMock server and register all the stubs
with it like this: with it, as shown in the following JUnit 5-based example:
[source,java,indent=0,subs="attributes"] [source,java,indent=0,subs="attributes"]
---- ----
@RunWith(SpringRunner.class)
@SpringBootTest @SpringBootTest
@AutoConfigureWireMock(port = 0, @AutoConfigureWireMock(port = 0,
stubs="classpath:META-INF/io.spring.initializr/initializr-web/{spring-initializr-version}") stubs="classpath:META-INF/io.spring.initializr/initializr-web/{spring-initializr-version}")
public class ClientApplicationTests { class ClientApplicationTests {
@Value("${wiremock.server.port}") @Value("${wiremock.server.port}")
private int port; private int port;
@@ -80,16 +79,16 @@ stubs declared as a dependency):
</dependency> </dependency>
---- ----
The test should use `@AutoConfigureStubRunner` instead: The test should use `@AutoConfigureStubRunner` instead, as shown in the following JUnit
5-based example:
[source,java,indent=0,,subs="attributes,specialchars"] [source,java,indent=0,,subs="attributes,specialchars"]
---- ----
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.NONE) @SpringBootTest(webEnvironment = WebEnvironment.NONE)
@AutoConfigureStubRunner( @AutoConfigureStubRunner(
ids = "io.spring.initializr:initializr-web:{spring-initializr-version}", ids = "io.spring.initializr:initializr-web:{spring-initializr-version}",
repositoryRoot = "https://repo.spring.io/{initializr-repo}") repositoryRoot = "https://repo.spring.io/{initializr-repo}")
public class ClientApplicationTests { class ClientApplicationTests {
@Autowired @Autowired
private StubFinder stubFinder; private StubFinder stubFinder;
@@ -100,9 +99,9 @@ The test should use `@AutoConfigureStubRunner` instead:
} }
---- ----
Here is an example of a test that retrieves the metadata of the service. The assertions Here is JUnit 5-based example of a test that retrieves the metadata of the service. The
do not matter much here but it illustrates how you could integrate that in the test suite assertions do not matter much here but it illustrates how you could integrate that in the
of a custom client: test suite of a custom client:
[source,java,indent=0,subs="verbatim,quotes,attributes"] [source,java,indent=0,subs="verbatim,quotes,attributes"]
---- ----

View File

@@ -18,8 +18,7 @@ package io.spring.initializr.stub;
import java.net.URI; import java.net.URI;
import org.junit.Test; import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
@@ -34,16 +33,14 @@ import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.http.RequestEntity; import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.web.client.RestTemplate; import org.springframework.web.client.RestTemplate;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
@RunWith(SpringRunner.class) // tag::test[]
@SpringBootTest(webEnvironment = WebEnvironment.NONE) @SpringBootTest(webEnvironment = WebEnvironment.NONE)
@AutoConfigureStubRunner(ids = "io.spring.initializr:initializr-web:${project.version}", stubsMode = StubsMode.LOCAL) @AutoConfigureStubRunner(ids = "io.spring.initializr:initializr-web:${project.version}", stubsMode = StubsMode.LOCAL)
// tag::test[] class ClientApplicationTests {
public class ClientApplicationTests {
@Autowired @Autowired
private StubFinder stubFinder; private StubFinder stubFinder;
@@ -52,7 +49,7 @@ public class ClientApplicationTests {
private RestTemplate restTemplate; private RestTemplate restTemplate;
@Test @Test
public void testCurrentMetadata() { void testCurrentMetadata() {
RequestEntity<Void> request = RequestEntity.get(createUri("/")) RequestEntity<Void> request = RequestEntity.get(createUri("/"))
.accept(MediaType.valueOf("application/vnd.initializr.v2.1+json")).build(); .accept(MediaType.valueOf("application/vnd.initializr.v2.1+json")).build();

View File

@@ -77,7 +77,7 @@ public abstract class AbstractInitializrIntegrationTests {
private RestTemplate restTemplate; private RestTemplate restTemplate;
@BeforeEach @BeforeEach
public void before(@TempDir Path folder) { void before(@TempDir Path folder) {
this.restTemplate = this.restTemplateBuilder.build(); this.restTemplate = this.restTemplateBuilder.build();
this.folder = folder; this.folder = folder;
} }

View File

@@ -169,7 +169,7 @@ class MainControllerIntegrationTests extends AbstractInitializrControllerIntegra
@Test @Test
@Disabled("Need a comparator that does not care about the number of elements in an array") @Disabled("Need a comparator that does not care about the number of elements in an array")
public void currentMetadataCompatibleWithV2() { void currentMetadataCompatibleWithV2() {
ResponseEntity<String> response = invokeHome(null, "*/*"); ResponseEntity<String> response = invokeHome(null, "*/*");
validateMetadata(response, AbstractInitializrIntegrationTests.CURRENT_METADATA_MEDIA_TYPE, "2.0.0", validateMetadata(response, AbstractInitializrIntegrationTests.CURRENT_METADATA_MEDIA_TYPE, "2.0.0",
JSONCompareMode.LENIENT); JSONCompareMode.LENIENT);
@@ -237,7 +237,7 @@ class MainControllerIntegrationTests extends AbstractInitializrControllerIntegra
@Test @Test
// make sure curl can still receive metadata with json // make sure curl can still receive metadata with json
public void curlWithAcceptHeaderJson() { void curlWithAcceptHeaderJson() {
ResponseEntity<String> response = invokeHome("curl/1.2.4", "application/json"); ResponseEntity<String> response = invokeHome("curl/1.2.4", "application/json");
validateContentType(response, AbstractInitializrIntegrationTests.CURRENT_METADATA_MEDIA_TYPE); validateContentType(response, AbstractInitializrIntegrationTests.CURRENT_METADATA_MEDIA_TYPE);
validateCurrentMetadata(response.getBody()); validateCurrentMetadata(response.getBody());
@@ -263,7 +263,7 @@ class MainControllerIntegrationTests extends AbstractInitializrControllerIntegra
@Test @Test
// make sure curl can still receive metadata with json // make sure curl can still receive metadata with json
public void httpieWithAcceptHeaderJson() { void httpieWithAcceptHeaderJson() {
ResponseEntity<String> response = invokeHome("HTTPie/0.8.0", "application/json"); ResponseEntity<String> response = invokeHome("HTTPie/0.8.0", "application/json");
validateContentType(response, AbstractInitializrIntegrationTests.CURRENT_METADATA_MEDIA_TYPE); validateContentType(response, AbstractInitializrIntegrationTests.CURRENT_METADATA_MEDIA_TYPE);
validateCurrentMetadata(response.getBody()); validateCurrentMetadata(response.getBody());
@@ -296,7 +296,7 @@ class MainControllerIntegrationTests extends AbstractInitializrControllerIntegra
@Test @Test
// Test that the current output is exactly what we expect // Test that the current output is exactly what we expect
public void validateCurrentProjectMetadata() { void validateCurrentProjectMetadata() {
validateCurrentMetadata(getMetadataJson()); validateCurrentMetadata(getMetadataJson());
} }

View File

@@ -39,7 +39,7 @@ class CommandLineHelpGeneratorTests {
private CommandLineHelpGenerator generator; private CommandLineHelpGenerator generator;
@BeforeEach @BeforeEach
public void init() { void init() {
this.generator = new CommandLineHelpGenerator(new MustacheTemplateRenderer("classpath:/templates")); this.generator = new CommandLineHelpGenerator(new MustacheTemplateRenderer("classpath:/templates"));
} }

View File

@@ -52,7 +52,7 @@ class DefaultInitializrMetadataUpdateStrategyTests {
private MockRestServiceServer mockServer; private MockRestServiceServer mockServer;
@BeforeEach @BeforeEach
public void setUp() { void setUp() {
this.restTemplate = new RestTemplate(); this.restTemplate = new RestTemplate();
this.mockServer = MockRestServiceServer.createServer(this.restTemplate); this.mockServer = MockRestServiceServer.createServer(this.restTemplate);
} }

View File

@@ -5,8 +5,7 @@
<module name="com.puppycrawl.tools.checkstyle.Checker"> <module name="com.puppycrawl.tools.checkstyle.Checker">
<module name="io.spring.javaformat.checkstyle.SpringChecks"/> <module name="io.spring.javaformat.checkstyle.SpringChecks"/>
<module name="com.puppycrawl.tools.checkstyle.TreeWalker"> <module name="com.puppycrawl.tools.checkstyle.TreeWalker">
<module <module name="com.puppycrawl.tools.checkstyle.checks.imports.ImportControlCheck">
name="com.puppycrawl.tools.checkstyle.checks.imports.ImportControlCheck">
<property name="file" <property name="file"
value="${main.basedir}/src/checkstyle/import-control.xml"/> value="${main.basedir}/src/checkstyle/import-control.xml"/>
<property name="path" value="^.*[\\/]src[\\/]main[\\/].*$"/> <property name="path" value="^.*[\\/]src[\\/]main[\\/].*$"/>
@@ -19,5 +18,6 @@
value="Please use BDDMockito imports."/> value="Please use BDDMockito imports."/>
<property name="ignoreComments" value="true"/> <property name="ignoreComments" value="true"/>
</module> </module>
<module name="io.spring.javaformat.checkstyle.check.SpringJUnit5Check"/>
</module> </module>
</module> </module>