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
Jaynandan Prasad 8Jaynandan Prasad 8 

How to unit test salesforce to salesforce connection via trigger

How to write the test class for the trigger to automate salesforce to salesforce connection via apex-

here's the trigger
 
trigger OrgCaseSync on Case (after insert, after update) {

PartnerNetworkConnection conn = [select Id, ConnectionStatus, ConnectionName from PartnerNetworkConnection  where ConnectionStatus = 'Accepted'];
        List<PartnerNetworkRecordConnection> recordConnectionToInsert  = new List<PartnerNetworkRecordConnection>  ();
        for (Case cc : Trigger.new){
            PartnerNetworkRecordConnection newrecord = new PartnerNetworkRecordConnection();

            newrecord.ConnectionId = conn.Id;
            newrecord.LocalRecordId = cc.id;  
            newrecord.SendClosedTasks = false;
            newrecord.SendOpenTasks = false;
            newrecord.SendEmails = false;
            recordConnectionToInsert.add(newrecord);
        }
        if (recordConnectionToInsert.size() > 0){
            System.debug('>>> Sharing ' + recordConnectionToInsert.size() + ' records');
            insert recordConnectionToInsert;
        }

 
Tavant TechnologiesTavant Technologies
You can test the code as if it is normal apex please find the class and test class below it is covering 100%
  
Controller Code
 
public class  testpartnerConnection{



  public void connect(){
  
    List<PartnerNetworkConnection> connMap = new List<PartnerNetworkConnection>([select Id, ConnectionStatus, ConnectionName from PartnerNetworkConnection ]);
for(PartnerNetworkConnection network : connMap) {    
     PartnerNetworkRecordConnection newrecord = new PartnerNetworkRecordConnection();

    newrecord.ConnectionId = '04P280000008OTXEA2';
    newrecord.LocalRecordId = '00128000002tqtD';  
    newrecord.RelatedRecords = 'Contact,Opportunity';
    newrecord.SendClosedTasks = true;
    newrecord.SendOpenTasks = true;
    newrecord.SendEmails = true;   

    insert newrecord;   
}    
  }

}

Test class code.

@isTest 
private class HelloWorldTestClass {
    static testMethod void validateHelloWorld() {
      testpartnerConnection t = new testpartnerConnection();
      t.connect();
    }
}
ManojjenaManojjena
Hi Janarden,
Try with below code it wil help .
 
@isTest
public class OrgCaseSyncTest{
  public static testMethod void unitTest(){
     Case cs=new Case();
     cs.Status='New';
     cs.Origin='Phone';
	 Test.startTest();
		 Insert cs;
		 cs=[SELECT id,Status FROM Case WHERE Id=cs.Id];
		 System.assertEquals(cs.Status,'New');
		 update cs;
	 Test.stopTest();
  }
}

Thanks 
Manoj
Stephen PriceStephen Price
Just an FYI for people searching for this answer Manoj, I think you forgot a colon before the cs.Id in your SELECT statement.  I believe it should be 

[SELECT id, Status FROM Case WHERE Id = :cs.Id]