类 BasicJsonTester
- java.lang.Object
- org.springframework.boot.test.json.BasicJsonTester
public class BasicJsonTester extends Object
AssertJ based JSON tester that works with basic JSON strings. Allows testing of JSON payloads created from any source, for example:public class ExampleObjectJsonTests { private BasicJsonTester json = new BasicJsonTester(getClass()); @Test public void testWriteJson() throws IOException { assertThat(json.from("example.json")).extractingJsonPathStringValue("@.name") .isEqualTo("Spring"); } }
SeeAbstractJsonMarshalTester
for more details.- 从以下版本开始:
- 1.4.0
构造器概要
构造器 限定符 构造器 说明 protected
BasicJsonTester()
Create a new uninitializedBasicJsonTester
instance.BasicJsonTester(Class<?> resourceLoadClass)
Create a newBasicJsonTester
instance that will load resources as UTF-8.BasicJsonTester(Class<?> resourceLoadClass, Charset charset)
Create a newBasicJsonTester
instance.
方法概要
所有方法 实例方法 具体方法 修饰符和类型 方法 说明 JsonContent<Object>
from(byte[] source)
Create JSON content from the specified JSON bytes.JsonContent<Object>
from(File source)
Create JSON content from the specified JSON file.JsonContent<Object>
from(InputStream source)
Create JSON content from the specified JSON input stream.JsonContent<Object>
from(CharSequence source)
Create JSON content from the specified String source.JsonContent<Object>
from(String path, Class<?> resourceLoadClass)
Create JSON content from the specified resource path.JsonContent<Object>
from(org.springframework.core.io.Resource source)
Create JSON content from the specified JSON resource.protected void
initialize(Class<?> resourceLoadClass, Charset charset, org.springframework.core.ResolvableType type)
Initialize the marshal tester for use.protected void
initialize(Class<?> resourceLoadClass, org.springframework.core.ResolvableType type)
Initialize the marshal tester for use, configuring it to load JSON resources as UTF-8.
构造器详细资料
BasicJsonTester
protected BasicJsonTester()
Create a new uninitializedBasicJsonTester
instance.
BasicJsonTester
public BasicJsonTester(Class<?> resourceLoadClass)
Create a newBasicJsonTester
instance that will load resources as UTF-8.- 参数:
resourceLoadClass
- the source class used to load resources
BasicJsonTester
public BasicJsonTester(Class<?> resourceLoadClass, Charset charset)
Create a newBasicJsonTester
instance.- 参数:
resourceLoadClass
- the source class used to load resourcescharset
- the charset used to load resources- 从以下版本开始:
- 1.4.1
方法详细资料
initialize
protected final void initialize(Class<?> resourceLoadClass, org.springframework.core.ResolvableType type)
Initialize the marshal tester for use, configuring it to load JSON resources as UTF-8.- 参数:
resourceLoadClass
- the source class used when loading relative classpath resourcestype
- the type under test
initialize
protected final void initialize(Class<?> resourceLoadClass, Charset charset, org.springframework.core.ResolvableType type)
Initialize the marshal tester for use.- 参数:
resourceLoadClass
- the source class used when loading relative classpath resourcescharset
- the charset used when loading relative classpath resourcestype
- the type under test- 从以下版本开始:
- 1.4.1
from
public JsonContent<Object> from(CharSequence source)
Create JSON content from the specified String source. The source can contain the JSON itself or, if it ends with.json
, the name of a resource to be loaded usingresourceLoadClass
.- 参数:
source
- JSON content or a.json
resource name- 返回:
- the JSON content
from
public JsonContent<Object> from(String path, Class<?> resourceLoadClass)
Create JSON content from the specified resource path.- 参数:
path
- the path of the resource to loadresourceLoadClass
- the source class used to load the resource- 返回:
- the JSON content
from
public JsonContent<Object> from(byte[] source)
Create JSON content from the specified JSON bytes.- 参数:
source
- the bytes of JSON- 返回:
- the JSON content
from
public JsonContent<Object> from(File source)
Create JSON content from the specified JSON file.- 参数:
source
- the file containing JSON- 返回:
- the JSON content
from
public JsonContent<Object> from(InputStream source)
Create JSON content from the specified JSON input stream.- 参数:
source
- the input stream containing JSON- 返回:
- the JSON content
from
public JsonContent<Object> from(org.springframework.core.io.Resource source)
Create JSON content from the specified JSON resource.- 参数:
source
- the resource containing JSON- 返回:
- the JSON content