Source:
<code>public void testCreateSelfJoinedRelationship_contentletAddedAsChild () throws DotDataException, DotSecurityException { Language language = languageAPI.getDefaultLanguage(); Structure structure = createStructure( "JUnit Test Structure_" + String.valueOf( new Date().getTime() ), "junit_test_structure_" + String.valueOf( new Date().getTime() ) ); Contentlet parent = createContentlet( structure, language, false ); Contentlet child = createContentlet( structure, language, false ); Relationship selfRelationship = createRelationShip(structure.getInode(), structure.getInode(), false ); List<Contentlet> contentRelationships = new ArrayList<>(); contentRelationships.add(child); contentletAPI.relateContent(parent, selfRelationship, contentRelationships, user, false ); ContentletRelationships contentletRelationships = contentletAPI.getAllRelationships( parent.getInode() , user, false); assertNotNull(contentletRelationships ); assertTrue(contentletRelationships.getRelationshipsRecords() != null && !contentletRelationships.getRelationshipsRecords().isEmpty() ); for (ContentletRelationshipRecords record : contentletRelationships.getRelationshipsRecords()) { if (record.isHasParent()) { assertNotNull(record.getRecords()); } } <START> contentletAPI.archive(child, user, false); <END> contentletAPI.archive(parent, user, false); contentletAPI.delete(child, user, false); contentletAPI.delete(parent, user, false); relationshipAPI.delete(selfRelationship); APILocator.getStructureAPI().delete(structure, user); }</code><technical_language>This clean in a finally section</technical_language>

Target:
public void testCreateSelfJoinedRelationship_contentletAddedAsChild () throws DotDataException, DotSecurityException { Contentlet parent = null; Contentlet child = null; Structure structure = null; Relationship selfRelationship = null; try { Language language = languageAPI.getDefaultLanguage(); structure = createStructure( "JUnit Test Structure_" + String.valueOf(new Date().getTime()), "junit_test_structure_" + String.valueOf(new Date().getTime())); parent = createContentlet(structure, language, false); child = createContentlet(structure, language, false); selfRelationship = createRelationShip(structure.getInode(), structure.getInode(), false); List<Contentlet> contentRelationships = new ArrayList<>(); contentRelationships.add(child); contentletAPI .relateContent(parent, selfRelationship, contentRelationships, user, false); ContentletRelationships contentletRelationships = contentletAPI .getAllRelationships(parent.getInode(), user, false); assertNotNull(contentletRelationships); assertTrue(contentletRelationships.getRelationshipsRecords() != null && !contentletRelationships.getRelationshipsRecords().isEmpty()); for (ContentletRelationshipRecords record : contentletRelationships .getRelationshipsRecords()) { if (record.isHasParent()) { assertNotNull(record.getRecords()); } } } finally { if (parent != null) { contentletAPI.archive(parent, user, false); contentletAPI.delete(parent, user, false); } if (child != null) { contentletAPI.archive(child, user, false); contentletAPI.delete(child, user, false); } if (selfRelationship != null) { relationshipAPI.delete(selfRelationship); } if (structure != null) { APILocator.getStructureAPI().delete(structure, user); } } }