0

I have a simple model class:

@Entity
public class Task {

  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  private Long id;

  @Size(min = 1, max = 80)
  @NotNull
  private String text;

  @NotNull
  private boolean isCompleted;

And here is my Spring Rest Data repository:

@CrossOrigin // TODO: configure specific domains
@RepositoryRestResource(collectionResourceRel = "task", path 
= "task")
public interface TaskRepository extends CrudRepository<Task, 
Long> {

}

So just as a sanity check, I was creating some tests to verify the end-points that are automatically created. post, delete, and get works just fine. I am however unable to properly update the isCompleted property.

Here are my test methods. The FIRST one passes no problem, but the SECOND one fails.

    @Test
    void testUpdateTaskText() throws Exception {
      Task task1 = new Task("task1");
      taskRepository.save(task1);

      // update task text and hit update end point
      task1.setText("updatedText");
      String json = gson.toJson(task1);
      this.mvc.perform(patch("/task/1")
            .contentType(MediaType.APPLICATION_JSON_UTF8)
            .content(json))
            .andExpect(status().isNoContent());

      // Pull the task from the repository and verify text="updatedText"
      Task updatedTask = taskRepository.findById((long) 1).get();
      assertEquals("updatedText", updatedTask.getText());
}

    @Test
    void testUpdateTaskCompleted() throws Exception {
      Task task1 = new Task("task1");
      task1.setCompleted(false);
      taskRepository.save(task1);

      // ensure repository properly stores isCompleted = false
      Task updatedTask = taskRepository.findById((long) 1).get();
      assertFalse(updatedTask.isCompleted());

      //Update isCompleted = true and hit update end point
      task1.setCompleted(true);
      String json = gson.toJson(task1);
      this.mvc.perform(patch("/task/1")
            .contentType(MediaType.APPLICATION_JSON_UTF8)
            .content(json))
            .andExpect(status().isNoContent());

      // Pull the task from the repository and verify isCompleted=true
      updatedTask = taskRepository.findById((long) 1).get();
      assertTrue(updatedTask.isCompleted());
}

EDIT: Modified test methods to be clear.

btbam91
  • 578
  • 2
  • 8
  • 20

1 Answers1

1

Finally figured out. Turns out the getter and setter in my model class was named incorrectly.

They should have been:

    public boolean getIsCompleted() {
    return isCompleted;
}

    public void setIsCompleted(boolean isCompleted) {
    this.isCompleted = isCompleted;
}

Found the answer per this SO Post: JSON Post request for boolean field sends false by default

btbam91
  • 578
  • 2
  • 8
  • 20