You need to sign in to do that
Don't have an account?
Surya 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>
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>
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
Try this code:
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
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!
Please use below code:-
if you need any assistanse, Please let me know!!
Kindly mark my solution as the best answer if it helps you.
Thanks
Mukesh
Your code is showing 70% code coverage...Thanks for helping :)
Your ans is perfect..Its give 100% Code coverage Thanks !!
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.
Thanks for your compliment. Kindly mark my solution as the best answer if it helps you.