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;
@BeforeEach
public void setup() {
void setup() {
this.statsMockController.stats.clear();
// Make sure our mock is going to be invoked with the stats
this.projectGenerationStatPublisher

View File

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

View File

@@ -48,6 +48,11 @@
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<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
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"]
----
@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureWireMock(port = 0,
stubs="classpath:META-INF/io.spring.initializr/initializr-web/{spring-initializr-version}")
public class ClientApplicationTests {
class ClientApplicationTests {
@Value("${wiremock.server.port}")
private int port;
@@ -80,16 +79,16 @@ stubs declared as a 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"]
----
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.NONE)
@AutoConfigureStubRunner(
ids = "io.spring.initializr:initializr-web:{spring-initializr-version}",
repositoryRoot = "https://repo.spring.io/{initializr-repo}")
public class ClientApplicationTests {
class ClientApplicationTests {
@Autowired
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
do not matter much here but it illustrates how you could integrate that in the test suite
of a custom client:
Here is JUnit 5-based example of a test that retrieves the metadata of the service. The
assertions do not matter much here but it illustrates how you could integrate that in the
test suite of a custom client:
[source,java,indent=0,subs="verbatim,quotes,attributes"]
----

View File

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

View File

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

View File

@@ -169,7 +169,7 @@ class MainControllerIntegrationTests extends AbstractInitializrControllerIntegra
@Test
@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, "*/*");
validateMetadata(response, AbstractInitializrIntegrationTests.CURRENT_METADATA_MEDIA_TYPE, "2.0.0",
JSONCompareMode.LENIENT);
@@ -237,7 +237,7 @@ class MainControllerIntegrationTests extends AbstractInitializrControllerIntegra
@Test
// make sure curl can still receive metadata with json
public void curlWithAcceptHeaderJson() {
void curlWithAcceptHeaderJson() {
ResponseEntity<String> response = invokeHome("curl/1.2.4", "application/json");
validateContentType(response, AbstractInitializrIntegrationTests.CURRENT_METADATA_MEDIA_TYPE);
validateCurrentMetadata(response.getBody());
@@ -263,7 +263,7 @@ class MainControllerIntegrationTests extends AbstractInitializrControllerIntegra
@Test
// make sure curl can still receive metadata with json
public void httpieWithAcceptHeaderJson() {
void httpieWithAcceptHeaderJson() {
ResponseEntity<String> response = invokeHome("HTTPie/0.8.0", "application/json");
validateContentType(response, AbstractInitializrIntegrationTests.CURRENT_METADATA_MEDIA_TYPE);
validateCurrentMetadata(response.getBody());
@@ -296,7 +296,7 @@ class MainControllerIntegrationTests extends AbstractInitializrControllerIntegra
@Test
// Test that the current output is exactly what we expect
public void validateCurrentProjectMetadata() {
void validateCurrentProjectMetadata() {
validateCurrentMetadata(getMetadataJson());
}

View File

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

View File

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

View File

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