gpt4 book ai didi

java - Jsonpath 在 Spring Boot 测试中为嵌套 JSON 对象返回 null

转载 作者:行者123 更新时间:2023-12-01 23:26:11 27 4
gpt4 key购买 nike

在 Spring Boot 测试中,我需要将调用剩余 Controller 的结果与之前创建的具有嵌套对象的 Java 实体进行比较/匹配。

这是 Java 实体:

@Entity
@Table(name="Referente")
public class Referente {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "idReferente", updatable = false, nullable = false)
private Integer idReferente;

@ManyToOne
@JoinColumn(name="idCliente")
private Cliente cliente;

@Column(name = "cognomeReferente")
private String cognomeReferente;

...

嵌套对象 Cliente 有另一个名为 Agente 的嵌套对象:

@Entity
@Table(name="Cliente")
public class Cliente {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "idCliente", updatable = false, nullable = false)
private Integer idCliente;

@Column(name = "nominativo")
private String nominativo;

@Column(name = "dataProssimoContatto")
private LocalDateTime dataProssimoContatto;

@ManyToOne
@JoinColumn(name = "idAgentePrimoContatto")
private Agente agentePrimoContatto;
@Entity
@Table(name="Agente")
public class Agente {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "idAgente", updatable = false, nullable = false)
private Integer idAgente;

@Column(name = "nome")
private String nome;

@Column(name = "cognome")
private String cognome;

@Column(name = "telefono")
private String telefono;

这是测试方法:

@Test
public void getAllReferente_Test() throws Exception {

initializeData();

mvc.perform(get("/referente").contentType(MediaType.APPLICATION_JSON))

.andExpect(status().isOk())
.andExpect(jsonPath("$[0].idReferente").value(referente1.getIdReferente()))
// Here the error
.andExpect(jsonPath("$[0].cliente").value(referente1.getCliente()))
.andExpect(jsonPath("$[0].cognomeReferente").value(referente1.getCognomeReferente()));

当我运行测试方法时,我收到以下错误消息:

java.lang.AssertionError: JSON path "$[0].cliente" 

expected:<Cliente [idCliente=1, nominativo=NOMINATIVOCLIENTE1, dataProssimoContatto=2019-10-16T16:19:59.111111111, agentePrimoContatto=Agente [idAgente=1, nome=NOME1, cognome=COGNOME1, telefono=11111, mail=MAIL1, isRemovibile=null], dataPrimoContatto=2019-07-20, agenteInEssere=Agente [idAgente=1, nome=NOME1, cognome=COGNOME1, telefono=11111, mail=MAIL1, isRemovibile=null], consensoPrivacy=true, indirizzo=VIA INDIRIZZO1, cap=11111, citta=CITTA1, provincia=PR, settoreMerceologico=SettoreMerceologico [idSettoreMerceologico=1, descrizione=SETTOREMERCEOLOGIOCO], statoCliente=StatoCliente [idStatoCliente=1, descrizione=NUOVO]]>

but was:<null>

但在另一个测试用例中(我没有嵌套对象)一切正常:

@Test
public void getAllCliente2Call() throws Exception {

initializeData();
mvc.perform(get("/cliente/data/" + dataProssimoContatto).contentType(MediaType.APPLICATION_JSON))

.andExpect(status().isOk())
.andExpect(jsonPath("$").isNotEmpty())
.andExpect(jsonPath("$", hasSize(2)))
.andExpect(jsonPath("$[0].idCliente").value(cliente2.getIdCliente()))
.andExpect(jsonPath("$[0].nominativo").value(cliente2.getNominativo())
.andExpect(jsonPath("$[0].dataProssimoContatto",is(cliente2.getDataProssimoContatto().toString())))
// Here works fine (not have nested objects in agente)
.andExpect(jsonPath("$[0].agentePrimoContatto").value(cliente2.getAgentePrimoContatto()));

为什么在第一个测试用例 JSON 路径“$[0].cliente”中返回 null?最好的方法是什么?

最佳答案

已解决

我按照其他线程解决了:

Matching object in JSON with jsonpath in Spring Boot Test

代码如下:

private <T> T asParsedJson(Object obj) throws JsonProcessingException {
String json = new ObjectMapper().writeValueAsString(obj);
return JsonPath.read(json, "$");
}

然后

.andExpect(jsonPath("$[0].cliente").value(equalTo(asParsedJson(referente1.getCliente()))))

关于java - Jsonpath 在 Spring Boot 测试中为嵌套 JSON 对象返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58306822/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com