0

I have a short question about EntityManager. I have a method updateAdmin(Admin a). Behaviour: This method should update an existing Admin in my database. Return true if an admin exist an the update was successful. Return false if the Admin that has to be updated does not exist in the database.

Now I want to write a JUnit test method. I can check that the updateAdmin(Admin a) method returns a true if the Admin is already in the database and the update was successful.

I also can check that the method returns false if the Admin does not exist. But how can I check that the database was not changed and the update was not successful??

Thanks for your answer! :)

Gimby
  • 5,095
  • 2
  • 35
  • 47
Momo Saibak
  • 37
  • 1
  • 9
  • have to tried `mysqli::begin_transaction ([ int $flags [, string $name ]] )` – Abdul Manan Apr 22 '15 at 11:13
  • I would assume you get an exception when the database update statement blows up. You can test that right now by for example leaving a property which is not-nullable as null. – Gimby Apr 22 '15 at 12:08

1 Answers1

0

You can try find()-ing the admin at first. If it doesn't exist already return false or perform a merge() and return true. Assuming you are using JPA EntityManager this should work

public boolean updateAdmin(Admin a) {
    Admin existingAdmin = entityManager.find(a.getId(), Admin.class);
    if (existingAdmin == null) {
        return false;
    }

    em.merge(a);

    return true;
}
mushfek0001
  • 3,845
  • 1
  • 21
  • 20