lundi 13 juin 2016

Updating Entity Works Via Running App, Fails in Tests

Spring 1.3.5

I've done all I can to verify this; the JSON being passed in via Postman is the same JSON I'm generating from my test. Updating the entity via Postman works fine. Updating via mockMvc is failing. First, it was failing with a 400 and I was getting the following error:

Could not read document: Already had POJO for id (java.lang.Long) [[ObjectId: key=1, type=com.fasterxml.jackson.databind.deser.impl.PropertyBasedObjectIdGenerator, scope=java.lang.Object]] (through reference chain: com.foo.Department["id"])

After doing some research, I ended up adding scope=Department.class to Department's @JsonIdentityInfo. This resolved that issue (although I really don't understand why) and now it fails because it claims the @OneToOne relationship property is null, even though when I look at the entity via the debugger is is right there.

Here is my Department Entity:

@Entity
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property="id", scope = Department.class)
@JsonRootName(plural = "departments", singular = "department")
public class Department {
  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  private Long id;

  @NotNull
  @Size(min = 5, max = 150)
  private String name;

  @ManyToOne(fetch = FetchType.LAZY)
  @JoinColumn(name = "brand_id")
  @JsonBackReference("brand-departments")
  private Brand brand;

  @OneToOne(fetch = FetchType.LAZY)
  @NotNull
  @JoinColumn(name = "department_group_id")
  private DepartmentGroup departmentGroup;

  @NotNull
  @Size(max = 10)
  private String shortDescription;

  // getters and setters

}

Again, everything works just fine via bootRun and posting the JSON from Postman. It only breaks when running my test. Here is my test:

@Test
public void successfulUpdateDepartment() throws Exception {
  Department existingDepartment = departmentService.first();
  UpdateDepartment department = new UpdateDepartment(existingDepartment.getId(), "Chngd Dep Y", existingDepartment.getShortDescription(), new DepartmentGroupId(departmentGroup.getId()));

  mockMvc.perform(put(URL)
       .contentType(contentType)
       .content(this.json(department)))
       .andExpect(status().isOk());
}

Aucun commentaire:

Enregistrer un commentaire