function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
SwkannanSwkannan 

Test Method Failing

Hello

 

We have a Reciprocal Relationships Package that extends the functionality of the relationships app in non-profit salesforce pack.

we have attempted to include the type of relationship between two contacts (eg. Mother, Daughter)

 

I am getting this error when I try to install the package.

 

 

Problem Component Detailtest_relationships.TestOrphanedRelDelete()

Apex Classes(01pU0000000HqgO)System.Exception: Assertion Failed: Expected: 0, Actual: 1
Class.TEST_Relationships.TestOrphanedRelDelete: line 179, column 3
External entry point



 

  /// <name> TestOrphanedRelDelete </name>
  /// <summary> test method for relationships and deleting the mirror record </summary>
  static testMethod void TestOrphanedRelDelete() {  
    Contact firstContact = new Contact (
      FirstName='Joe',
      LastName='Johanssen'
    );
    insert firstContact;

    Contact secondContact = new Contact (
      FirstName='Bobby',
      LastName='Johanssen'
    );
    insert secondContact;

    Relationship_Type__c firstRelType = new Relationship_Type__c (
      Name='&&Test_Relationship 1'
    );
    
    Relationship_Type__c secondRelType = new Relationship_Type__c (
      Name='&&Test_Relationship 2',
      Reverse_Relationship_Name__c = firstRelType.Id
    );
    
    firstRelType.Reverse_Relationship_Name__c = secondRelType.Id;
    
    insert firstRelType;
    insert secondRelType;

    Relationship__c[] crel = new Relationship__c[]{
      new Relationship__c(contact__c = firstContact.id, Relatedcontact__c = secondContact.id, Relationship_Type__c=firstRelType.Id)
    };
    insert crel;
    
    // check that rel gets created
    Relationship__c crel_original = [select id, Description__c,Relationship_Type__c,reciprocalrelationship__c from Relationship__c where id=:crel[0].Id];
    //relationship should be set right
    System.assertEquals(crel_original.Relationship_Type__c, firstRelType.Id);

    // check for mirror rel
    Relationship__c crel_mirror = [select id, Description__c, reciprocalrelationship__c, Relationship_Type__c from Relationship__c where reciprocalrelationship__c=:crel_original.Id];
    //mirror should reference the original
    System.assertEquals(crel_mirror.reciprocalrelationship__c, crel_original.Id);
    Contact secondContactForDelete = [select id from contact where id=:secondContact.id];
    Test.startTest();
    delete secondContactForDelete;
    Test.stopTest();
    // test change to the rel
    Relationship__c[] orphanedCrel = [select id from Relationship__c where id = :crel_original.Id];
    //original should have updated relationship
    System.assertEquals(0, orphanedCrel.size());
  }
}

Can someone tell me why this is happening?

 

Thanks in advance!

ashish raiashish rai

Hello,

      Well i think the exception is because you are assigning the Reverse_Relationship_Name__c=firstRelType.Id without inserting the firstRelType. So you have to first off all insert thet value. Similarly you have used  firstRelType.Reverse_Relationship_Name__c  =  secondRelType.Id without inserting the secondRelType. So you have to insert that value first. Your code should be like this:

 

static testMethod void TestOrphanedRelDelete()

 {     

     Contact firstContact = new Contact (      FirstName='Joe',      LastName='Johanssen'    );   

     insert firstContact;
    Contact secondContact = new Contact (      FirstName='Bobby',      LastName='Johanssen'    );   

     insert secondContact;
     Relationship_Type__c firstRelType = new Relationship_Type__c (      Name='&&Test_Relationship 1'    );       

     insert firstRelType;

     Relationship_Type__c secondRelType = new Relationship_Type__c (      Name='&&Test_Relationship 2',                            

     Reverse_Relationship_Name__c = firstRelType.Id    );           

     insert secondRelType;

     firstRelType.Reverse_Relationship_Name__c = secondRelType.Id;   

     update firstRelType;    
    Relationship__c[] crel = new Relationship__c[]

    {      new Relationship__c(contact__c = firstContact.id, Relatedcontact__c = secondContact.id,        

           Relationship_Type__c=firstRelType.Id)   

    };   

   insert crel;        // check that rel gets created   

   Relationship__c crel_original = [select id,   Description__c,Relationship_Type__c,reciprocalrelationship__c from    

   Relationship__c where id=:crel[0].Id];    //relationship should be set right        System.assertEquals(crel_original.Relationship_Type__c, firstRelType.Id);
    // check for mirror rel    Relationship__c crel_mirror = [select id, Description__c, reciprocalrelationship__c, Relationship_Type__c from Relationship__c where reciprocalrelationship__c=:crel_original.Id];    //mirror should reference the original    System.assertEquals(crel_mirror.reciprocalrelationship__c, crel_original.Id);    Contact secondContactForDelete = [select id from contact where id=:secondContact.id];    Test.startTest();    delete secondContactForDelete;    Test.stopTest();    // test change to the rel    Relationship__c[] orphanedCrel = [select id from Relationship__c where id = :crel_original.Id];    //original should have updated relationship    System.assertEquals(0, orphanedCrel.size());  }}

 

Think this will resolve four exception.

SwkannanSwkannan

Hello 

 

Thank you for your response.

 

I made all the changes that you mentioned and I am still getting this error :(

When I run all the tests everything is fine. 

When I package it and install in the test environment I am getting this error.

 

I tried installing the package by ignoring Apex test failures and it works but I read that it is not best practice.

is this something you can help me with??

 

Thank you very much