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
SoundarSoundar 

Test Class Coverage With Explanation

Dear friends,

I am new to the development , could you please cover the test class for following code with explaination.

Code
---------------------------------------------------------------------------------

public with sharing class AccountDealOpportunity {

    public AccountDealOpportunity(ApexPages.StandardController controller) {
        
        lstOpp = new List<Opportunity__c>();
        AccId = ApexPages.currentPage().getParameters().get('id');
        Account iAcc = [SELECT id, Recordtype.Name, Name FROM Account WHERE Id =: AccId];
        String Query = 'SELECT id, Name, Originator__c, Investor__c, Interested_Products__c, Asset_Class__c, Investor_Category__c,'+
                        +'Amount__c,Valid_Till__c,RecordType.Name FROM Opportunity__c where ';
        if(iAcc.RecordType.Name == 'Investor')
        {
            query = query + 'Investor__c =: AccId';
        }
        else if(iAcc.RecordType.Name == 'Originator')
        {
            query = query + 'Originator__c =: AccId';
        }
        lstOpp = Database.query(query);
    }


    public List<Opportunity__c> lstOpp {get;set;}
    Public Boolean isShowOpp {get;set;}
    String AccId;
    
    public AccountDealOpportunity()
    {
        
    }
    
    public pageReference CreateDeal()
    {
        PageReference DealPage = new PageReference('/apex/CreateDeals?id=' + AccId);
        DealPage.setRedirect(true);
        return DealPage;
    }
    
    public pageReference OpportunitytoDeal()
    {
        return Null;
    }

}

------------------------------------------------------------------------


Thanks In Advance ....

Regards,
Soundar Raj
Best Answer chosen by Soundar
Ankur Saini 9Ankur Saini 9
Hi Soundar Raj,

I hope it will help you.

Apex Class:
public with sharing class AccountDealOpportunity {

    public List<Opportunity> lstOpp {get;set;}
    Public Boolean isShowOpp {get;set;}
    public String AccId{get;set;}
    
    public AccountDealOpportunity(ApexPages.StandardController controller) {
        
        lstOpp = new List<Opportunity>();
        AccId = ApexPages.currentPage().getParameters().get('id');
        Account iAcc = [SELECT id, Recordtype.Name, Name FROM Account WHERE Id =: AccId];
        String Query = 'SELECT id, Name, Dealer__c , Prospect__c FROM Opportunity where ';
        if(iAcc.RecordType.Name == 'Dealer')
        {
            query = query + 'Dealer__c =: AccId';
        }
        else if(iAcc.RecordType.Name == 'Prospect')
        {
            query = query + 'Prospect__c =: AccId';
        }
        lstOpp = Database.query(query);
    }
    
    public pageReference CreateDeal()
    {
        PageReference DealPage = new PageReference('/apex/CreateDeals?id=' + AccId);
        DealPage.setRedirect(true);
        return DealPage;
    }
    
    public pageReference OpportunitytoDeal()
    {
        return Null;
    }

}
Test Class:
@isTest
public class AccountDealOpportunity_Test {
    static testMethod void test1(){
        
        String strRecordTypeId = [Select Id From RecordType Where SobjectType = 'Account' and Name = 'Dealer'].Id;
        String strRecordTypeId2 = [Select Id From RecordType Where SobjectType = 'Account' and Name = 'Prospect'].Id;
        
        Account acc = new Account();
        acc.Name='accname';
        acc.RecordTypeId=strRecordTypeId;
        insert acc;

        System.currentPageReference().getParameters().put('id', acc.id);

        ApexPages.StandardController sc = new ApexPages.StandardController(acc);
        AccountDealOpportunity ado = new AccountDealOpportunity(sc);
        	ado.isShowOpp=true;
        	ado.CreateDeal();
        	ado.OpportunitytoDeal();
        
        acc.RecordTypeId=strRecordTypeId2;
            update acc;
            AccountDealOpportunity ado2 = new AccountDealOpportunity(sc);       
    }
}

Thanks,
Ankur Saini
http://mirketa.com
 

All Answers

Ankur Saini 9Ankur Saini 9
Hi Soundar Raj,

I hope it will help you.

Apex Class:
public with sharing class AccountDealOpportunity {

    public List<Opportunity> lstOpp {get;set;}
    Public Boolean isShowOpp {get;set;}
    public String AccId{get;set;}
    
    public AccountDealOpportunity(ApexPages.StandardController controller) {
        
        lstOpp = new List<Opportunity>();
        AccId = ApexPages.currentPage().getParameters().get('id');
        Account iAcc = [SELECT id, Recordtype.Name, Name FROM Account WHERE Id =: AccId];
        String Query = 'SELECT id, Name, Dealer__c , Prospect__c FROM Opportunity where ';
        if(iAcc.RecordType.Name == 'Dealer')
        {
            query = query + 'Dealer__c =: AccId';
        }
        else if(iAcc.RecordType.Name == 'Prospect')
        {
            query = query + 'Prospect__c =: AccId';
        }
        lstOpp = Database.query(query);
    }
    
    public pageReference CreateDeal()
    {
        PageReference DealPage = new PageReference('/apex/CreateDeals?id=' + AccId);
        DealPage.setRedirect(true);
        return DealPage;
    }
    
    public pageReference OpportunitytoDeal()
    {
        return Null;
    }

}
Test Class:
@isTest
public class AccountDealOpportunity_Test {
    static testMethod void test1(){
        
        String strRecordTypeId = [Select Id From RecordType Where SobjectType = 'Account' and Name = 'Dealer'].Id;
        String strRecordTypeId2 = [Select Id From RecordType Where SobjectType = 'Account' and Name = 'Prospect'].Id;
        
        Account acc = new Account();
        acc.Name='accname';
        acc.RecordTypeId=strRecordTypeId;
        insert acc;

        System.currentPageReference().getParameters().put('id', acc.id);

        ApexPages.StandardController sc = new ApexPages.StandardController(acc);
        AccountDealOpportunity ado = new AccountDealOpportunity(sc);
        	ado.isShowOpp=true;
        	ado.CreateDeal();
        	ado.OpportunitytoDeal();
        
        acc.RecordTypeId=strRecordTypeId2;
            update acc;
            AccountDealOpportunity ado2 = new AccountDealOpportunity(sc);       
    }
}

Thanks,
Ankur Saini
http://mirketa.com
 
This was selected as the best answer
SoundarSoundar
Hi Ankur Saini, 

Really Thanks it's working fine. 

Could You please give me your mail id.


Regards,

Soundar Raj.
SoundarSoundar
Hi Ankur Saini,

Now It's Covering 94% as well. But onle line is not covering. Can you please advise why it's not covering. 



public with sharing class AccountDealOpportunity {

    public AccountDealOpportunity(ApexPages.StandardController controller) {
        
        lstOpp = new List<Opportunity__c>();
        AccId = ApexPages.currentPage().getParameters().get('id');
        Account iAcc = [SELECT id, Recordtype.Name, Name FROM Account WHERE Id =: AccId];
        String Query = 'SELECT id, Name, Originator__c, Investor__c, Interested_Products__c, Asset_Class__c, Investor_Category__c,'+
                        +'Amount__c,Valid_Till__c,RecordType.Name FROM Opportunity__c where ';
        if(iAcc.RecordType.Name == 'Investor')
        {
            query = query + 'Investor__c =: AccId'; 
        }
        else if(iAcc.RecordType.Name == 'Originator')
        {
            query = query + 'Originator__c =: AccId';
        }
        lstOpp = Database.query(query);
    }


    public List<Opportunity__c> lstOpp {get;set;}
    Public Boolean isShowOpp {get;set;} // This Line Is Not Covering....
    String AccId;
    
    public AccountDealOpportunity()
    {
        
    }
    
    public pageReference CreateDeal()
    {
        PageReference DealPage = new PageReference('/apex/CreateDeals?id=' + AccId);
        DealPage.setRedirect(true);
        return DealPage;
    }
    
    public pageReference OpportunitytoDeal()
    {
        return Null;
    }

}


Regards,

Soundar Raj.