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

Help in Wring Test Class?
Can any onecan help me with test class
public with sharing class OpportunityQuickCreateExt
{
public OpportunityQuickCreateExt( QuickCreateController quickCreateCont )
{
User currentUser = [ SELECT Default_Continent__c, Default_Market__c, Default_Region__c
FROM User WHERE Id = :UserInfo.getUserId() ];
quickCreateCont.quickCreateObject.put( 'Name', 'Dummy Name' );
quickCreateCont.quickCreateObject.put( 'StageName', 'E Qualify / Suspect-No RFP' );
quickCreateCont.quickCreateObject.put( 'Continent__c', currentUser.Default_Continent__c );
if( !String.isEmpty( currentUser.Default_Region__c ) )
{
List<Region__c> defaultRegion = [SELECT Id FROM Region__c WHERE Name = :currentUser.Default_Region__c LIMIT 1 ];
quickCreateCont.quickCreateObject.put( 'Region__c', defaultRegion.isEmpty() ? NULL : defaultRegion[0].Id );
}
if( !String.isEmpty( currentUser.Default_Market__c ) )
{
List<Market__c> defaultMarket = [SELECT Id FROM Market__c WHERE Name = :currentUser.Default_Market__c LIMIT 1 ];
quickCreateCont.quickCreateObject.put( 'Market__c', defaultMarket.isEmpty() ? NULL : defaultMarket[0].Id );
}
}
}
public with sharing class OpportunityQuickCreateExt
{
public OpportunityQuickCreateExt( QuickCreateController quickCreateCont )
{
User currentUser = [ SELECT Default_Continent__c, Default_Market__c, Default_Region__c
FROM User WHERE Id = :UserInfo.getUserId() ];
quickCreateCont.quickCreateObject.put( 'Name', 'Dummy Name' );
quickCreateCont.quickCreateObject.put( 'StageName', 'E Qualify / Suspect-No RFP' );
quickCreateCont.quickCreateObject.put( 'Continent__c', currentUser.Default_Continent__c );
if( !String.isEmpty( currentUser.Default_Region__c ) )
{
List<Region__c> defaultRegion = [SELECT Id FROM Region__c WHERE Name = :currentUser.Default_Region__c LIMIT 1 ];
quickCreateCont.quickCreateObject.put( 'Region__c', defaultRegion.isEmpty() ? NULL : defaultRegion[0].Id );
}
if( !String.isEmpty( currentUser.Default_Market__c ) )
{
List<Market__c> defaultMarket = [SELECT Id FROM Market__c WHERE Name = :currentUser.Default_Market__c LIMIT 1 ];
quickCreateCont.quickCreateObject.put( 'Market__c', defaultMarket.isEmpty() ? NULL : defaultMarket[0].Id );
}
}
}
Try this and let me know if it helps you.
Note: Might be some update needed according to your class.
Thank you for quick response.
I added one field in Market having look up with Region.
and I am getting error while saving.
Error:Line 48: Abstract classes cannot be constructed: QuickCreateController
Here is my Updated code
@istest
private class TestClass {
@testSetup static void setup() {
Id profileId = [SELECT Id FROM Profile WHERE Name='Standard User'].Id; //Update here filter criteria according to your req.
List<String> ContinentList = new List<String>{'Asia', 'Africa', 'Antarctica', 'Europe', 'Australia'};
List<String> RegionList = new List<String>{'East', 'West', 'North', 'South', 'North-East'};
List<String> MarketList = new List<String>{'Market-1', 'Market-2', 'Market-3', 'Market-4', 'Market-5'};
List<User> userList = new List<User>();
List<Region__c> lstRegion = new List<Region__c>();
List<Market__c> lstMarket = new List<Market__c>();
for(Integer index = 0; index < 5; index++){
Region__c objReg = new Region__c(
Name = RegionList[index]
);
Market__c objMar = new Market__c(
Name = MarketList[index],
Region__c = RegionList[index]
); //Market having look up with region
lstRegion.add(objReg);
lstMarket.add(objMar);
}
for(Integer index = 0; index < 5; index++) {
userList.add(createTestUser(profileId, 'Test-' + index, 'User', ContinentList[index], MarketList[index], RegionList[index]));
}
//insert all test data.
try{
insert userList;
insert lstRegion;
insert lstMarket;
}
catch(Exception ex) {
system.debug('---Exception---' + ex);
}
}
@istest static void testClassMethod() {
Test.startTest();
List<User> lstUsers = [Select Id, Name From User];
system.runAs(lstUsers[0]){
QuickCreateController quickCreateCont = new QuickCreateController(); // error in this line
OpportunityQuickCreateExt objClass = new OpportunityQuickCreateExt(quickCreateCont);
}
Test.stopTest();
}
private static User createTestUser(Id profID, String fName, String lName, String Continent, String Market, String Region) {
String orgId = userInfo.getOrganizationId();
String dateString = String.valueof(Datetime.now()).replace(' ','').replace(':','').replace('-','');
Integer randomInt = Integer.valueOf(math.rint(math.random()*1000000));
String uniqueName = orgId + dateString + randomInt;
User tuser = new User( firstname = fName,
lastName = lName,
email = uniqueName + '@test' + orgId + '.org',
Username = uniqueName + '@test' + orgId + '.org',
EmailEncodingKey = 'ISO-8859-1',
Alias = uniqueName.substring(18, 23),
TimeZoneSidKey = 'America/Los_Angeles',
LocaleSidKey = 'en_US',
LanguageLocaleKey = 'en_US',
ProfileId = profId,
Default_Continent__c = '',
Default_Market__c = '',
Default_Region__c = ''
);
return tuser;
}
}
I updated the code and commented on the line where error was comming. Plz follow that comment.
Note: Check whether this class "QuickCreateController" is a Abstract class or not, If it is then create instance of any extended class and pass as param to below line. Like:
<Your Extended Class Name> absInst = new <Your Extended Class Name>();
OpportunityQuickCreateExt objClass = new OpportunityQuickCreateExt(absInst); // Then pass here "absInst" as parameter.
Hope it will help you.
Now my Class saved but test failed it is giving this error
System.QueryException: List has no rows for assignment to SObject
Updated Code:
@istest
private class OpportunityQuickCreateExtTest {
@testSetup static void setup() {
Id profileId = [SELECT Id FROM Profile WHERE Name='Standard User'].Id; //Update here filter criteria according to your req.
List<String> ContinentList = new List<String>{'Asia', 'Africa', 'Antarctica', 'Europe', 'Australia'};
List<String> RegionList = new List<String>{'East', 'West', 'North', 'South', 'North-East'};
List<String> MarketList = new List<String>{'Market-1', 'Market-2', 'Market-3', 'Market-4', 'Market-5'};
List<User> userList = new List<User>();
List<Region__c> lstRegion = new List<Region__c>();
List<Market__c> lstMarket = new List<Market__c>();
for(Integer index = 0; index < 5; index++){
Region__c objReg = new Region__c(
Name = RegionList[index]
);
lstRegion.add(objReg);
}
insert lstRegion;
for(Integer index = 0; index < 5; index++){
Market__c objMar = new Market__c(
Name = MarketList[index],
Region__c = lstRegion[index].Id
); //Market having look up with region
lstMarket.add(objMar);
}
for(Integer index = 0; index < 5; index++) {
userList.add(createTestUser(profileId, 'Test-' + index, 'User', ContinentList[index], MarketList[index], RegionList[index]));
}
//insert all test data.
try{
insert userList;
insert lstMarket;
}
catch(Exception ex) {
system.debug('---Exception---' + ex);
}
}
@istest static void testClassMethod() {
Test.startTest();
List<User> lstUsers = [Select Id, Name From User];
system.runAs(lstUsers[0]){
QuickCreateController quickCreateCont = new OpportunityQuickCreateController();
OpportunityQuickCreateExt objClass = new OpportunityQuickCreateExt(quickCreateCont);
}
Test.stopTest();
}
private static User createTestUser(Id profID, String fName, String lName, String Continent, String Market, String Region) {
String orgId = userInfo.getOrganizationId();
String dateString = String.valueof(Datetime.now()).replace(' ','').replace(':','').replace('-','');
Integer randomInt = Integer.valueOf(math.rint(math.random()*1000000));
String uniqueName = orgId + dateString + randomInt;
User tuser = new User( firstname = fName,
lastName = lName,
email = uniqueName + '@test' + orgId + '.org',
Username = uniqueName + '@test' + orgId + '.org',
EmailEncodingKey = 'ISO-8859-1',
Alias = uniqueName.substring(18, 23),
TimeZoneSidKey = 'America/Los_Angeles',
LocaleSidKey = 'en_US',
LanguageLocaleKey = 'en_US',
ProfileId = profId,
Default_Continent__c = '',
Default_Market__c = '',
Default_Region__c = ''
);
return tuser;
}
}