【发布时间】:2017-06-03 04:10:25
【问题描述】:
我在 spring-data-jpa 之上使用 spring-data-rest。
我正在编写集成测试以使用 MockMvc 和内存测试数据库测试我的 SDR API。
到目前为止,我专注于 GET,但现在我正在考虑为 POST、PUT 和 PATCH 请求创建测试,看起来我必须编写自己的 JSON 生成器(可能基于 GSON)为了获取相关实体的 URL 之类的东西,例如
public class ForecastEntity {
@RestResource
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "UNITID", referencedColumnName = "ID")
private UnitEntity unit;
}
在我的测试中,我会用父母/孩子建立一个实体:
ForecastEntity forecast = new ForecastEntity();
forecast.setTitle("test-forecast");
forecast.setUnit(new UnitEntity("test-unit"));
应该像这样生成 JSON:
{
"title" : "test-forecast",
"unit" : "http://localhost/units/test-unit"
}
SDR 中是否有可用于从测试中手动初始化的实体生成 JSON 的功能?
【问题讨论】:
-
也许Spring Restbucks - SDR作者的例子可以提供帮助:MoneySerializationTest
标签: java json integration-testing spring-data-rest spring-test