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
Allen2Allen2 

please anyone help me to write test class for this apex class

public class ProReqClass 
{
    public String checkPro(Opportunity[] old_oppValue,Opportunity[] new_oppValue)
    {
        String message='';
        List<OpportunityLineItem> oppProductList = new List<OpportunityLineItem>();
        Set<Id> priceBookEntryIds = new Set<Id>();
        Set<String> productName = new Set<String>();
        Set<String> stagNames = new Set<String>();
        stagNames.add('5. Prove');
        stagNames.add('6. Negotiate');
        stagNames.add('7. Contract');
        stagNames.add('8. Won');
        stagNames.add('4. Negotiate');
        stagNames.add('5. Closed Won');
        
        try
        {
            if(new_oppValue[0].RecordTypeId !=null)
            {
                 List<RecordType> rtypes = [Select Name, Id From RecordType where sObjectType='Opportunity' and isActive=true and Id=:new_oppValue[0].RecordTypeId];
                 if (rtypes.size()==1)
                 {    
                    if((rtypes[0].Name == 'AB Opportunity') && stagnames.contains(new_OppValue[0].StageName))
                    {   
                        oppProductList = [Select Id, PriceBookEntryId, OpportunityId, Source__c, Custom_Hardware_or_Software__c, Service__c, Input__c, Envelope_Finishing_System__c, Selective_Opener_Inbound_Only__c, OCR_Reading__c, Inline_Scale_or_Meter__c, Retrofit__c, Status__c from OpportunityLineItem where OpportunityId=:new_oppValue[0].Id];
                        if(oppProductList.size() > 0)
                        {                         
                            for (integer i=0; i<oppProductList.size(); i++)
                            {                         
                                if(oppProductList[i].Source__c ==null || oppProductList[i].Custom_Hardware_or_Software__c ==null || oppProductList[i].Service__c==null || oppProductList[i].Input__c==null || oppProductList[i].Envelope_Finishing_System__c == null || oppProductList[i].Selective_Opener_Inbound_Only__c ==null ||oppProductList[i].OCR_Reading__c == null || oppProductList[i].Inline_Scale_or_Meter__c ==null || oppProductList[i].Retrofit__c ==null || oppProductList[i].Status__c==null)
                                {                             
                                    priceBookEntryIds.add(oppProductList[i].PriceBookEntryId);
                                }
                            }
                            if(priceBookEntryIds.size()>0)
                            {  
                                List<PricebookEntry> productIdsList = new List<PricebookEntry>();
                                productIdsList = [select Name from PricebookEntry where id in :priceBookEntryIds];                      
                                if(productIdsList.size()>0)
                                {                             
                                    for (Integer j=0;j<productIdsList.size();j++)
                                    {
                                        message+=productIdsList[j].Name+',';                                     
                                    }
                                    return message;
                                }
                                else
                                {
                                    return message;
                                }                                                                               
                            }
                            else
                            {                                 
                                return message;
                            }
                        }
                        else
                        {                             
                            return message;
                        }
                    }
                    else
                    {                         
                        return message;
                    }
                 }
                 else
                 {                     
                    return message;
                 }            
            }
            else
            {                  
                return message;
            }
        }
        Catch(Exception e)  
        {
            System.debug('While Processing:'+e.getMessage());
            return message;
        }
    }
}
Raj VakatiRaj Vakati
try this
 
@isTest
public class ProReqClassTest{  

    public static testmethod void firstTest(){
       
	     Account a = new Account();
        a.Name = 'Test';
        a.Industry = 'Retail';
        
        insert a;
        
        Id oppRecId = Schema.SObjectType.Opportunity.getRecordTypeInfosByName().get('AB_Opportunity').getRecordTypeId();

		
        Opportunity o = new Opportunity();
        o.name = 'Test';
		o.RecordTypeId =oppRecId ;
        o.AccountId = a.Id;
        o.StageName = 'Closed Won';
        o.CloseDate = date.today();
        o.Type = 'New Customers';
        
        insert o;
		
		Id pricebookId = Test.getStandardPricebookId();

//Create your product
Product2 prod = new Product2(
     Name = 'Product X',
     ProductCode = 'Pro-X',
     isActive = true
);
insert prod;

//Create your pricebook entry
PricebookEntry pbEntry = new PricebookEntry(
     Pricebook2Id = pricebookId,
     Product2Id = prod.Id,
     UnitPrice = 100.00,
     IsActive = true
);
insert pbEntry;

//create your opportunity line item.  This assumes you already have an opportunity created, called opp
OpportunityLineItem oli = new OpportunityLineItem(
     OpportunityId = o.Id,
     Quantity = 5,
     PricebookEntryId = pbEntry.Id,
     TotalPrice = quantity * pbEntry.UnitPrice
);
insert oli;
ProReqClass p = new ProReqClass();

p.checkPro(new Opportunity[]{o.Id} , new Opportunity[]{o.Id});

	   
    }  

}

 
Allen2Allen2
Hi Raj,

I am getting this error Initial expression is of incorrect type, expected: Opportunity but was: Id on line 54
 
Raj VakatiRaj Vakati
try this
 
@isTest
public class ProReqClassTest{  

    public static testmethod void firstTest(){
       
	     Account a = new Account();
        a.Name = 'Test';
        a.Industry = 'Retail';
        
        insert a;
        
        Id oppRecId = Schema.SObjectType.Opportunity.getRecordTypeInfosByName().get('AB_Opportunity').getRecordTypeId();

		
        Opportunity o = new Opportunity();
        o.name = 'Test';
		o.RecordTypeId =oppRecId ;
        o.AccountId = a.Id;
        o.StageName = 'Closed Won';
        o.CloseDate = date.today();
        o.Type = 'New Customers';
        
        insert o;
		
		Id pricebookId = Test.getStandardPricebookId();

//Create your product
Product2 prod = new Product2(
     Name = 'Product X',
     ProductCode = 'Pro-X',
     isActive = true
);
insert prod;

//Create your pricebook entry
PricebookEntry pbEntry = new PricebookEntry(
     Pricebook2Id = pricebookId,
     Product2Id = prod.Id,
     UnitPrice = 100.00,
     IsActive = true
);
insert pbEntry;

//create your opportunity line item.  This assumes you already have an opportunity created, called opp
OpportunityLineItem oli = new OpportunityLineItem(
     OpportunityId = o.Id,
     Quantity = 5,
     PricebookEntryId = pbEntry.Id,
     TotalPrice = quantity * pbEntry.UnitPrice
);
insert oli;
ProReqClass p = new ProReqClass();

 

p.checkPro(new Opportunity[]{o} , new Opportunity[]{o});

	   
    }  

}

 
Allen2Allen2
thanks it is covering 38% of code now...

below part is not covering

f(new_oppValue[0].RecordTypeId !=null)
            {
                 List<RecordType> rtypes = [Select Name, Id From RecordType where sObjectType='Opportunity' and isActive=true and Id=:new_oppValue[0].RecordTypeId];
                 if (rtypes.size()==1)
                 {    
                    if((rtypes[0].Name == 'AB Opportunity') && stagnames.contains(new_OppValue[0].StageName))
                    {   
                        oppProductList = [Select Id, PriceBookEntryId, OpportunityId, Source__c, Custom_Hardware_or_Software__c, Service__c, Input__c, Envelope_Finishing_System__c, Selective_Opener_Inbound_Only__c, OCR_Reading__c, Inline_Scale_or_Meter__c, Retrofit__c, Status__c from OpportunityLineItem where OpportunityId=:new_oppValue[0].Id];
                        if(oppProductList.size() > 0)
                        {                         
                            for (integer i=0; i<oppProductList.size(); i++)
                            {                         
                                if(oppProductList[i].Source__c ==null || oppProductList[i].Custom_Hardware_or_Software__c ==null || oppProductList[i].Service__c==null || oppProductList[i].Input__c==null || oppProductList[i].Envelope_Finishing_System__c == null || oppProductList[i].Selective_Opener_Inbound_Only__c ==null ||oppProductList[i].OCR_Reading__c == null || oppProductList[i].Inline_Scale_or_Meter__c ==null || oppProductList[i].Retrofit__c ==null || oppProductList[i].Status__c==null)
                                {                             
                                    priceBookEntryIds.add(oppProductList[i].PriceBookEntryId);
                                }
                            }
                            if(priceBookEntryIds.size()>0)
                            {  
                                List<PricebookEntry> productIdsList = new List<PricebookEntry>();
                                productIdsList = [select Name from PricebookEntry where id in :priceBookEntryIds];                      
                                if(productIdsList.size()>0)
                                {                             
                                    for (Integer j=0;j<productIdsList.size();j++)
                                    {
                                        message+=productIdsList[j].Name+',';                                     
                                    }
                                    return message;
                                }
                                else
                                {
                                    return message;
                                }                                                                               
                            }
                            else
                            {                                 
                                return message;
                            }
                        }
                        else
                        {                             
                            return message;
                        }
                    }
                    else
                    {                         
                        return message;
                    }
                 }
                 else
                 {                     
                    return message;
                 }            
            }
            else
            {                  
                return message;​​​​​​​
Raj VakatiRaj Vakati
Can u verifiy the data setup for test class is meeting all conditions?
Allen2Allen2

okay... I will check it again then will let you know.

Thanks Raj