Half Ass Dev Half Ass Dev -4 years ago 101
Java Question

JUnit Test on soft delete

I am writing JUnit for a RestFul API written in Spring boot.

The API has a feature called "soft delete", which is having a database column called "active", if user delete the object, the active is set to 0, to indicated this object has been deleted.

Here is my Junit test code for the soft delete. To my surprise, the Junit result shows the active is always 1 even after I delete the object.

enter image description here

Here is the delete code. As you can see, it just updates the "active" and set it to false.

@RepositoryRestResource(collectionResourceRel = "manufacturers", path = "manufacturers")
public interface ManufacturerRepository extends CrudRepository<Manufacturer, String> {

@Query("update Manufacturer m set m.active=false where m.id=: {#m.getManufacturerId()}")
void delete(@Param("m") Manufacturer m);


Since I can see in my database table the active is set to 0, I am sure my test code is wrong. Any idea on why this is happening?


Thanks for the advice. I have updated the code to get a fresh copy from database. Now the active is false, and my code now pass the test.

enter image description here

Answer Source

It looks like you don't get the manufacturer after "deleting" it in the DB.

Unless you update your your Manufacturer in repository.delete(manufacturer), you will have to call a getter to get the updated version of your object from your database.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download