0

I have created a simple apex class to be used in conjunction with a visualforce page. Everything is working as I had intended it to in the sandbox and I am now ready to push it over to production. The only problem is that I have no idea how or where to begin writing a test class to test my code coverage.

Here is the class that I created:

public with sharing class clsOpportunity10 {

    public PageReference save() {
        update opportunities;       
        update prospects;
        update pursuits;
        update closing;
        update budgeting;
        update promised;
        return null;
    }


    public List<Opportunity> opportunities {get;set;}
    public clsOpportunity10()
    {
        opportunities = [select name, Project_Delivery_Method__c, Amount, Gross_Margin__c, Gross_Margin1__c, Probability, Probability_of_Owner_Proceeding__c, Bid_Due_Date__c, CloseDate, stagename, First_Year_Burn__c, Second_Year_Burn__c, Third_Year_Burn__c, Fourth_Year_Burn__c, Current_120_Day_List__c, Affecting_Current_Year_Book_and_Burn__c, Pathway_to_CY_Plan__c from opportunity where StageName = 'Needs Qualifying' and Branch__c = '10 - Western PA' and RecordType.Name = 'Construction' ORDER BY Probability DESC];
    }

    public list<Opportunity> prospects {get;set;}

    {
        prospects = [select name, Project_Delivery_Method__c, Amount, Gross_Margin__c, Gross_Margin1__c, Probability, Probability_of_Owner_Proceeding__c, Bid_Due_Date__c, CloseDate, stagename, First_Year_Burn__c, Second_Year_Burn__c, Third_Year_Burn__c, Fourth_Year_Burn__c, Current_120_Day_List__c, Affecting_Current_Year_Book_and_Burn__c, Pathway_to_CY_Plan__c from opportunity where StageName = 'Selling' and Branch__c = '10 - Western PA' and RecordType.Name = 'Construction' ORDER BY Probability DESC];
    }

    public list<Opportunity> pursuits {get;set;}

    {
        pursuits = [select name, Project_Delivery_Method__c, Amount, Gross_Margin__c, Gross_Margin1__c, Probability, Probability_of_Owner_Proceeding__c, Bid_Due_Date__c, CloseDate, stagename, First_Year_Burn__c, Second_Year_Burn__c, Third_Year_Burn__c, Fourth_Year_Burn__c, Current_120_Day_List__c, Affecting_Current_Year_Book_and_Burn__c, Pathway_to_CY_Plan__c from opportunity where StageName = 'Proposal' and Branch__c = '10 - Western PA' and RecordType.Name = 'Construction' ORDER BY Probability DESC];
    }

     public list<Opportunity> closing {get;set;}

    {
        closing = [select name, Project_Delivery_Method__c, Amount, Gross_Margin__c, Gross_Margin1__c, Probability, Probability_of_Owner_Proceeding__c, Bid_Due_Date__c, CloseDate, stagename, First_Year_Burn__c, Second_Year_Burn__c, Third_Year_Burn__c, Fourth_Year_Burn__c, Current_120_Day_List__c, Affecting_Current_Year_Book_and_Burn__c, Pathway_to_CY_Plan__c from opportunity where StageName = 'Closing' and Branch__c = '10 - Western PA' and RecordType.Name = 'Construction' ORDER BY Probability DESC];
    }

     public list<Opportunity> budgeting {get;set;}

    {
        budgeting = [select name, Project_Delivery_Method__c, Amount, Gross_Margin__c, Gross_Margin1__c, Probability, Probability_of_Owner_Proceeding__c, Bid_Due_Date__c, CloseDate, stagename, First_Year_Burn__c, Second_Year_Burn__c, Third_Year_Burn__c, Fourth_Year_Burn__c, Current_120_Day_List__c, Affecting_Current_Year_Book_and_Burn__c, Pathway_to_CY_Plan__c from opportunity where StageName = 'Budgeting' and Branch__c = '10 - Western PA' and RecordType.Name = 'Construction' ORDER BY Probability DESC];
    }

     public list<Opportunity> promised {get;set;}

    {
        promised = [select name, Project_Delivery_Method__c, Amount, Gross_Margin__c, Gross_Margin1__c, Probability, Probability_of_Owner_Proceeding__c, Bid_Due_Date__c, CloseDate, stagename, First_Year_Burn__c, Second_Year_Burn__c, Third_Year_Burn__c, Fourth_Year_Burn__c, Current_120_Day_List__c, Affecting_Current_Year_Book_and_Burn__c, Pathway_to_CY_Plan__c from opportunity where StageName = 'Promised' and Branch__c = '10 - Western PA' and RecordType.Name = 'Construction' ORDER BY Probability DESC];
    }

}

I am new to apex (and writing code in general) so, any help would be greatly appreciated.

1
  • What have you tried so far? What part of the docs or online tutorials are you stuck on? For your controller, you'd want your test methods to assert that you're getting the lists that you expect, and that when the save button is pressed, the records are updated.
    – Nick C
    Commented Jul 7, 2016 at 15:11

1 Answer 1

2

You can refer this link An Introduction to Apex Code Test Methods

to start writing test classes.

Below I have tried to show you following things: 1. How to create setup data. 2. How to create user for which you will run the test scenarios.

@isTest
public class clsOpportunity10_Test{
@testSetup static void prepareControllerClassData() 
{
    //retrieve the profile information
    Map<String,Id> profileIdNameMap = TestUtil.prepareProfileIdNameMap();

   //create Executive user.
    User mbExecutiveUser = TestUtil.createUser('Executive', 'user', profileIdNameMap.get('Executive')); 
    System.runAs(mbExecutiveUser)
     {
         createOpportunities();            
     }      
}


// create an user for the test class 
public static User createUser(String firstName, String lastName, String profileId){
    String sRandom = String.ValueOf(Integer.valueOf(math.rint(math.random()*1000000)));
    User newUser = new User(
                ProfileId = profileId,
                Firstname=firstName,
                Lastname=lastName,
                Username = firstName + sRandom + '@xyz.com',                    
                Alias = firstName.substring(0,2) + lastName.substring(0,2),
                Email=firstName + lastName + '@xyz.com',
                EmailEncodingKey='UTF-8',
                LanguageLocaleKey='en_US',                    
                LocaleSidKey='en_US',
                TimeZoneSidKey='America/Chicago'
            );
    return newUser;
}

public static Map<String,Id> prepareProfileIdNameMap()
{
    if(profileIdNameMap.isEmpty())
    {
        List<Profile> lstProfile = [SELECT Id, Name FROM Profile WHERE Name IN (<specify all the profile names>)];

        for(Profile objProfile:lstProfile)
        {
            profileIdNameMap.put(objProfile.Name, objProfile.Id);
        }            
    }
    return profileIdNameMap;        
}

public static void createOpportunities()
{
    List<Opportunity> lstOpportunity = new List<Opportunity>();
    Opportunity oppty1 = new Opportunity();
    oppty1.RecordTypeId= Schema.SObjectType.Opportunity.getRecordTypeInfosByName().get('Construction').getRecordTypeId();
    oppty1.StageName = 'Needs Qualifying';
    oppty1.Branch__c = '10 - Western PA';
    //assign all the values to be respective attributes
    lstOpportunity.add(oppty1);

    //same way for other stages and branches create opportunity data.
    //and finally insert the list
    insert(lstOpportunity);
}

public static void updateOpportunities()
{
    System.runAs(mbExecutiveUser)
    {
        //you will retrieve the opportunity record

        //update the record

        //put System.assert() to verify values
    }
}}
2
  • As link-only answers are frowned upon here, this would be better as a comment on the question.
    – Keith C
    Commented Jul 7, 2016 at 15:44
  • @Bryan Gallagher I have added code sample, hopefully it will serve the purpose Commented Jul 7, 2016 at 17:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .