Skip to content

Commit b40f0f6

Browse files
committed
Removing remaining usage of Hamcrest from tests
Closes gh-776
1 parent df0e38c commit b40f0f6

19 files changed

+658
-436
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?xml version="1.0"?>
2+
<!DOCTYPE suppressions PUBLIC
3+
"-//Checkstyle//DTD SuppressionFilter Configuration 1.2//EN"
4+
"https://checkstyle.org/dtds/suppressions_1_2.dtd">
5+
<suppressions>
6+
<suppress files="com[\\/]example[\\/]restassured" checks="IllegalImport" />
7+
</suppressions>

config/checkstyle/checkstyle.xml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
11
<?xml version="1.0"?>
22
<!DOCTYPE module PUBLIC "-//Puppy Crawl//DTD Check Configuration 1.3//EN" "https://checkstyle.org/dtds/configuration_1_3.dtd">
33
<module name="com.puppycrawl.tools.checkstyle.Checker">
4+
<module name="SuppressionFilter">
5+
<property name="file" value="${config_loc}/checkstyle-suppressions.xml"/>
6+
</module>
47
<module name="io.spring.javaformat.checkstyle.SpringChecks">
58
<property name="avoidStaticImportExcludes" value=" org.springframework.restdocs.cli.CliDocumentation.*"/>
69
</module>
7-
10+
<module name="com.puppycrawl.tools.checkstyle.TreeWalker">
11+
<module name="com.puppycrawl.tools.checkstyle.checks.imports.IllegalImportCheck">
12+
<property name="regexp" value="true" />
13+
<property name="illegalPkgs" value="^org\.hamcrest.*" />
14+
</module>
15+
</module>
816
</module>

spring-restdocs-asciidoctor-support/src/test/java/org/springframework/restdocs/asciidoctor/SnippetsDirectoryResolverTests.java

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2019 the original author or authors.
2+
* Copyright 2014-2022 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.
@@ -23,11 +23,10 @@
2323

2424
import org.junit.Rule;
2525
import org.junit.Test;
26-
import org.junit.rules.ExpectedException;
2726
import org.junit.rules.TemporaryFolder;
2827

2928
import static org.assertj.core.api.Assertions.assertThat;
30-
import static org.hamcrest.CoreMatchers.equalTo;
29+
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
3130

3231
/**
3332
* Tests for {@link SnippetsDirectoryResolver}.
@@ -39,9 +38,6 @@ public class SnippetsDirectoryResolverTests {
3938
@Rule
4039
public TemporaryFolder temporaryFolder = new TemporaryFolder();
4140

42-
@Rule
43-
public ExpectedException thrown = ExpectedException.none();
44-
4541
@Test
4642
public void mavenProjectsUseTargetGeneratedSnippetsRelativeToDocdir() throws IOException {
4743
this.temporaryFolder.newFile("pom.xml");
@@ -57,17 +53,15 @@ public void illegalStateExceptionWhenMavenPomCannotBeFound() throws IOException
5753
Map<String, Object> attributes = new HashMap<>();
5854
String docdir = new File(this.temporaryFolder.getRoot(), "src/main/asciidoc").getAbsolutePath();
5955
attributes.put("docdir", docdir);
60-
this.thrown.expect(IllegalStateException.class);
61-
this.thrown.expectMessage(equalTo("pom.xml not found in '" + docdir + "' or above"));
62-
getMavenSnippetsDirectory(attributes);
56+
assertThatIllegalStateException().isThrownBy(() -> getMavenSnippetsDirectory(attributes))
57+
.withMessage("pom.xml not found in '" + docdir + "' or above");
6358
}
6459

6560
@Test
6661
public void illegalStateWhenDocdirAttributeIsNotSetInMavenProject() throws IOException {
6762
Map<String, Object> attributes = new HashMap<>();
68-
this.thrown.expect(IllegalStateException.class);
69-
this.thrown.expectMessage(equalTo("docdir attribute not found"));
70-
getMavenSnippetsDirectory(attributes);
63+
assertThatIllegalStateException().isThrownBy(() -> getMavenSnippetsDirectory(attributes))
64+
.withMessage("docdir attribute not found");
7165
}
7266

7367
@Test
@@ -100,9 +94,9 @@ public void gradleProjectsUseBuildGeneratedSnippetsBeneathProjectdirWhenGradlePr
10094
@Test
10195
public void illegalStateWhenGradleProjectdirAndProjectdirAttributesAreNotSetInGradleProject() throws IOException {
10296
Map<String, Object> attributes = new HashMap<>();
103-
this.thrown.expect(IllegalStateException.class);
104-
this.thrown.expectMessage(equalTo("projectdir attribute not found"));
105-
new SnippetsDirectoryResolver().getSnippetsDirectory(attributes);
97+
assertThatIllegalStateException()
98+
.isThrownBy(() -> new SnippetsDirectoryResolver().getSnippetsDirectory(attributes))
99+
.withMessage("projectdir attribute not found");
106100
}
107101

108102
private File getMavenSnippetsDirectory(Map<String, Object> attributes) {

spring-restdocs-core/build.gradle

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,6 @@ dependencies {
6363
testImplementation("org.assertj:assertj-core")
6464
testImplementation("org.javamoney:moneta")
6565
testImplementation("org.mockito:mockito-core")
66-
testImplementation("org.hamcrest:hamcrest-core")
67-
testImplementation("org.hamcrest:hamcrest-library")
6866
testImplementation("org.springframework:spring-test")
6967

7068
testRuntimeOnly("org.glassfish:javax.el:3.0.0")

spring-restdocs-core/src/test/java/org/springframework/restdocs/headers/RequestHeadersSnippetFailureTests.java

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2021 the original author or authors.
2+
* Copyright 2014-2022 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.
@@ -21,14 +21,12 @@
2121

2222
import org.junit.Rule;
2323
import org.junit.Test;
24-
import org.junit.rules.ExpectedException;
2524

2625
import org.springframework.restdocs.snippet.SnippetException;
2726
import org.springframework.restdocs.templates.TemplateFormats;
2827
import org.springframework.restdocs.testfixtures.OperationBuilder;
2928

30-
import static org.hamcrest.CoreMatchers.endsWith;
31-
import static org.hamcrest.CoreMatchers.equalTo;
29+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
3230
import static org.springframework.restdocs.headers.HeaderDocumentation.headerWithName;
3331

3432
/**
@@ -42,25 +40,21 @@ public class RequestHeadersSnippetFailureTests {
4240
@Rule
4341
public OperationBuilder operationBuilder = new OperationBuilder(TemplateFormats.asciidoctor());
4442

45-
@Rule
46-
public ExpectedException thrown = ExpectedException.none();
47-
4843
@Test
4944
public void missingRequestHeader() throws IOException {
50-
this.thrown.expect(SnippetException.class);
51-
this.thrown.expectMessage(
52-
equalTo("Headers with the following names were not found" + " in the request: [Accept]"));
53-
new RequestHeadersSnippet(Arrays.asList(headerWithName("Accept").description("one")))
54-
.document(this.operationBuilder.request("http://localhost").build());
45+
assertThatExceptionOfType(SnippetException.class)
46+
.isThrownBy(() -> new RequestHeadersSnippet(Arrays.asList(headerWithName("Accept").description("one")))
47+
.document(this.operationBuilder.request("http://localhost").build()))
48+
.withMessage("Headers with the following names were not found in the request: [Accept]");
5549
}
5650

5751
@Test
5852
public void undocumentedRequestHeaderAndMissingRequestHeader() throws IOException {
59-
this.thrown.expect(SnippetException.class);
60-
this.thrown.expectMessage(
61-
endsWith("Headers with the following names were not found" + " in the request: [Accept]"));
62-
new RequestHeadersSnippet(Arrays.asList(headerWithName("Accept").description("one")))
63-
.document(this.operationBuilder.request("http://localhost").header("X-Test", "test").build());
53+
assertThatExceptionOfType(SnippetException.class)
54+
.isThrownBy(() -> new RequestHeadersSnippet(Arrays.asList(headerWithName("Accept").description("one")))
55+
.document(this.operationBuilder.request("http://localhost").header("X-Test", "test").build()))
56+
.withMessageEndingWith("Headers with the following names were not found in the request: [Accept]");
57+
6458
}
6559

6660
}

spring-restdocs-core/src/test/java/org/springframework/restdocs/headers/ResponseHeadersSnippetFailureTests.java

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2021 the original author or authors.
2+
* Copyright 2014-2022 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.
@@ -21,14 +21,12 @@
2121

2222
import org.junit.Rule;
2323
import org.junit.Test;
24-
import org.junit.rules.ExpectedException;
2524

2625
import org.springframework.restdocs.snippet.SnippetException;
2726
import org.springframework.restdocs.templates.TemplateFormats;
2827
import org.springframework.restdocs.testfixtures.OperationBuilder;
2928

30-
import static org.hamcrest.CoreMatchers.endsWith;
31-
import static org.hamcrest.CoreMatchers.equalTo;
29+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
3230
import static org.springframework.restdocs.headers.HeaderDocumentation.headerWithName;
3331

3432
/**
@@ -42,25 +40,21 @@ public class ResponseHeadersSnippetFailureTests {
4240
@Rule
4341
public OperationBuilder operationBuilder = new OperationBuilder(TemplateFormats.asciidoctor());
4442

45-
@Rule
46-
public ExpectedException thrown = ExpectedException.none();
47-
4843
@Test
4944
public void missingResponseHeader() throws IOException {
50-
this.thrown.expect(SnippetException.class);
51-
this.thrown.expectMessage(
52-
equalTo("Headers with the following names were not found" + " in the response: [Content-Type]"));
53-
new ResponseHeadersSnippet(Arrays.asList(headerWithName("Content-Type").description("one")))
54-
.document(this.operationBuilder.response().build());
45+
assertThatExceptionOfType(SnippetException.class).isThrownBy(
46+
() -> new ResponseHeadersSnippet(Arrays.asList(headerWithName("Content-Type").description("one")))
47+
.document(this.operationBuilder.response().build()))
48+
.withMessage("Headers with the following names were not found" + " in the response: [Content-Type]");
5549
}
5650

5751
@Test
5852
public void undocumentedResponseHeaderAndMissingResponseHeader() throws IOException {
59-
this.thrown.expect(SnippetException.class);
60-
this.thrown.expectMessage(
61-
endsWith("Headers with the following names were not found" + " in the response: [Content-Type]"));
62-
new ResponseHeadersSnippet(Arrays.asList(headerWithName("Content-Type").description("one")))
63-
.document(this.operationBuilder.response().header("X-Test", "test").build());
53+
assertThatExceptionOfType(SnippetException.class).isThrownBy(
54+
() -> new ResponseHeadersSnippet(Arrays.asList(headerWithName("Content-Type").description("one")))
55+
.document(this.operationBuilder.response().header("X-Test", "test").build()))
56+
.withMessageEndingWith(
57+
"Headers with the following names were not found in the response: [Content-Type]");
6458
}
6559

6660
}
Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2021 the original author or authors.
2+
* Copyright 2014-2022 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.
@@ -22,13 +22,12 @@
2222

2323
import org.junit.Rule;
2424
import org.junit.Test;
25-
import org.junit.rules.ExpectedException;
2625

2726
import org.springframework.restdocs.snippet.SnippetException;
2827
import org.springframework.restdocs.templates.TemplateFormats;
2928
import org.springframework.restdocs.testfixtures.OperationBuilder;
3029

31-
import static org.hamcrest.CoreMatchers.equalTo;
30+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
3231

3332
/**
3433
* Tests for failures when rendering {@link LinksSnippet} due to missing or undocumented
@@ -41,42 +40,40 @@ public class LinksSnippetFailureTests {
4140
@Rule
4241
public OperationBuilder operationBuilder = new OperationBuilder(TemplateFormats.asciidoctor());
4342

44-
@Rule
45-
public ExpectedException thrown = ExpectedException.none();
46-
4743
@Test
4844
public void undocumentedLink() throws IOException {
49-
this.thrown.expect(SnippetException.class);
50-
this.thrown.expectMessage(equalTo("Links with the following relations were not" + " documented: [foo]"));
51-
new LinksSnippet(new StubLinkExtractor().withLinks(new Link("foo", "bar")),
52-
Collections.<LinkDescriptor>emptyList()).document(this.operationBuilder.build());
45+
assertThatExceptionOfType(SnippetException.class)
46+
.isThrownBy(() -> new LinksSnippet(new StubLinkExtractor().withLinks(new Link("foo", "bar")),
47+
Collections.<LinkDescriptor>emptyList()).document(this.operationBuilder.build()))
48+
.withMessage("Links with the following relations were not documented: [foo]");
5349
}
5450

5551
@Test
5652
public void missingLink() throws IOException {
57-
this.thrown.expect(SnippetException.class);
58-
this.thrown.expectMessage(
59-
equalTo("Links with the following relations were not" + " found in the response: [foo]"));
60-
new LinksSnippet(new StubLinkExtractor(), Arrays.asList(new LinkDescriptor("foo").description("bar")))
61-
.document(this.operationBuilder.build());
53+
assertThatExceptionOfType(SnippetException.class)
54+
.isThrownBy(() -> new LinksSnippet(new StubLinkExtractor(),
55+
Arrays.asList(new LinkDescriptor("foo").description("bar")))
56+
.document(this.operationBuilder.build()))
57+
.withMessage("Links with the following relations were not found in the response: [foo]");
6258
}
6359

6460
@Test
6561
public void undocumentedLinkAndMissingLink() throws IOException {
66-
this.thrown.expect(SnippetException.class);
67-
this.thrown.expectMessage(equalTo("Links with the following relations were not"
68-
+ " documented: [a]. Links with the following relations were not" + " found in the response: [foo]"));
69-
new LinksSnippet(new StubLinkExtractor().withLinks(new Link("a", "alpha")),
70-
Arrays.asList(new LinkDescriptor("foo").description("bar"))).document(this.operationBuilder.build());
62+
assertThatExceptionOfType(SnippetException.class)
63+
.isThrownBy(() -> new LinksSnippet(new StubLinkExtractor().withLinks(new Link("a", "alpha")),
64+
Arrays.asList(new LinkDescriptor("foo").description("bar")))
65+
.document(this.operationBuilder.build()))
66+
.withMessage("Links with the following relations were not documented: [a]. Links with the following"
67+
+ " relations were not found in the response: [foo]");
7168
}
7269

7370
@Test
7471
public void linkWithNoDescription() throws IOException {
75-
this.thrown.expect(SnippetException.class);
76-
this.thrown.expectMessage(equalTo("No description was provided for the link with rel 'foo' and no"
77-
+ " title was available from the link in the payload"));
78-
new LinksSnippet(new StubLinkExtractor().withLinks(new Link("foo", "bar")),
79-
Arrays.asList(new LinkDescriptor("foo"))).document(this.operationBuilder.build());
72+
assertThatExceptionOfType(SnippetException.class)
73+
.isThrownBy(() -> new LinksSnippet(new StubLinkExtractor().withLinks(new Link("foo", "bar")),
74+
Arrays.asList(new LinkDescriptor("foo"))).document(this.operationBuilder.build()))
75+
.withMessage("No description was provided for the link with rel 'foo' and no title was available"
76+
+ " from the link in the payload");
8077
}
8178

8279
}

spring-restdocs-core/src/test/java/org/springframework/restdocs/operation/QueryStringParserTests.java

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,10 @@
1919
import java.net.URI;
2020
import java.util.Arrays;
2121

22-
import org.junit.Rule;
2322
import org.junit.Test;
24-
import org.junit.rules.ExpectedException;
2523

2624
import static org.assertj.core.api.Assertions.assertThat;
27-
import static org.hamcrest.CoreMatchers.equalTo;
25+
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
2826

2927
/**
3028
* Tests for {@link QueryStringParser}.
@@ -33,9 +31,6 @@
3331
*/
3432
public class QueryStringParserTests {
3533

36-
@Rule
37-
public ExpectedException thrown = ExpectedException.none();
38-
3934
private final QueryStringParser queryStringParser = new QueryStringParser();
4035

4136
@Test
@@ -76,9 +71,9 @@ public void encoded() {
7671

7772
@Test
7873
public void malformedParameter() {
79-
this.thrown.expect(IllegalArgumentException.class);
80-
this.thrown.expectMessage(equalTo("The parameter 'a=apple=avocado' is malformed"));
81-
this.queryStringParser.parse(URI.create("http://localhost?a=apple=avocado"));
74+
assertThatIllegalArgumentException()
75+
.isThrownBy(() -> this.queryStringParser.parse(URI.create("http://localhost?a=apple=avocado")))
76+
.withMessage("The parameter 'a=apple=avocado' is malformed");
8277
}
8378

8479
@Test

spring-restdocs-core/src/test/java/org/springframework/restdocs/operation/preprocess/PrettyPrintingContentModifierTests.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,9 @@
2323
import org.junit.Rule;
2424
import org.junit.Test;
2525

26-
import org.springframework.restdocs.testfixtures.OutputCapture;
26+
import org.springframework.restdocs.testfixtures.OutputCaptureRule;
2727

2828
import static org.assertj.core.api.Assertions.assertThat;
29-
import static org.hamcrest.Matchers.isEmptyString;
3029

3130
/**
3231
* Tests for {@link PrettyPrintingContentModifier}.
@@ -37,7 +36,7 @@
3736
public class PrettyPrintingContentModifierTests {
3837

3938
@Rule
40-
public OutputCapture outputCapture = new OutputCapture();
39+
public OutputCaptureRule outputCapture = new OutputCaptureRule();
4140

4241
@Test
4342
public void prettyPrintJson() throws Exception {
@@ -61,17 +60,17 @@ public void empytContentIsHandledGracefully() throws Exception {
6160
@Test
6261
public void nonJsonAndNonXmlContentIsHandledGracefully() throws Exception {
6362
String content = "abcdefg";
64-
this.outputCapture.expect(isEmptyString());
6563
assertThat(new PrettyPrintingContentModifier().modifyContent(content.getBytes(), null))
6664
.isEqualTo(content.getBytes());
65+
assertThat(this.outputCapture).isEmpty();
6766
}
6867

6968
@Test
7069
public void nonJsonContentThatInitiallyLooksLikeJsonIsHandledGracefully() throws Exception {
7170
String content = "\"abc\",\"def\"";
72-
this.outputCapture.expect(isEmptyString());
7371
assertThat(new PrettyPrintingContentModifier().modifyContent(content.getBytes(), null))
7472
.isEqualTo(content.getBytes());
73+
assertThat(this.outputCapture).isEmpty();
7574
}
7675

7776
@Test

0 commit comments

Comments
 (0)