In a Spring Boot test I need to compare/matching the result from a call to a rest controller with a Java entity previously created that have nested objects.
This is the Java entity:
@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;
...
The nested object Cliente have another nested object called 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;
This is the test method:
@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()));
When I run the test method I get the following error message:
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>
But in another test case (where I don't have nested objects) everything works fine:
@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()));
Why in the first test case JSON path "$[0].cliente" return null? What's the best way to do this?