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
Shruthi MN 19Shruthi MN 19 

Test class to display opp list

Can anyone provide Test class for following use Case.
Consider all positive negative and bulk test case

Use case ::
Collection of Closed Won Opportunities
1. I have created a custom quota object
2. Create a VF page to display all the Closed won opportunities in the org Only if
1)The opportunitys cloased date lies between start and end date of quota
2)The User which is dispalyed in the assed to user should be the same as the owner
3) The record type is BDM

I have written the below test class for the same. I do not know how to write the logic can anyone help me with the same

Apex class

public with sharing class listopponquota {
    
    public list<Opportunity> oppList{get;set;}
    
    public listopponquota()
    {
        String selectedVal ='';
        oppList = new list<Opportunity>();
        oppList();
    }
    
    public pageReference oppList() {
        Id qId = (Id) ApexPages.currentPage().getParameters().get('id');
        system.debug('Id-->'+qId);    
        List<quota__c> quotaList1 = new List<quota__c>();
        List<Opportunity> oppList1 = new List<Opportunity>();
        quotaList1 =[select Id, Name,Start_Date__c,End_Date__c,Assign_to_User__c, OwnerId from quota__c where Id   =: qId Limit 1];
        oppList1 = [Select Id, Name, CloseDate, StageName,OwnerId,Amount From Opportunity where StageName = 'Closed Won'];
        
        system.debug('quotaList1-->'+quotaList1);
        if(quotaList1.size() > 0){
            for(quota__c q : quotaList1){
                
                for(Opportunity opp : oppList1){
                    system.debug('closedDate-->'+opp.CloseDate);
                    if(opp.CloseDate  >=  q.Start_Date__c  && opp.CloseDate <= q.End_Date__c && q.Assign_to_User__c == q.OwnerId){
                        oppList.add(opp);
                    } 
                }                
            }
        }
        system.debug('oppList-->'+oppList);
        return null;
    }
}

Vf page

<apex:page controller="listopponquota" lightningStylesheets="true" >
    <apex:form > 
        <apex:pageBlock rendered="true" >
            <apex:pageBlockTable value="{!oppList}" var="v">
                <apex:column value="{!v.OwnerId}"/>
                
                <apex:column headerValue="Opportunity Name">
                    <apex:outputlink value="/{!v.Id}">{!v.Name}</apex:outputlink>
                </apex:column>
                <apex:column value="{!v.CloseDate}"/>
                <apex:column value="{!v.StageName}"/>
                <apex:column value="{!v.Amount}"/>
                            </apex:pageBlockTable>
            
            
        </apex:pageBlock>
    </apex:form> 
</apex:page>

Test class

@isTest
private class testclassquotaVfpage{
    static testMethod void listopponquota(){ 
        
        Quota__c op = new Quota__c ();
        op.Name = 'test_shruthi';
      
        op.Start_Date__c = Date.newInstance(2020,02,06);
        op.End_Date__c = Date.newInstance(2020,02,19);
        insert op;
        
        
        
    }
}
    Opportunity opp = new Opportunity(Name='test opp', StageName='stage', Probability = 95, CloseDate=system.today());

Opportunity opp = new Opportunity();
        opp.Name = 'test_shruthi';
        
        opp.type = 'New Customer';
        opp.CloseDate = system.today();
        opp.StageName ='Closed Won';
        // add all required fields here
        insert opp;
        



   Database.executeBatch(new ContactWithClosedWonOpportunities());

    Test.stopTest();
}

}



 
Shruthi MN 19Shruthi MN 19
User-added image
Devi ChandrikaDevi Chandrika (Salesforce Developers) 
Hi shruthi,
You are getting this because you are closing the class and method with braces before it ends.Try by removing braces in 14,15 line.

Try this code
@isTest
private class testclassquotaVfpage{
    static testMethod void listopponquota(){ 
        
        Quota__c op = new Quota__c ();
        op.Name = 'test_shruthi';
      
        op.Start_Date__c = Date.newInstance(2020,02,06);
        op.End_Date__c = Date.newInstance(2020,02,19);
        insert op;
        
        
    Opportunity opp = new Opportunity(Name='test opp', StageName='stage', Probability = 95, CloseDate=system.today());

Opportunity opp = new Opportunity();
        opp.Name = 'test_shruthi';
        
        opp.type = 'New Customer';
        opp.CloseDate = system.today();
        opp.StageName ='Closed Won';
        // add all required fields here
        insert opp;
        



   Database.executeBatch(new ContactWithClosedWonOpportunities());

    Test.stopTest();
}

}

Hope this helps you
Let me know if this helps you. Kindly mark it as solved so that it may help others in future.

Thanks and Regards
Shruthi MN 19Shruthi MN 19
I am getting below error

User-added image
Devi ChandrikaDevi Chandrika (Salesforce Developers) 
You have two variables as opp ,you cannot have two variables with same name . Change opp to some other name and for 2nd error check if batch class ContactWithClosedWonOpportunities is there in the org.
@isTest
private class testclassquotaVfpage{
    static testMethod void listopponquota(){ 
        
        Quota__c op = new Quota__c ();
        op.Name = 'test_shruthi';
      
        op.Start_Date__c = Date.newInstance(2020,02,06);
        op.End_Date__c = Date.newInstance(2020,02,19);
        insert op;
        
        list<Opportunity> oplist = new list<Opportunity>();
    Opportunity opp1 = new Opportunity(Name='test opp', StageName='stage', Probability = 95, CloseDate=system.today());
    oplist.add(opp1);

Opportunity opp = new Opportunity();
        opp.Name = 'test_shruthi';
        
        opp.type = 'New Customer';
        opp.CloseDate = system.today();
        opp.StageName ='Closed Won';
        // add all required fields here
        oplist.add(opp);
        insert oplist ;
        



   Database.executeBatch(new ContactWithClosedWonOpportunities());

    Test.stopTest();
}

}

Hope this helps you
Let me know if this helps you. Kindly mark it as solved so that it may help others in future.

Thanks and Regards
Shruthi MN 19Shruthi MN 19
User-added image