You need to sign in to do that
Don't have an account?
Jason Liu 7
Test Class for SOQL
Hi Team,
I'm new to the apex coding, so I'm wondering if anyone could help me with the test class associate with following SOQL queries.
public class HomePageController {
Public static RecordType Rectyp = [select id from recordType where DeveloperName = 'Relationship_Expansion_Leads'];
public List<Lead> getLeads(){
List<Lead> leads = [select id, name from lead where owner.id =: UserInfo.getUserId() and status = 'Open' and RecordTypeId =: Rectyp.id ];
return leads;
}
}
Thanks,
Jason Liu
I'm new to the apex coding, so I'm wondering if anyone could help me with the test class associate with following SOQL queries.
public class HomePageController {
Public static RecordType Rectyp = [select id from recordType where DeveloperName = 'Relationship_Expansion_Leads'];
public List<Lead> getLeads(){
List<Lead> leads = [select id, name from lead where owner.id =: UserInfo.getUserId() and status = 'Open' and RecordTypeId =: Rectyp.id ];
return leads;
}
}
Thanks,
Jason Liu
NOTE: This code has not been tested and may contain typographical or logical errors
NOTE: When adding code please use the "Add a code sample" button (icon <>) to increase readability and make it easier to reference.
This will not use a hardcoded record type id, and it sets the OwnerId to what the query is expecting.
All Answers
[1] http://www.sfdc99.com/2013/05/14/how-to-write-a-test-class/
[2] http://pcon.github.io/presentations/testing/
[3] http://blog.deadlypenguin.com/blog/2014/07/23/intro-to-apex-auto-converting-leads-in-a-trigger/
[4] http://blog.deadlypenguin.com/blog/testing/strategies/
@isTest
private class HomePageControllerTest {
static testMethod void myUnitTest() {
Profile p = [SELECT Id FROM Profile where Name = 'System Administrator'];
User u = new User(Alias = 'standt', Email='standarduser@testorg.com', EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', LocaleSidKey='en_US', ProfileId = p.Id,TimeZoneSidKey='America/Los_Angeles', UserName='1512434@dfe1.COM');
System.runAs(u){
//Create Data for Customer Objet
Lead leads = new Lead();
leads.LastName = 'TEST Name';
leads.status = 'Qualified';
leads.Phone = '5165039576';
Leads.Email = '1@1.com';
leads.Classification__c = 'Consumer';
leads.RecordTypeId = '012q0000000CkoQAAS';
leads.Bank_Relationship_Manager__c = UserInfo.getUserId();
insert leads;
HomePageController controller = new HomePageController();
//Now, our trigger will fire on After update event so update the Records
Test.startTest();//Starts the scope of test
leads.status = 'Open';
update leads;
controller.getLeads() ;
Test.stopTest();//Ends the scope of test
//Now check if it is giving desired results using system.assert Statement.New invoice should be created
}
}
}
NOTE: This code has not been tested and may contain typographical or logical errors
NOTE: When adding code please use the "Add a code sample" button (icon <>) to increase readability and make it easier to reference.
This will not use a hardcoded record type id, and it sets the OwnerId to what the query is expecting.