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
Raju mancheRaju manche 

Test class for sosl

Public with sharing  class SampleDuplicateCustomerPO_Check
{
    public void DuplicateCustomerPO_Check_method(case newcaseList)
    {
        Schema.DescribeSObjectResult d = Schema.SObjectType.Case; 
        Map<String,Schema.RecordTypeInfo> rtMapByName = d.getRecordTypeInfosByName();
        Schema.RecordTypeInfo rtByName =  rtMapByName.get('CIR');
        Id LegalRecId = rtByName.getRecordTypeId();   
        
        Case c = newcaseList;
        List<Case> CaseList2 = new List<Case>();
        List<List<case>> CaseListRam = new List<List<case>>();
        Boolean DupeFoundAllow = false; 
        
        String rtId = Schema.SObjectType.Case.getRecordTypeInfosByName().get('MCS - CS').getRecordTypeId();
        String Casenum = '';
        if(c.GE_ES_PO__c != '' && c.GE_ES_PO__c != null &&c.RecordTypeId == rtId ){    
            CaseListRam = [Find :c.GE_ES_PO__c Returning Case(CaseNumber where GE_ES_PO__c = :c.GE_ES_PO__c AND id != :c.id)];
            DupeFoundAllow = c.GE_MCS_Duplicate_PO_ok__c;
        } 
        for(List<case> cas : CaseListRam){
            CaseList2.addall(cas);
        }
        for(case ca : CaseList2){
            if(Casenum == '')
            Casenum = ca.CaseNumber;
            else
            Casenum = Casenum+','+ca.CaseNumber;
        }
        if(CaseList2.size() > 0){
            if(DupeFoundAllow){
                List<Case> CaseList3 = new List<Case>();
                
            }else{
                c.addError('There is already a Case with same Customer PO# and the CaseNumbers are: '+CaseNum+'. Please check "Duplicate PO OK?" if this is not a duplicate order!');        
            }
        }
    
    }
............................
This is my class.I am trying to write a test class for this. I tried so many ways but still i am getting 75% code coverage. For deployment this coverage is not a problem. But as per my organization I need to increase to 85%.
Please help me on this. I am posting my test class as well.


......................
@isTest
public class Test_Sample123{
/* usefull code commented
static testMethod void ParentCaseAutoUnfollow123()
    {
    Account acc=new Account();
    acc.name='test';
    insert acc;
    String rt= Schema.SObjectType.Case.getRecordTypeInfosByName().get('MCS - CS').getRecordTypeId();           
    Case cs=new Case();
        cs.GE_ES_PO__c='123456';
        cs.GE_MCS_Duplicate_PO_ok__c=true;
        cs.recordTypeid = rt;  
        cs.status='closed'; 
        insert cs;
        Case cs6=new Case();
        cs6.GE_ES_PO__c='123456';
        cs6.GE_MCS_Duplicate_PO_ok__c=true;
        cs6.recordTypeid = rt;  
        cs6.status='new';
        insert cs6;              
        List<case>csl=new List<case>();     
        csl.add(cs);
        csl.add(cs6);
        Map <id,case>oldMap = new Map<id,case>();
        oldmap.put(cs.id, cs);
        Map<Id,Case>oldMapCase1=(Map<Id,Case>)oldMap;
        oldMapCase1.put(cs.id, cs6);       
        cs.status='closed';
        update cs;       
        GE_OG_trg_Case_ParentCaseAutoUnfollow  cw1=new GE_OG_trg_Case_ParentCaseAutoUnfollow();
        cw1.mcsTaskUpdate(csl,oldMapCase1);        
        list<task>tslit=new List<task>();
        task t=new task();
        t.Status='In Progress';
        t.subject='test';
        t.whatid=cs.id;
        tslit.add(t);
        insert tslit;   
        GEESUtil.caseRecTypes=null;      
        GE_OG_trg_Case_ParentCaseAutoUnfollow  cw=new GE_OG_trg_Case_ParentCaseAutoUnfollow();
        cw.mcsTaskInsert(csl);        
    }      
    */      
    static testMethod void duplicatePO()
    {
    Account acc=new Account();
    acc.name='test';
    insert acc;
    
        String rt= Schema.SObjectType.Case.getRecordTypeInfosByName().get('MCS - CS').getRecordTypeId();        
        //opportunity opp=[select name from opportunity where recordtype.name='MCS' limit 1];
        List<Case> CaseList = new List<Case>();
        Case cs=new Case();
        cs.GE_ES_PO__c='123456';
        cs.GE_MCS_Duplicate_PO_ok__c=false;
        cs.recordTypeid = rt;  
        cs.status='new'; 
        caselist.add(cs);        
        //insert cs;
        cs.GE_ES_PO__c='';
        Case cs1=new Case();
        cs1.GE_ES_PO__c='1234567';
        cs1.GE_MCS_Duplicate_PO_ok__c=true;
        cs1.recordTypeid = rt;  
        cs1.status='new';
        caselist.add(cs1); 
        insert caselist;
        cs.GE_ES_PO__c='1234567';
         try
        {
            update cs;
        }
        catch(DMLException e)
        {
            
            //System.Assert(e.getMessage().contains('Recommendation should be Compliance Accepted to generate DOA'));
            System.Assert(e.getMessage().contains('There is already a Case with same Customer PO# and the CaseNumbers are'));
            
        }       
        
        /*
        List<case>caselist23=new List<case>();
        for(case cas : caselist){
            caselist23.add(cas);
        }
            caselist23.add(cs6);     
            */      
            SampleDuplicateCustomerPO_Check dup=new SampleDuplicateCustomerPO_Check();           
            String casenum='';
            //dup.DuplicateCustomerPO_Check_method(cs1);
            dup.DuplicateCustomerPO_Check_method(cs);             
    }   
    }
bob_buzzardbob_buzzard
When testing SOSL you have to prime the results that will be returned by the query, as the indexing happens after the records are committed to the database which never happens in a test context:

https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_testing_SOSL.htm