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
Surya KilledarSurya Killedar 

I need test class for it someone please help me out..

*Controller Class

public class ConOpptyRelatedToAccount 
{
     public ConOpptyRelatedToAccount(ApexPages.StandardController controller)
        {
        }
     public List<Contact> getContacts() 
    {
        
        List<Contact> con = [SELECT Id, FirstName, LastName, Title, Email FROM Contact WHERE AccountId=:apexpages.currentpage().getparameters().get('id')];
        return con;
    }
    
    public List<Opportunity> getOpportunities() 
    {
        
        List<Opportunity> Oppty = [SELECT Id, CloseDate, Amount, StageName, Name FROM Opportunity WHERE AccountId=:apexpages.currentpage().getparameters().get('id')];
        return Oppty;
    }
    
    public pagereference downLoad(){
        pagereference p = new pagereference('/apex/PDFofRelatedList');
        return p;
    }
  
}

******************************************************************************
*Vf page
<apex:page standardController="Account"  extensions="ConOpptyRelatedToAccount"  >
    <apex:form >
        
        <apex:pageBlock title="Contacts List" >
            
            <apex:pageBlockTable value="{!contacts }" var="Con">
                <apex:column value="{!Con.FirstName }"/>
                <apex:column value="{!Con.LastName }"/>
                <apex:column value="{!Con.Title }"/>
                <apex:column value="{!Con.Email }"/>
            </apex:pageBlockTable>
        </apex:pageBlock>
        
        <apex:pageBlock title="Opportunity List" >
            <apex:pageBlockTable value="{!opportunities }" var="op">
                <apex:column value="{!op.Name }"/>
                <apex:column value="{!op.CloseDate }"/>
                <apex:column value="{!op.StageName }"/>
                <apex:column value="{!op.Amount }"/>
            </apex:pageBlockTable>
            
            <apex:pageBlockButtons location="Bottom">
                <apex:commandButton value="Download Pdf" action="{!downLoad}" />
            </apex:pageBlockButtons>
            
        </apex:pageBlock>
    </apex:form>
</apex:page>
Best Answer chosen by Surya Killedar
Surya KilledarSurya Killedar
Hi Mukesh,
I have made some minor correction in your code so now its gives a 100% code coverage.

@isTest
public class TestConOpptyRelatedToAccount {
    @isTest
        static void testMethod1() 
        {
            Account acc = new Account();
            acc.Name='Test Account' ;
            insert acc;
            
            Contact con = new Contact();
            con.FirstName='Test1';
            con.LastName='Test2';
            con.Accountid= acc.id;
            con.Email= 'test@gmail.com';
                insert con;
            
            Opportunity oppt = new Opportunity();
            oppt.Name ='New mAWS Deal';
            oppt.AccountID = acc.ID;
            oppt.StageName = 'Closed Won';
            oppt.Amount = 3000;
            oppt.CloseDate = System.today();
            
            insert oppt;
            
            Test.StartTest(); 
            ApexPages.StandardController sc = new ApexPages.StandardController(acc);
            ConOpptyRelatedToAccount testAccPlan = new ConOpptyRelatedToAccount (sc);
            
            PageReference pageRef = Page.ReletedAccountOfConOppty; 
            pageRef.getParameters().put('id', String.valueOf(acc.Id));
            Test.setCurrentPage(pageRef);

            testAccPlan.downLoad();
            testAccPlan.getContacts();
            testAccPlan.getOpportunities();
            
            Test.StopTest();
        }
    }


Thanks & Regards
Surya.

All Answers

Suraj Tripathi 47Suraj Tripathi 47
Hi Surya 

Try this code:
 
@isTest
public class ConOpptyRelatedToAccount_Test {
    @isTest
    public static void getContactTest(){
        Contact cObj = new Contact();
        cObj.LastName = 'Test';
        Insert cObj;
        Test.StartTest(); 
        PageReference pageRef = Page.PDFofRelatedList;
        Test.setCurrentPage(pageRef);
        ApexPages.Standardcontroller sc = new ApexPages.Standardcontroller(cObj);
        ApexPages.currentPage().getParameters().put('Id',cObj.id);
        ConOpptyRelatedToAccount classObj = new ConOpptyRelatedToAccount(sc);
		List<Contact> obj = classObj.getContacts();
        Test.stopTest();
    }
    @isTest
    public static void getOpportunityTest(){
        Opportunity opp = new Opportunity();
        opp.StageName = 'Qualify';
        opp.CloseDate = Date.today().addDays(5);
        opp.Name = 'OPp';
        Insert opp;
        Test.StartTest(); 
        PageReference pageRef = Page.PDFofRelatedList;
        Test.setCurrentPage(pageRef);
        ApexPages.Standardcontroller sc = new ApexPages.Standardcontroller(opp);
        ApexPages.currentPage().getParameters().put('Id',opp.id);
        ConOpptyRelatedToAccount classObj = new ConOpptyRelatedToAccount(sc);
		List<Opportunity> oppList = classObj.getOpportunities();
        classObj.downLoad();
        Test.stopTest();
    }
}
Code Coverage : 100%

I hope you find the above solution helpful. If it does, please mark it as Best Answer to help others too

Thanks and Regards,
Suraj Tripathi
AbhinavAbhinav (Salesforce Developers) 
Check this:

https://salesforce.stackexchange.com/questions/244788/how-do-i-write-an-apex-unit-test

https://salesforce.stackexchange.com/questions/244794/how-do-i-increase-my-code-coverage-or-why-cant-i-cover-these-lines

Thanks!
mukesh guptamukesh gupta
Hi Surya,

Please use below code:-
 
@isTest 
public class ExtensionTestClass 
{
 static testMethod void testMethod1() 
 {
 Account acc = new Account();
 acc.Name='Test Account' ;
 insert acc;
 
 Contact con = new Contact();
		con.FirstName='Test1';
		con.LastName='Test2';
		con.Accountid= acc.id;
		con.Email= 'test@gmail.com'
		insert con;
		
		// Creates first opportunity
    Opportunity oppt = new Opportunity(Name ='New mAWS Deal',
                            AccountID = acc.ID,
                            StageName = 'Closed Won',
                            Amount = 3000,
                            CloseDate = System.today()
                            );

   insert oppt;
 

  Test.StartTest(); 
  ApexPages.StandardController sc = new ApexPages.StandardController(acc);
  ConOpptyRelatedToAccount testAccPlan = new ConOpptyRelatedToAccount (sc);

  PageReference pageRef = Page.xyz; // Add your VF page Name here
  pageRef.getParameters().put('id', String.valueOf(acc.Id));
  Test.setCurrentPage(pageRef);

  testAccPlan.getContacts();
  
  testAccPlan.getOpportunities();
  
 Test.StopTest();
 }
}

if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh
Surya KilledarSurya Killedar
Hi Mukesh.
Your code is showing 70% code coverage...Thanks for helping :)
Surya KilledarSurya Killedar
Hi Suraj,
Your ans is perfect..Its give 100% Code coverage Thanks !!
Surya KilledarSurya Killedar
Hi Mukesh,
I have made some minor correction in your code so now its gives a 100% code coverage.

@isTest
public class TestConOpptyRelatedToAccount {
    @isTest
        static void testMethod1() 
        {
            Account acc = new Account();
            acc.Name='Test Account' ;
            insert acc;
            
            Contact con = new Contact();
            con.FirstName='Test1';
            con.LastName='Test2';
            con.Accountid= acc.id;
            con.Email= 'test@gmail.com';
                insert con;
            
            Opportunity oppt = new Opportunity();
            oppt.Name ='New mAWS Deal';
            oppt.AccountID = acc.ID;
            oppt.StageName = 'Closed Won';
            oppt.Amount = 3000;
            oppt.CloseDate = System.today();
            
            insert oppt;
            
            Test.StartTest(); 
            ApexPages.StandardController sc = new ApexPages.StandardController(acc);
            ConOpptyRelatedToAccount testAccPlan = new ConOpptyRelatedToAccount (sc);
            
            PageReference pageRef = Page.ReletedAccountOfConOppty; 
            pageRef.getParameters().put('id', String.valueOf(acc.Id));
            Test.setCurrentPage(pageRef);

            testAccPlan.downLoad();
            testAccPlan.getContacts();
            testAccPlan.getOpportunities();
            
            Test.StopTest();
        }
    }


Thanks & Regards
Surya.
This was selected as the best answer
Suraj Tripathi 47Suraj Tripathi 47
Hi Surya,
Thanks for your compliment. Kindly mark my solution as the best answer if it helps you.