Skip to content

Commit

Permalink
Add support for JSON assertions using JSON compare
Browse files Browse the repository at this point in the history
This commit moves JSON content AssertJ support from Spring Boot.

See gh-21178

Co-authored-by: Brian Clozel <[email protected]>
  • Loading branch information
snicoll and bclozel committed Mar 15, 2024
1 parent 214a54d commit 97ebc43
Show file tree
Hide file tree
Showing 14 changed files with 1,143 additions and 0 deletions.
1 change: 1 addition & 0 deletions spring-test/spring-test.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ dependencies {
optional("org.apache.groovy:groovy")
optional("org.apache.tomcat.embed:tomcat-embed-core")
optional("org.aspectj:aspectjweaver")
optional("org.assertj:assertj-core")
optional("org.hamcrest:hamcrest")
optional("org.htmlunit:htmlunit") {
exclude group: "commons-logging", module: "commons-logging"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.test.json;

import org.assertj.core.api.AssertProvider;

import org.springframework.lang.Nullable;
import org.springframework.util.Assert;

/**
* JSON content usually created from a JSON tester. Generally used only to
* {@link AssertProvider provide} {@link JsonContentAssert} to AssertJ
* {@code assertThat} calls.
*
* @author Phillip Webb
* @author Diego Berrueta
* @since 6.2
*/
public final class JsonContent implements AssertProvider<JsonContentAssert> {

private final String json;

@Nullable
private final Class<?> resourceLoadClass;

/**
* Create a new {@link JsonContent} instance.
* @param json the actual JSON content
* @param resourceLoadClass the source class used to load resources
*/
JsonContent(String json, @Nullable Class<?> resourceLoadClass) {
Assert.notNull(json, "JSON must not be null");
this.json = json;
this.resourceLoadClass = resourceLoadClass;
}

/**
* Use AssertJ's {@link org.assertj.core.api.Assertions#assertThat assertThat}
* instead.
*/
@Override
public JsonContentAssert assertThat() {
return new JsonContentAssert(this.json, this.resourceLoadClass, null);
}

/**
* Return the actual JSON content string.
* @return the JSON content
*/
public String getJson() {
return this.json;
}

@Override
public String toString() {
return "JsonContent " + this.json;
}

}
Loading

0 comments on commit 97ebc43

Please sign in to comment.