mardi 1 septembre 2015

Hibernate Entity to DTO testing

I have hibernate (simplified) entity like so:

@Id
@GeneratedValue
private Long id;

@NotNull
private String prop1;

@NotNull
private int prop2;

I am mapping it to a DTO:

private Long id;

private String prop1;

private int prop2;

//getters/setters etc

I want to write a unit test to check the mapping went ok.

I thought the best approach would be:

  • Create a new test entity and load it with dummy data.
  • run mapping.
  • Assert expected values.

The problem I have with this is there is not Setter on the entity id as I believe it to best practice to keep this private? I had considered creating another constructor but wondered if there were any alternatives I am missing?

Any advice welcomed, also if I have the right strategy for testing, as manually populating the entities and entities within entities seems to be a bit ham fisted.

thanks,

Aucun commentaire:

Enregistrer un commentaire