Issue
So I have this project I'm working on for academic purpose, we are asked to add Log4j and JUnit tool to it . So After I added the log4j lines and tested the delete method at first it was working but then it won't delete anything anymore.
it would be great if someone could help me detect the problem because I tried changing the code but still the deleting isn't happening.
so this is my service '''
@Service
public class DepartementServiceImpl implements IDepartementService {
@Autowired
DepartementRepository deptRepoistory;
private static final Logger l = LogManager.getLogger(DepartementServiceImpl.class);
@Transactional
public int deleteDepartementById(int depId) {
try {
l.info("In deleteDepartmentById : ");
l.info(" department id= " + depId);
deptRepoistory.delete(deptRepoistory.findById(depId).get());
l.info("Out of deleteDepartmentById. ");
return 0 ;
}
catch (Exception e) {
l.error("erreur In deleteDepartementById() : could not be found " + e);
return -1 ; }
}
}
'''
and this is the testing class
'''
@RunWith(SpringRunner.class)
@SpringBootTest
public class DepartementTest {
@Autowired
IDepartementService DepService;
@Autowired
DepartementRepository deptRepoistory;
@Test
public void testDeleteDepartementById(){
int depId = 30 ;
DepService.deleteDepartementById(depId);
assertNull(DepService.getDepartmentById(depId));
}
}
'''
and this is the repository '''
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
import tn.esprit.spring.entities.Departement;
@Repository
public interface DepartementRepository extends CrudRepository<Departement, Integer> {
}
'''
and this what console show when I run it as JUnit test :
'''
2020-nov.-02 10:48:09.237 [32m[INFO ][0;39m [36mtn.esprit.spring.DepartementTest[0;39m - [34mStarted DepartementTest in 4.861 seconds (JVM running for 5.623)[0;39m
2020-nov.-02 10:48:09.358 [32m[INFO ][0;39m [36mtn.esprit.spring.services.DepartementServiceImpl[0;39m - [34mIn deleteDepartmentById : [0;39m
2020-nov.-02 10:48:09.368 [32m[INFO ][0;39m [36mtn.esprit.spring.services.DepartementServiceImpl[0;39m - [34m department id= 30[0;39m
Hibernate: select departemen0_.id as id1_1_0_, departemen0_.entreprise_id as entrepri3_1_0_, departemen0_.name as name2_1_0_, entreprise1_.id as id1_4_1_, entreprise1_.name as name2_4_1_, entreprise1_.raison_social as raison_s3_4_1_ from departement departemen0_ left outer join entreprise entreprise1_ on departemen0_.entreprise_id=entreprise1_.id where departemen0_.id=?
Hibernate: select departemen0_.entreprise_id as entrepri3_1_0_, departemen0_.id as id1_1_0_, departemen0_.id as id1_1_1_, departemen0_.entreprise_id as entrepri3_1_1_, departemen0_.name as name2_1_1_ from departement departemen0_ where departemen0_.entreprise_id=?
2020-nov.-02 10:48:09.458 [32m[INFO ][0;39m [36mtn.esprit.spring.services.DepartementServiceImpl[0;39m - [34mOut of deleteDepartmentById.
'''
and this is the JUnit results which is right because the delete didn't happen, because it is supposed to find it null after deleting but the supposed deleted object is still there.
'''
java.lang.AssertionError: expected null, but was:<tn.esprit.spring.entities.Departement@2c1a8529>
at org.junit.Assert.fail(Assert.java:88)
at org.junit.Assert.failNotNull(Assert.java:755)
at org.junit.Assert.assertNull(Assert.java:737)
at org.junit.Assert.assertNull(Assert.java:747)
at tn.esprit.spring.DepartementTest.testDeleteDepartementById(DepartementTest.java:75)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.springframework.test.context.junit4.statements.RunBeforeTestExecutionCallbacks.evaluate(RunBeforeTestExecutionCallbacks.java:74)
at org.springframework.test.context.junit4.statements.RunAfterTestExecutionCallbacks.evaluate(RunAfterTestExecutionCallbacks.java:84)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:251)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
'''
My Entity
'''
@Entity
public class Departement implements Serializable {
private static final long serialVersionUID = -357738161698377833L;
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private int id;
private String name;
//@JsonManagedReference
@JsonIgnore
@ManyToMany
private List<Employe> employes;
@OneToMany(mappedBy="departement")
private List<Mission> missions;
@ManyToOne
private Entreprise entreprise;
public Departement() {
super();
}
public Departement(String name) {
this.name = name;
}
}
'''
Update
So I Added new department without assigning an enterprise to it and the delete is back to work so I assume that the problem lays with the association between the Departement Entity and the Enterprise Entity
Entreprise Entity
'''
@Entity
public class Entreprise implements Serializable{
/**
*
*/
private static final long serialVersionUID = 3152690779535828408L;
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private int id;
private String name;
private String raisonSocial;
@OneToMany(mappedBy="entreprise",
cascade = {CascadeType.PERSIST, CascadeType.REMOVE},
fetch=FetchType.EAGER)
private List<Departement> departements = new ArrayList<>();
....
}
'''
UPDATE :
So this problem is cleared , I just added another method to remove the entreprise from the Department( disaffectDepartfromEnt) and the delete worked perfectly after that
Solution
Solved:
So I Added new department without assigning an enterprise to it and the delete is back to work so I assume that the problem lays with the association between the Departement Entity and the Enterprise Entity So now this problem is cleared , I just added another method to remove the entreprise from the Department( disaffectDepartfromEnt) and the delete worked perfectly after that.
Answered By - R_pri