Discussions
- General Development
- Schema Development
- Apex Code Development
- Visualforce Development
- Formulas & Validation Rules
- Security
- Mobile
- Force.com Sites & Site.com
- Chatter Development
- Java Development
- .NET Development
- Perl, PHP, Python & Ruby
- Desktop Integration
- APIs and Integrations
- Visual Workflow
- Apple, Mac and OS X
- VB and Office Development
- AppExchange Directory & Packaging
- Salesforce Labs & Open Source Projects
- Other Salesforce Applications
- Jobs Board
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic to the Top
- Bookmark
- Subscribe
- Printer Friendly Page
Test Clas
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
11-15-2012 01:35 AM
Hi , can any one provide me a test class for the below apex class
public with sharing class DSPCustomActions {
private final sObject record = null;
public DSPCustomActions (ApexPages.StandardController controller) {
this.record = controller.getRecord();
}
public DSPCustomActions (ApexPages.StandardSetController controller) {
}
public PageReference redirectToDSP() {
if (this.record instanceof DSP_Product__c) {
ID dspID = ((DSP_Product__c)this.record).Sales_Plan__c;
if (dspID==null) // handle NEW
return null;
else { // handle EDIT
PageReference ref = new PageReference('/'+dspId);
ref.setRedirect(false);
return ref;
}
}
else
return null;
}
public boolean deleteDSPSuccess {get; set;}
public PageReference deleteDSP() {
if (this.record instanceof Distributor_Sales_Plan__c) {
Distributor_Sales_Plan__c dsp = (Distributor_Sales_Plan__c)this.record;
if (dsp.Id!=null) {
ID accountID = dsp.Account__c;
String profileName = [select Name from Profile where Id = :UserInfo.getProfileId() limit 1].Name;
if(profileName == 'System Administrator' || profileName == 'Sales User'
|| [Select count() From AccountShare Where UserOrGroupId = :UserInfo.getUserId() And AccountId = :accountID And AccountAccessLevel IN ('All', 'Edit')] > 0){
List<DSP_Product__c> records = [select Id from DSP_Product__c where Sales_Plan__c=:dsp.Id limit 1];
if (records.size()>0)
return null;
boolean ok = false;
try {
delete dsp;
ok = true;
} catch(Exception e) {
ok = false;
}
if (ok) {
PageReference ref = new PageReference('/'+accountID);
ref.setRedirect(false);
return ref;
} else
return null;
} else{
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'Insuff
return null;
}
} else
return null;
} else
return null;
}
public PageReference createNewDSP() {
return createDSP(date.today().year());
}
public PageReference createNewNextDSP() {
return createDSP(date.today().year()+1);
}
//private class DebugException extends Exception {}
private PageReference createDSP(integer year) {
Distributor_Sales_Plan__c dsp = null;
PageReference ref = null;
ID accountID = ApexPages.currentPage().getParameters().get('id');
String profileName = [select Name from Profile where Id = :UserInfo.getProfileId() limit 1].Name;
if(profileName == 'System Administrator' || profileName.contains('CRM Manager')
|| [Select count() From AccountShare Where UserOrGroupId = :UserInfo.getUserId() And AccountId = :accountID And AccountAccessLevel IN ('All', 'Edit')] > 0){
try {
try {
dsp = [select Id From Distributor_Sales_Plan__c where Account__c = :accountID and Name = :string.valueOf(year) limit 1];
} catch (Exception e) {
dsp = null;
}
if (dsp==null) {
dsp = new Distributor_Sales_Plan__c(Account__c = accountID, Name = string.valueOf(year));
//throw new DebugException('duplicate record');
insert dsp;
}
ref = new ApexPages.StandardController(dsp).view();
ref.setRedirect(false);
} catch (Exception e) {
ApexPages.addMessages(e);
ref = null;
}
} else {
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'Insuff
return null;
}
return ref;
}
public PageReference ActionNotSupported() {
return null;
}
}
Naresh
Solved! Go to Solution.
Re: Test Clas
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
11-15-2012 04:29 AM
Have you tried writing something yourself already ?
Re: Test Clas
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
11-15-2012 08:42 AM
Hi,
To write test methods, you will have to create Test Data and call the controller methods using instance of your class.
The below links will help you to start writing test methods:
http://wiki.developerforce.com/page/An_Introductio
http://www.salesforce.com/us/developer/docs/apexco
Hope this helps :)
Thanks,
Devendra
Devendra
Re: Test Clas
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
11-15-2012 10:51 PM
Hi,
I tried with this but it has 66% only.
DSPCustomActions ca = new DSPCustomActions(ssc);
ref = ca.ActionNotSupported();
Distributor_Sales_Plan__c dspPrev;
insert dspPrev = new Distributor_Sales_Plan__c(Account__c = account.Id, Name=string.valueOf(prevYear), Account__r = account);
DSP_Product__c dspp1 = new DSP_Product__c(
Sales_Plan__c = dspPrev.Id,
Product_Shipper__c = labelParts[0].Id,
Type__c = 'Actual',
Jan__c = 20,
Account__c=account.Id);
insert dspp1;
DSP_Product__c dspp2 = new DSP_Product__c(
Sales_Plan__c = dspPrev.Id,
Competitor_Product__c = cmProducts[0].Id,
Jan__c = 20,
Account__c=account.Id);
insert dspp2;
Distributor_Sales_Plan__c dsp;
// dsp delete page
insert dsp = new Distributor_Sales_Plan__c(Account__c = account.Id, Name=string.valueOf(year), Account__r = account);
ApexPages.StandardController sc = new ApexPages.StandardController(dsp);
ca = new DSPCustomActions(sc);
ca.deleteDSP();
// dsp
insert dsp = new Distributor_Sales_Plan__c(Account__c = account2.Id, Name=string.valueOf(year), Account__r = account2);
DSP_Product__c dspp = new DSP_Product__c(
Sales_Plan__c = dsp.Id,
Product_Shipper__c = labelParts[0].Id,
Type__c = 'Plan',
Account__c=account2.Id);
sc = new ApexPages.StandardController(dspp);
ca = new DSPCustomActions(sc);
ref = ca.redirectToDSP();
dspp = new DSP_Product__c();
sc = new ApexPages.StandardController(dspp);
ca = new DSPCustomActions(sc);
ref = ca.redirectToDSP();
Naresh
Re: Test Clas
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
11-15-2012 11:26 PM
I think you missed to call createDSP() method.
Please check on this.
Thanks,
Devendra
Devendra

