Reply
Regular Contributor
KNK
Posts: 110
0
Accepted Solution

Test Clas

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,'Insufficient Priviledge'));
                    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,'Insufficient Priviledge'));
            return null;
        }          
        return ref;    
    }
   
     public PageReference ActionNotSupported() {
        return null;
    }  


}

Thanks,
Naresh
Trusted Contributor
Sdry
Posts: 306
0

Re: Test Clas

Have you tried writing something yourself already ?

may the force be with you | Certified Salesforce.com Administrator | Certified Salesforce Developer | www.absi.be
Super Contributor
Devendra@SFDC
Posts: 712
0

Re: Test Clas

 

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_Introduction_to_Apex_Code_Test_Methods

 

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_test.htm

 

Hope this helps :)

 

Thanks,

Devendra

Thanks,
Devendra
Regular Contributor
KNK
Posts: 110
0

Re: Test Clas

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();           

Thanks,
Naresh
Super Contributor
Devendra@SFDC
Posts: 712
0

Re: Test Clas

 

I think you missed to call createDSP() method.

 

Please check on this.

 

Thanks,

Devendra

Thanks,
Devendra