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 NarsiShruthi Narsi 

URGENT!!Can anyone help me with test class its failing

I have written a test class to add list of leads

@isTest
private class UpdateUnitPriceTest {

    static testMethod void myUnitTest() {

        Test.startTest();

       Account myaccount = new Account (name='Test');
        insert myaccount;
        
LeadList = [select id,Name,CreatedDate,Company,OwnerId,Owner.Name,LeadSource,recordType.Name,CRM_User__r.Name,CRM_User__r.Id,CRM_User__c, Status, Account__c,LastName, Account__r.id,Account__r.OwnerId
                    from Lead 
                    where    CRM_User__c !=null And Account__c=: acc.Id AND LeadSource='Partner' AND recordType.Name = 'Partner Lead Registration' ];
    }

        // Verify the expressions are the same
        System.assertEquals('Lead list',+LeadList);

        

        Test.stopTest();

    }
}

Apex class
@isTest
public class PartnerLeadControllerTestClass {
    
    static testMethod void leadscontroller()
    {        
        Account myaccount = new Account (name='Test');
        insert myaccount;
        
        PageReference pageRef = Page.PartnerleadList;
        pageRef.getparameters().put('recordId', myaccount.Id);  
        Test.setCurrentPage(pageRef);
        
        Apexpages.StandardController sc = new Apexpages.StandardController(myaccount);
        PartnerLeadController partlead = new PartnerLeadController(sc);   
        partlead.getlead();
        partlead.initlead();
    }
   
}
Best Answer chosen by Shruthi Narsi
MoonpieMoonpie
Shruthi,

Am I correct in assuming that since you have not replied as quickly as you had been, that it is working?

If so, please let us know it is - and please mark this question (and your duplicate) as answered.
If not, please reply with what issue(s) you are still having.

All Answers

MoonpieMoonpie
Hi Shruthi,

Please give more information about what exactly is failing, where it is failing, if it is just not successful (your System.assertEquals() is failing) or whether there are some other errors.

Have you checked the log files, and have you at any point inserted any System.debug() calls and checked the logs in order to make sure values at various places in the code are what you expect them to be before you get to the end?
Shruthi NarsiShruthi Narsi
User-added image
MoonpieMoonpie
I see one thing: In your SOQL statement you reference acc.Id, but acc is not defined anywhere.

Did you mean to use myaccount?
Shruthi NarsiShruthi Narsi
@isTest
public class PartnerLeadControllerTestClass {
    
    static testMethod void leadscontroller()
    {        
        Account myaccount = new Account (name='Test');
        insert myaccount;
        
        PageReference pageRef = Page.PartnerleadList;
        pageRef.getparameters().put('recordId', myaccount.Id);  
        Test.setCurrentPage(pageRef);
        
        Apexpages.StandardController sc = new Apexpages.StandardController(myaccount);
        PartnerLeadController partlead = new PartnerLeadController(sc);   
        partlead.getlead();
        partlead.initlead();
    }
   
}

Apex class

public  class PartnerLeadController {
    public Account acc{get;set;}
    public List<Lead> LeadList{get;set;}  
    Set<String> UserContactId = new Set<String>(); 
    public PartnerLeadController(ApexPages.StandardController stdController) {
        LeadList = new List<Lead>();
        this.acc = (Account)stdController.getRecord();
        list<contact> conlist=[Select Id From Contact WHERE AccountId =:acc.Id]; 
        for(User u: [Select Id, Name, ContactId FROM USER WHERE isactive=TRUE AND ContactId In:conlist]){
            UserContactId.add(u.Id);
        }
        LeadList = [select id,Name,CreatedDate,Company,OwnerId,Owner.Name,LeadSource,MAU__c,Estimated_Opportunity_Deal__c,recordType.Name,CRM_User__r.Name,CRM_User__r.Id,CRM_User__c, Status, Account__c,LastName, Account__r.id,Account__r.OwnerId
                    from Lead 
                    where    CRM_User__c !=null And Account__c=: acc.Id AND LeadSource='Partner' AND recordType.Name = 'Partner Lead Registration' ];
    }
}

VF Page

<apex:page standardController="Account" extensions="PartnerLeadController" lightningStylesheets="true" sidebar="false"  >

    <apex:form >
       

        <apex:pageBlock rendered="true" >
            <apex:outputPanel layout="block" styleClass="container">
            <apex:pageBlockTable value="{!LeadList}" var="ld" width="100" >
               
                          
            <apex:column >
                <apex:outputLink value="/{!ld.Owner}" target="_top">{!ld.Owner.Name}</apex:outputLink>
                <apex:facet name="header">Owner Name</apex:facet>
            </apex:column>/> 
                
                 <apex:column ><apex:outputField value="{!ld.Name}" />
                <apex:facet name="header">Lead Name</apex:facet>
            </apex:column>
            <apex:column ><apex:outputField value="{!ld.Status}" />
                <apex:facet name="header">Status</apex:facet>
            </apex:column> 
                <apex:column ><apex:outputField value="{!ld.Company}" />
                <apex:facet name="header">Company</apex:facet>
            </apex:column> 
                 <apex:column ><apex:outputField value="{!ld.MAU__c}" />
                <apex:facet name="header">MAU</apex:facet>
            </apex:column> 
                <apex:column ><apex:outputField value="{!ld.Estimated_Opportunity_Deal__c}" />
                <apex:facet name="header">Estimated Opportunity Deal</apex:facet>
            </apex:column> 
                
            <apex:column ><apex:outputField value="{!ld.CreatedDate}" />
                <apex:facet name="header">CreatedDate</apex:facet>
         
            </apex:column> 
            </apex:pageBlockTable>
                </apex:outputPanel>
        </apex:pageBlock>  
      
    </apex:form>
</apex:page>
MoonpieMoonpie
Can you please reply with all of that code again, but instead of pasting it in this text box where we write our replies, can you please first click the "< >" button in the reply header and then past the code in the code text box?  It will make it much easier to read, and it will give it line numbers.
 
Shruthi NarsiShruthi Narsi
I have written a test class to add list of leads and disply on account page the lead should be of record type Partner Lead Registration


@isTest
public class PartnerLeadControllerTestClass {
    
    static testMethod void leadscontroller()
    {        
        Account myaccount = new Account (name='Test');
        insert myaccount;
        
        PageReference pageRef = Page.PartnerleadList;
        pageRef.getparameters().put('recordId', myaccount.Id);  
        Test.setCurrentPage(pageRef);
        
        Apexpages.StandardController sc = new Apexpages.StandardController(myaccount);
        PartnerLeadController partlead = new PartnerLeadController(sc);   
        partlead.getlead();
        partlead.initlead();
    }
   
}

Apex class

public  class PartnerLeadController {
    public Account acc{get;set;}
    public List<Lead> LeadList{get;set;}  
    Set<String> UserContactId = new Set<String>(); 
    public PartnerLeadController(ApexPages.StandardController stdController) {
        LeadList = new List<Lead>();
        this.acc = (Account)stdController.getRecord();
        list<contact> conlist=[Select Id From Contact WHERE AccountId =:acc.Id]; 
        for(User u: [Select Id, Name, ContactId FROM USER WHERE isactive=TRUE AND ContactId In:conlist]){
            UserContactId.add(u.Id);
        }
        LeadList = [select id,Name,CreatedDate,Company,OwnerId,Owner.Name,LeadSource,MAU__c,Estimated_Opportunity_Deal__c,recordType.Name,CRM_User__r.Name,CRM_User__r.Id,CRM_User__c, Status, Account__c,LastName, Account__r.id,Account__r.OwnerId
                    from Lead 
                    where    CRM_User__c !=null And Account__c=: acc.Id AND LeadSource='Partner' AND recordType.Name = 'Partner Lead Registration' ];
    }
}

VF Page

<apex:page standardController="Account" extensions="PartnerLeadController" lightningStylesheets="true" sidebar="false"  >

    <apex:form >
       

        <apex:pageBlock rendered="true" >
            <apex:outputPanel layout="block" styleClass="container">
            <apex:pageBlockTable value="{!LeadList}" var="ld" width="100" >
               
                          
            <apex:column >
                <apex:outputLink value="/{!ld.Owner}" target="_top">{!ld.Owner.Name}</apex:outputLink>
                <apex:facet name="header">Owner Name</apex:facet>
            </apex:column>/> 
                
                 <apex:column ><apex:outputField value="{!ld.Name}" />
                <apex:facet name="header">Lead Name</apex:facet>
            </apex:column>
            <apex:column ><apex:outputField value="{!ld.Status}" />
                <apex:facet name="header">Status</apex:facet>
            </apex:column> 
                <apex:column ><apex:outputField value="{!ld.Company}" />
                <apex:facet name="header">Company</apex:facet>
            </apex:column> 
                 <apex:column ><apex:outputField value="{!ld.MAU__c}" />
                <apex:facet name="header">MAU</apex:facet>
            </apex:column> 
                <apex:column ><apex:outputField value="{!ld.Estimated_Opportunity_Deal__c}" />
                <apex:facet name="header">Estimated Opportunity Deal</apex:facet>
            </apex:column> 
                
            <apex:column ><apex:outputField value="{!ld.CreatedDate}" />
                <apex:facet name="header">CreatedDate</apex:facet>
         
            </apex:column> 
            </apex:pageBlockTable>
                </apex:outputPanel>
        </apex:pageBlock>  
      
    </apex:form>
</apex:page>
MoonpieMoonpie
That is still not in a code text box.

However, I still say check your UpdateUnitPriceTest class that you originally posted.  It seems to me that you copied and pasted your SOQL statement from another location (perhaps your PartnerLeadController class) and it references acc.Id when acc is not previously defined.
Shruthi NarsiShruthi Narsi
dont check unit price test class

I want you to correct the code because im new to testing and I notb aware of writing for loop in test class can you help me with this
MoonpieMoonpie
Shruthi,

Please slow down.

You posted code originally that included UpdateUnitPriceTest and asked for help without originally stating the issue.  I saw something that appeared incorrect in that code, but now you say not to look there.  And then you show one error, and then ask for help with a for loop.

Could you please isolate the section of code, or the full method or the full class or the classes that you only want help with, and then DO NOT paste that code in the text box where you type your reply to this post, but instead go to the top of your reply text box, click the < > button (highlighted below)...

User-added image

... and THEN paste your code in the text box that pops up:

User-added image

Then please ask a specific question or tell us what error you are getting.

People want to help, but it is much harder when code is hard to read and the person needing help is not being specific.

Thanks!
 
Shruthi NarsiShruthi Narsi
I have written a test class to add list of leads and disply on account page the lead should be of record type Partner Lead Registration


@isTest
public class PartnerLeadControllerTestClass {
    
    static testMethod void leadscontroller()
    {        
        Account myaccount = new Account (name='Test');
        insert myaccount;
        
        PageReference pageRef = Page.PartnerleadList;
        pageRef.getparameters().put('recordId', myaccount.Id);  
        Test.setCurrentPage(pageRef);
        
        Apexpages.StandardController sc = new Apexpages.StandardController(myaccount);
        PartnerLeadController partlead = new PartnerLeadController(sc);   
        partlead.getlead();
        partlead.initlead();
    }
   
}

Apex class

public  class PartnerLeadController {
    public Account acc{get;set;}
    public List<Lead> LeadList{get;set;}  
    Set<String> UserContactId = new Set<String>(); 
    public PartnerLeadController(ApexPages.StandardController stdController) {
        LeadList = new List<Lead>();
        this.acc = (Account)stdController.getRecord();
        list<contact> conlist=[Select Id From Contact WHERE AccountId =:acc.Id]; 
        for(User u: [Select Id, Name, ContactId FROM USER WHERE isactive=TRUE AND ContactId In:conlist]){
            UserContactId.add(u.Id);
        }
        LeadList = [select id,Name,CreatedDate,Company,OwnerId,Owner.Name,LeadSource,MAU__c,Estimated_Opportunity_Deal__c,recordType.Name,CRM_User__r.Name,CRM_User__r.Id,CRM_User__c, Status, Account__c,LastName, Account__r.id,Account__r.OwnerId
                    from Lead 
                    where    CRM_User__c !=null And Account__c=: acc.Id AND LeadSource='Partner' AND recordType.Name = 'Partner Lead Registration' ];
    }
}

VF Page

<apex:page standardController="Account" extensions="PartnerLeadController" lightningStylesheets="true" sidebar="false"  >

    <apex:form >
       

        <apex:pageBlock rendered="true" >
            <apex:outputPanel layout="block" styleClass="container">
            <apex:pageBlockTable value="{!LeadList}" var="ld" width="100" >
               
                          
            <apex:column >
                <apex:outputLink value="/{!ld.Owner}" target="_top">{!ld.Owner.Name}</apex:outputLink>
                <apex:facet name="header">Owner Name</apex:facet>
            </apex:column>/> 
                
                 <apex:column ><apex:outputField value="{!ld.Name}" />
                <apex:facet name="header">Lead Name</apex:facet>
            </apex:column>
            <apex:column ><apex:outputField value="{!ld.Status}" />
                <apex:facet name="header">Status</apex:facet>
            </apex:column> 
                <apex:column ><apex:outputField value="{!ld.Company}" />
                <apex:facet name="header">Company</apex:facet>
            </apex:column> 
                 <apex:column ><apex:outputField value="{!ld.MAU__c}" />
                <apex:facet name="header">MAU</apex:facet>
            </apex:column> 
                <apex:column ><apex:outputField value="{!ld.Estimated_Opportunity_Deal__c}" />
                <apex:facet name="header">Estimated Opportunity Deal</apex:facet>
            </apex:column> 
                
            <apex:column ><apex:outputField value="{!ld.CreatedDate}" />
                <apex:facet name="header">CreatedDate</apex:facet>
         
            </apex:column> 
            </apex:pageBlockTable>
                </apex:outputPanel>
        </apex:pageBlock>  
      
    </apex:form>
</apex:page>

 
Shruthi NarsiShruthi Narsi
@Eric can u help me now thank you for correcting me..I didnt know about this feature....
MoonpieMoonpie
Shruthi,

One thing I see is this....

Inside your PartnerLeadController class, you have the following public variables with getters and setters:
acc
LeadList
And you have no public methods.

In your test class you call...
partlead.getlead();
partlead.initlead();
...but those methods don't exist.

----
When you called
PartnerLeadController partlead = new PartnerLeadController(sc);
that instantiated the class and should have called its constructor.
 
Shruthi NarsiShruthi Narsi
ok now can u make the changes what is a constructor?
MoonpieMoonpie
The constructor is this part of your PartnerLeadController class:
public PartnerLeadController(ApexPages.StandardController stdController) {...}

What are you trying to accomplish with...
partlead.getlead();
partlead.initlead();
in your test class?
Shruthi NarsiShruthi Narsi
I have chaged the apex class this was my prev test class can u correct th code for me since I dont know where I am going wrong
MoonpieMoonpie
So you previously had those methods inside your PartnerLeadController class?

Try deleting...
partlead.getlead();
partlead.initlead();
...and see what happens.
Shruthi NarsiShruthi Narsi
it is passing but code covergae 0%

@isTest
public class PartnerLeadControllerTestClass {
    
    static testMethod void leadscontroller()
    {        
        Account myaccount = new Account (name='Test');
        insert myaccount;
        
        PageReference pageRef = Page.PartnerleadList;
        pageRef.getparameters().put('recordId', myaccount.Id);  
        Test.setCurrentPage(pageRef);
        
        //Apexpages.StandardController sc = new Apexpages.StandardController(myaccount);
        //PartnerLeadController partlead = new PartnerLeadController(sc);   
        //partlead.getlead();
        //partlead.initlead();
    }
   
}
MoonpieMoonpie
Why did you comment out the other two lines?
Shruthi NarsiShruthi Narsi
I removed it and its covered 90%. how to cover this line
User-added image
MoonpieMoonpie
You must actually call the various lines of code in your Apex class from your test class in order for there to be coverage.  You commented out the lines that would at least call the constructor of your test class.
 
Shruthi NarsiShruthi Narsi
Updated code

@isTest
public class PartnerLeadControllerTestClass {
    
    static testMethod void leadscontroller()
    {        
        Account myaccount = new Account (name='Test');
        insert myaccount;
        
        
        PageReference pageRef = Page.PartnerleadList;
        pageRef.getparameters().put('recordId', myaccount.Id);  
        Test.setCurrentPage(pageRef);
        
        
        Apexpages.StandardController sc = new Apexpages.StandardController(myaccount);
        PartnerLeadController partlead = new PartnerLeadController(sc);   
        
        //partlead.getlead();
        //partlead.initlead();
    }
   
}
MoonpieMoonpie
Do you now understand that when you call the...
PartnerLeadController partlead = new PartnerLeadController(sc);
...line, it called the constructor inside your Apex class, which covered all that code?


As far as that one line of code, I do not know.  Are you sure this SOQL call...
[Select Id, Name, ContactId FROM USER WHERE isactive=TRUE AND ContactId In:conlist]
...actually returned any results? 

If not, it would have skipped the code inside the for loop.


(Or for that matter, if this call returned zero results...
conlist=[Select Id From Contact WHERE AccountId =:acc.Id]
...that would also cause the for loop code to be skipped)

 
MoonpieMoonpie
Shruthi,

Am I correct in assuming that since you have not replied as quickly as you had been, that it is working?

If so, please let us know it is - and please mark this question (and your duplicate) as answered.
If not, please reply with what issue(s) you are still having.
This was selected as the best answer
Shruthi NarsiShruthi Narsi
Code is not getting deployed in production. Its tell 0% code coverage.Can you help me with writing the entire test class again with 100% code coveage with best practises being used

 
MoonpieMoonpie
How did it go back to 0%?  You had 90%!

And you never responded to my question three posts above where I asked if you are certain your Contact & User SOQL statements are actually returning results in your test class?