You need to sign in to do that
Don't have an account?

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();
}
}
@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();
}
}
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
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?
Did you mean to use myaccount?
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>
@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>
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.
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
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)...
... and THEN paste your code in the text box that pops up:
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!
One thing I see is this....
Inside your PartnerLeadController class, you have the following public variables with getters and setters: And you have no public methods.
In your test class you call... ...but those methods don't exist.
----
When you called that instantiated the class and should have called its constructor.
What are you trying to accomplish with... in your test class?
Try deleting... ...and see what happens.
@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();
}
}
@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();
}
}
As far as that one line of code, I do not know. Are you sure this SOQL call... ...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... ...that would also cause the for loop code to be skipped)
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.
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?