Skip to content

Commit 97ebc43

Browse files
snicollbclozel
andcommitted
Add support for JSON assertions using JSON compare
This commit moves JSON content AssertJ support from Spring Boot. See gh-21178 Co-authored-by: Brian Clozel <[email protected]>
1 parent 214a54d commit 97ebc43

File tree

14 files changed

+1143
-0
lines changed

14 files changed

+1143
-0
lines changed

Diff for: spring-test/spring-test.gradle

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ dependencies {
3232
optional("org.apache.groovy:groovy")
3333
optional("org.apache.tomcat.embed:tomcat-embed-core")
3434
optional("org.aspectj:aspectjweaver")
35+
optional("org.assertj:assertj-core")
3536
optional("org.hamcrest:hamcrest")
3637
optional("org.htmlunit:htmlunit") {
3738
exclude group: "commons-logging", module: "commons-logging"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* Copyright 2002-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.test.json;
18+
19+
import org.assertj.core.api.AssertProvider;
20+
21+
import org.springframework.lang.Nullable;
22+
import org.springframework.util.Assert;
23+
24+
/**
25+
* JSON content usually created from a JSON tester. Generally used only to
26+
* {@link AssertProvider provide} {@link JsonContentAssert} to AssertJ
27+
* {@code assertThat} calls.
28+
*
29+
* @author Phillip Webb
30+
* @author Diego Berrueta
31+
* @since 6.2
32+
*/
33+
public final class JsonContent implements AssertProvider<JsonContentAssert> {
34+
35+
private final String json;
36+
37+
@Nullable
38+
private final Class<?> resourceLoadClass;
39+
40+
/**
41+
* Create a new {@link JsonContent} instance.
42+
* @param json the actual JSON content
43+
* @param resourceLoadClass the source class used to load resources
44+
*/
45+
JsonContent(String json, @Nullable Class<?> resourceLoadClass) {
46+
Assert.notNull(json, "JSON must not be null");
47+
this.json = json;
48+
this.resourceLoadClass = resourceLoadClass;
49+
}
50+
51+
/**
52+
* Use AssertJ's {@link org.assertj.core.api.Assertions#assertThat assertThat}
53+
* instead.
54+
*/
55+
@Override
56+
public JsonContentAssert assertThat() {
57+
return new JsonContentAssert(this.json, this.resourceLoadClass, null);
58+
}
59+
60+
/**
61+
* Return the actual JSON content string.
62+
* @return the JSON content
63+
*/
64+
public String getJson() {
65+
return this.json;
66+
}
67+
68+
@Override
69+
public String toString() {
70+
return "JsonContent " + this.json;
71+
}
72+
73+
}

0 commit comments

Comments
 (0)