+ Start a Discussion
Raja Bipin Chandra  M BRaja Bipin Chandra M B 

Test method for Controller extension for Chatter : ConnectAPI.ChatterMessages.sendMessage

Hello Folks,

I am finding hard to write a test method for the below code, Can somebody give me a headstart ? 
 
public with sharing class Opportunitymanager {
    
    public String body { get; set; }
    
    public Opportunity opp;
    public User usr;
    Id oid;
    
    
    
    
    public Opportunitymanager(ApexPages.StandardController stdController) {
        opp = (Opportunity)stdController.getRecord(); 
        oid = apexpages.currentpage().getparameters().get('ownerid');
        
        usr = [select id,FirstName,LastName,Email,Manager.Id,Manager.Manager.Id,Manager.Manager.FirstName,Manager.Manager.LastName,Manager.FirstName,Manager.LastName,Manager.Email from User where Id= :oid];  
    }


    public User  getuser() {
        return usr;
    }
    
      public PageReference submit() {
    ConnectAPI.ChatterMessages.sendMessage('Related to Opportunity: '+opp.Name+'\n'+'Link: https://emea.salesforce.com/'+opp.id+'\n'+body,usr.id+','+usr.Manager.Id+','+opp.Territory_Manager__c);
   
    PageReference returnPage = new ApexPages.StandardController(opp).view();
   
    returnPage.setRedirect(true);
     ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Info, 'Message successfully sent'));
    return new PageReference('javascript:window.close()');
    //return returnPage;
      } 


}

 
ManojjenaManojjena
Hi Raja ,

Try with below code it will help .
@isTest(SeeAllData=True)
public class TestOpportunitymanager {
	public static testmethod void unitTest(){
		Profile p = [SELECT Id FROM Profile WHERE Name='System Administrator']; 
        
        User usr = new User(Alias = 'sys', Email='systemadmin@testorg.com', 
                          EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', 
                          LocaleSidKey='en_US', ProfileId = p.Id, 
                          TimeZoneSidKey='America/Los_Angeles', UserName='sysadmintest@ss.com');
				Insert usr;
        User mangerusr = new User(Alias = 'sysm', Email='systemadminman@testorg.com', 
                          EmailEncodingKey='UTF-8', LastName='TestMan', LanguageLocaleKey='en_US', 
                          LocaleSidKey='en_US', ProfileId = p.Id, 
                          TimeZoneSidKey='America/Los_Angeles', UserName='Managertest@ss.com',ManagerId=usr.Id);
        
				Insert mangerusr ;
        User mangerusrsManager = new User(Alias = 'sysmm', Email='systemadminmanMan@testorg.com', 
                          EmailEncodingKey='UTF-8', LastName='TestManMan', LanguageLocaleKey='en_US', 
                          LocaleSidKey='en_US', ProfileId = p.Id, 
                          TimeZoneSidKey='America/Los_Angeles', UserName='ManagerManagertest@ss.com',ManagerId=mangerusr.Id);
        
				Insert mangerusrsManager ;
	   Opportunity opp=new Opportunity ();
		   Opp.Name='TestOpportunity';
		   opp.CloseDate=System.Today();
		   Opp.StageName='Prospecting';
		   opp.Territory_Manager__c=mangerusrsManager .Id;
		   Insert opp;
	   opp=[SELECT id,Name FROM Opportunity WHERE id=: opp.Id LIMIT 1];
	   System.assertEquals(opp.Name,'TestOpportunity');
	   Apexpages.currentpage().getparameters().put('ownerid',mangerusrsManager.Id);
	   ApexPages.StandardController con=new ApexPages.StandardController(opp);
		   Opportunitymanager opman=new Opportunitymanager(con);
		   opman.getuser();
	       opman.submit();
    }
}

For detail in test class please follow below links 

https://developer.salesforce.com/page/An_Introduction_to_Apex_Code_Test_Methods  

https://developer.salesforce.com/page/How_to_Write_Good_Unit_Tests

Thanks 
Manoj
Amit Chaudhary 8Amit Chaudhary 8
Please try below test class :-
 
@isTest()
public class OpportunitymanagerTest 
{
	public static testmethod void unitTest()
	{
		Profile p = [SELECT Id FROM Profile WHERE Name='System Administrator']; 
        User usr = new User(Alias = 'sys', Email='systemadmin@testorg.com', 
                          EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', 
                          LocaleSidKey='en_US', ProfileId = p.Id, 
                          TimeZoneSidKey='America/Los_Angeles', UserName='sysadmintest@ss.com');
		Insert usr;

        User mangerusr = new User(Alias = 'sysm', Email='systemadminman@testorg.com', 
                          EmailEncodingKey='UTF-8', LastName='TestMan', LanguageLocaleKey='en_US', 
                          LocaleSidKey='en_US', ProfileId = p.Id, 
                          TimeZoneSidKey='America/Los_Angeles', UserName='Managertest@ss.com',ManagerId=usr.Id);
		Insert mangerusr ;

		Account acct = new Account();
		   acct.Name = 'Salesforce.com';
		   acct.Industry = 'Technology';
		insert acct;
		
		Opportunity opp=new Opportunity ();
			Opp.AccountID =acct.id,
			Opp.Name='OpportunityTest';
			opp.CloseDate=System.Today();
			Opp.StageName='New';
			opp.Territory_Manager__c=mangerusr.Id;
		Insert opp;
	   
		Test.StartTest();
			System.runAs(usr)
			{
				   Apexpages.currentpage().getparameters().put('ownerid',usr.id);
				   ApexPages.StandardController con=new ApexPages.StandardController(opp);
				   Opportunitymanager extObj=new Opportunitymanager(con);
				   extObj.getuser();
				   extObj.submit();
			}
		Test.StartTest();
	}
}

Please mark this as solution by selecting it as best answer if this solves your problem, So that if anyone has this issue this post can help
Raja Bipin Chandra  M BRaja Bipin Chandra M B
Thanks folks, but i am getting an error in the submit  method as below. How should we pass the args here? Any suggestions please.
 
ConnectApi.ConnectApiException: Invalid recipient ID: null


Regards,
Raja
ManojjenaManojjena
HI Raja ,

Check the below link it may help ,

https://github.com/alouie-sfdc/ConnectApiHelper/blob/master/ConnectApiHelperTest.cls

Let me know any issue .