+ Start a Discussion
StaciStaci 

Test Class Help Please

I have a test class started, got the dummy data created and now I'm not sure where to go.

How do you write a test class for all this??  Can't SF create something that would automatically create the test class for you????
public with sharing class LicenseList { 
private final License_Numbers__c ln; 
public user currentuser{get;set;} 
public id tobeEdited{get;set;}
public decimal aTotal{get;set;}
public decimal tTotal{get;set;}
public id tobeAdded{get;set;}
public List<AccountWrapper> wrappers{get;set;}
public static Integer addCount{get;set;}
private Integer nextIdent=0;

public LicenseList(ApexPages.StandardSetController controller) { 
currentuser=new User(); 
currentuser=[Select Id, Business_Unit_new__c from User where Id=:userinfo.getuserId()]; 
this.ln = (License_Numbers__c)controller.getRecord(); 

wrappers=new List<AccountWrapper>();
for(Integer idx=0; idx<1; idx++)
{
    wrappers.add(new AccountWrapper(nextIdent++));
}

} 

//------------------------------------------------------------------------------------

public List<License_Numbers__c> lntypes16 = new List<License_Numbers__c>();

public List <License_Numbers__c> getLicenseList(){ 
lntypes16 = [select id, Name, X2016_Cost__c, Business_Unit__c, X2016_Starting_Amount__c, X2016_Subtotal__c, BMS_Code__c, License_Type__c, Monthly_Unit_Price__c, Running_License_Total__c, Org__c FROM License_Numbers__c where Business_Unit__c =:currentuser.Business_Unit_new__c AND License_Year__c = '2016' ORDER BY Order_Number__c, License_Type__c];
       

return lntypes16; 
} 


//-----------------------------------------------------------------------------
public List<License_Numbers__c> selectedTypes = new List<License_Numbers__c>();
 
public List <License_Numbers__c> getLicenseList17(){ 

selectedTypes=[select id, Name, BU_Agrees_to_Pay__c, Annualized_Running_Total__c, X2017_Cost__c, Business_Unit__c,X2017_Total_Needed__c, X2016_Starting_Amount__c, X2017_Subtotal__c, BMS_Code__c, License_Type__c, Monthly_Unit_Price__c, Org__c FROM License_Numbers__c where Business_Unit__c =:currentuser.Business_Unit_new__c AND License_Year__c = '2017' ORDER BY Order_Number__c, License_Type__c]; 
 aTotal = 0.0;
 tTotal = 0.0;       
        for(License_Numbers__c a:selectedTypes){
                aTotal = aTotal + a.X2017_Cost__c; 
                tTotal = tTotal + a.X2017_Subtotal__c;  
        }
return selectedTypes; 
} 
    public void saveRecord(){ 
    License_Numbers__c tobeupdated;
    for(License_Numbers__c temp:selectedTypes){
        if(temp.id==tobeEdited){
            tobeupdated = temp;
            
         }
     }
        update selectedTypes;
        tobeEdited = null;
    } 
//--------------------------------------------------------------------------------------------    

public List<License_Numbers__c> addlic {get; set;} 
  
public PageReference save()
{
    List<License_Numbers__c> addlic=new List<License_Numbers__c>();
    for(AccountWrapper wrap:wrappers)
    {
        addlic.add(wrap.lic);
    }
    insert addlic;
    
    return new PageReference('/apex/LicenseSummaryHomePage');
}

public class AccountWrapper
{
    public License_Numbers__c lic{get; private set;}
    public Integer ident{get; private set;}
    
    public AccountWrapper(Integer inIdent)
    {
        ident=inIdent;
        lic=new License_Numbers__c();
    }
}   

//-------------------------------------------------------------------------------------------

public SelectOption[] getSchedules() {
        return new SelectOption[] { 
            new SelectOption('Value0', '--None--'),
            new SelectOption('Value1', 'Value1'),
            new SelectOption('Value2', 'Value2'), 

  };

    }
public String discountScheduleID { get; set; }

//--------------------------------------------------------------
 Public string OutPutString{get;set;}
 Public String SelectOption;
public void displayDescription(){
if (discountScheduleID == 'Value1')
{
OutPutString = 'No Description Available' ;
}else if (discountScheduleID == 'Value2')
    {
        OutPutString = 'No Description Available' ;
}}





 
@isTest 
private class LicenseListTest{
   
   static testMethod void LicenseListTest(){
        Profile p = [SELECT Id FROM Profile WHERE Name='Community Super User' limit 1]; 
        //create a user
        UserRole r = [SELECT Id FROM userrole WHERE name = 'CM User' limit 1];
        
        User u1 = new User(LastName = 'Test Class',
                                    Username = 'CRMUser@Test.com',
                                    UserRoleId = r.Id,
                                    Email = 'CRMUser@Test.com',
                                    Alias = 'tclass',
                                    CommunityNickname = 'tclass',
                                    TimeZoneSidKey = 'Asia/Kolkata',
                                    LocaleSidKey = 'en_US',
                                    EmailEncodingKey = 'UTF-8', 
                                    ProfileId = p.Id,
                                    LanguageLocaleKey = 'en_US',
                                    Business_Unit__c = 'GCI');
        insert u1;

}
static testMethod void LicenseList_test() {

//create a license Record
        License_Numbers__c ln1 = new License_Numbers__c(
                                     Business_Unit__c = 'GCI', 
                                     X2016_Starting_Amount__c = 5, 
                                     BMS_Code__c = 'X1357', 
                                     License_Type__c = 'Service Cloud - Unlimited Edition', 
                                     Monthly_Unit_Price__c = 83.00, 
                                     Running_License_Total__c = 500, 
                                     Org__c = 'CRM Innovation', 
                                     License_Year__c = '2016');
        insert ln1;
        
         //create a license Record
        License_Numbers__c ln2 = new License_Numbers__c(
                                     Business_Unit__c = 'GCI', 
                                     X2016_Starting_Amount__c = 5, 
                                     BMS_Code__c = 'X1357', 
                                     License_Type__c = 'Analytics Cloud - Wave Analytics Platform', 
                                     Monthly_Unit_Price__c = 52.50, 
                                     Org__c = 'CRM Innovation', 
                                     License_Year__c = '2016');
        insert ln2;
}



static testMethod void LicenseList17_test() {
//create a license Record
        License_Numbers__c ln3 = new License_Numbers__c(
                                     Business_Unit__c = 'GCI', 
                                     X2016_Starting_Amount__c = 5, 
                                     BMS_Code__c = 'X1357', 
                                     License_Type__c = 'Service Cloud - Unlimited Edition', 
                                     Monthly_Unit_Price__c = 83.00, 
                                     Running_License_Total__c = 500, 
                                     Org__c = 'CRM Innovation', 
                                     License_Year__c = '2017');
        insert ln3;
        
         //create a license Record
        License_Numbers__c ln4 = new License_Numbers__c(
                                     Business_Unit__c = 'GCI', 
                                     X2016_Starting_Amount__c = 5, 
                                     BMS_Code__c = 'X1357', 
                                     License_Type__c = 'Analytics Cloud - Wave Analytics Platform', 
                                     Monthly_Unit_Price__c = 52.50, 
                                     Org__c = 'CRM Innovation', 
                                     License_Year__c = '2017');
        insert ln4;

}


}

 
SFDC_BigDogSFDC_BigDog

Could you also post your visualforce page associated with this apex class.
 

Thanks

StaciStaci
@SFDC_BigDog
<apex:page standardController="License_Numbers__c" recordSetvar="lic" extensions="LicenseList" showHeader="false" sidebar="false">
<!------------------------------------------------------------------------------------------------------>
<!--Based on option chosen for License Type, will show description of that license type-->
<apex:pageBlock title="License Definitions">
<apex:form id="theForm">
<apex:pageBlockSection columns="1">
<apex:selectList label="Please select a License Type"  value="{!discountScheduleID}" size="1" >
   <apex:actionSupport event="onchange" action="{!displayDescription}" rerender="description"/>
   <apex:selectOptions value="{!schedules}" />
</apex:selectList> 

<apex:outputText label="Description" id="description" value="{!OutPutString}"/>

<br></br><br></br><br></br><br></br>
</apex:pageBlockSection>
</apex:form>
</apex:pageBlock>
<!---------------------------------------------------------------------------------------------------->
<apex:form >
<!--displays the licenses that are owned by your business unit (declared on User record)-->
<apex:pageBlock title="Current Licenses">
<apex:pageBlockSection columns="1">

<apex:pageBlockTable value="{!LicenseList}" var="ln">

<apex:outputLink value="{!ln.Id}">{!ln.Name}</apex:outputLink>

<apex:column value="{!ln.Org__c}" />
<apex:column value="{!ln.Business_Unit__c}" />
<apex:column value="{!ln.BMS_Code__c}" />
<apex:column value="{!ln.License_Type__c}" />
<apex:column value="{!ln.Monthly_Unit_Price__c}" />
<apex:column headerValue="2016 Starting Amount" value="{!ln.X2016_Starting_Amount__c}" />
<apex:column headerValue="2016 Running License Total" value="{!ln.Running_License_Total__c}" />
<apex:column headerValue="2016 Annualized Cost" value="{!ln.X2016_Subtotal__c}" />
<apex:column headerValue="2016 Cost" value="{!ln.X2016_Cost__c}"/>

</apex:pageBlockTable>
</apex:pageBlockSection>

                
          
</apex:pageBlock>

</apex:form>

<apex:panelGrid columns="1">

</apex:panelGrid>



<!--------------------------------------------------------------------------------------------------------->
<apex:form id="theForm">
<apex:pageBlock id="blockId" title="2017 License Needs">

<apex:pageBlockSection columns="2">

<td>
<apex:outputText style="font-weight:800" label="Total 2017 Annualized Cost" value="${0, number, ###,###,###,##0.00}">
    <apex:param value="{!tTotal}" />
</apex:outputText>
</td>
<td>
<apex:outputText style="font-weight:800" label="Total 2017 Estimated Cost" value="${0, number, ###,###,###,##0.00}">
    <apex:param value="{!aTotal}" />
</apex:outputText>
</td>
</apex:pageBlockSection>

<!-------------------------------------------------------------------------------------------------->


<apex:pageBlockTable value="{!LicenseList17}" var="ln17">

<apex:outputLink value="{!ln17.Id}">{!ln17.Name}</apex:outputLink>
<apex:column >
<apex:commandbutton value="Edit" rerender="blockId" rendered="{!!(tobeEdited == ln17.id)}">
    <apex:param assignTo="{!tobeEdited}" value="{!ln17.id}" name="ittobeedited"/>
</apex:commandbutton>
<apex:commandButton value="Save" action="{!saveRecord}" reRender="blockId" rendered="{!tobeEdited == ln17.id}"/>
</apex:column> 

<apex:column value="{!ln17.Org__c}" />

<apex:column headerValue="BMS Code">
    <!--Allows it to be edited-->
    <apex:inputfield value="{!ln17.BMS_Code__c}" rendered="{!tobeEdited == ln17.id}"/>
    <!--After Save-->
    <apex:outputField value="{!ln17.BMS_Code__c}" rendered="{!!(tobeEdited == ln17.id)}"/>
</apex:column>    

<apex:column value="{!ln17.License_Type__c}"/>

<apex:column headerValue="BU Agrees to Pay">
    <apex:inputfield value="{!ln17.BU_Agrees_to_Pay__c}" rendered="{!tobeEdited == ln17.id}"/>
    <apex:outputField value="{!ln17.BU_Agrees_to_Pay__c}" rendered="{!!(tobeEdited == ln17.id)}"/>
</apex:column> 

<apex:column value="{!ln17.Monthly_Unit_Price__c}" />

<apex:column headerValue="2017 Number Needed">
    <apex:inputfield value="{!ln17.X2017_Total_Needed__c}" rendered="{!tobeEdited == ln17.id}"/>
    <apex:outputField value="{!ln17.X2017_Total_Needed__c}" rendered="{!!(tobeEdited == ln17.id)}"/>
</apex:column>    

<apex:column headerValue="2017 Annualized Cost" value="{!ln17.X2017_Subtotal__c}" />

<apex:column headerValue="Estimated 2017 Cost" value="{!ln17.X2017_Cost__c}"/>

</apex:pageBlockTable>

</apex:pageBlock>

<!------------------------------------------------------------------------------------------>
<apex:pageBlock title="New Licenses Needed for 2017">
<apex:pageBlockSection columns="1">
<apex:pageBlockTable value="{!wrappers}" var="wrapper" id="wtable">

<apex:column HeaderValue="Org">
<apex:inputField value="{!wrapper.lic.Org__c}" />
</apex:column>

<apex:column HeaderValue="Business Unit">
<apex:inputField value="{!wrapper.lic.Business_Unit__c}" />
</apex:column>

<apex:column HeaderValue="BMS Code">
<apex:inputField value="{!wrapper.lic.BMS_Code__c}" />
</apex:column>

<apex:column HeaderValue="License Type">
<apex:inputField value="{!wrapper.lic.License_Type__c}"  />
</apex:column>

<apex:column HeaderValue="BU Agrees to Pay">
<apex:inputField value="{!wrapper.lic.BU_Agrees_to_Pay__c}" />
</apex:column>

<apex:column HeaderValue="2017 Number Needed">
<apex:inputField value="{!wrapper.lic.X2017_Total_Needed__c}" />
</apex:column>

<apex:column HeaderValue="License Year">
<apex:inputField value="{!wrapper.lic.License_Year__c}" />
</apex:column>

</apex:pageBlockTable>
</apex:pageBlockSection>
<apex:commandButton value="Save" action="{!save}"/>
</apex:pageBlock>



</apex:form>

<!--------------------------------------------------------------------------------------------------------->
</apex:page>

 
StaciStaci
Hi @SFDC_BigDog

I'm starting to get some coverage, but can you help me cover the 
public SelectOption[] getSchedules() { 
part?

Here's what I have so far for everything else - its not complete, but its getting there. 

Thanks!
 
@isTest 
private class LicenseListTest{

   
   static testMethod void LicenseListTest(){
        Profile p = [SELECT Id FROM Profile WHERE Name='Community Super User' limit 1]; 
        //create a user
        UserRole r = [SELECT Id FROM userrole WHERE name = 'CM User' limit 1];
        
        
        
        User u1 = new User(LastName = 'Test Class',
                                    Username = 'CRMUser@Test.com',
                                    UserRoleId = r.Id,
                                    Email = 'CRMUser@Test.com',
                                    Alias = 'tclass',
                                    CommunityNickname = 'tclass',
                                    TimeZoneSidKey = 'Asia/Kolkata',
                                    LocaleSidKey = 'en_US',
                                    EmailEncodingKey = 'UTF-8', 
                                    ProfileId = p.Id,
                                    LanguageLocaleKey = 'en_US',
                                    Business_Unit__c = 'GCI');
        insert u1;
        
        

}
static testMethod void LicenseList_test() {
List<License_Numbers__c> newlic16 = new List<License_Numbers__c>();
//create a license Record
        License_Numbers__c ln1 = new License_Numbers__c(
                                     Business_Unit__c = 'GCI', 
                                     X2016_Starting_Amount__c = 5, 
                                     BMS_Code__c = 'X1357', 
                                     License_Type__c = 'Service Cloud - Unlimited Edition', 
                                     Monthly_Unit_Price__c = 83.00, 
                                     Running_License_Total__c = 500, 
                                     Org__c = 'CRM Innovation', 
                                     License_Year__c = '2016');
        newlic16.add(ln1);
        
         //create a license Record
        License_Numbers__c ln2 = new License_Numbers__c(
                                     Business_Unit__c = 'GCI', 
                                     X2016_Starting_Amount__c = 5, 
                                     BMS_Code__c = 'X1357', 
                                     License_Type__c = 'Analytics Cloud - Wave Analytics Platform', 
                                     Monthly_Unit_Price__c = 52.50, 
                                     Org__c = 'CRM Innovation', 
                                     License_Year__c = '2016');
         newlic16.add(ln2);
           
  
Test.startTest();
ApexPages.StandardSetController con1 = new ApexPages.StandardSetController(newlic16);
LicenseList controller = new LicenseList(con1);
controller.saveRecord();
Test.stopTest();  
  
  
         
}        
        //Test.startTest();
        List<License_Numbers__c> lntypes16 = [select id FROM License_Numbers__c where License_Year__c = '2016' ORDER BY Order_Number__c, License_Type__c limit 1];
        //Test.stopTest();
        
        
        List<License_Numbers__c> selectedTypes=[select id, Name, BU_Agrees_to_Pay__c, Annualized_Running_Total__c, X2017_Cost__c, Business_Unit__c,X2017_Total_Needed__c, X2016_Starting_Amount__c, X2017_Subtotal__c, BMS_Code__c, License_Type__c, Monthly_Unit_Price__c, Org__c FROM License_Numbers__c where  License_Year__c = '2017' ORDER BY Order_Number__c, License_Type__c]; 
     




static testMethod void LicenseList17_test() {

List<License_Numbers__c> newlic17 = new List<License_Numbers__c>();
//create a license Record
        License_Numbers__c ln3 = new License_Numbers__c(
                                     Business_Unit__c = 'GCI', 
                                     X2016_Starting_Amount__c = 5, 
                                     BMS_Code__c = 'X1357', 
                                     License_Type__c = 'Service Cloud - Unlimited Edition', 
                                     Monthly_Unit_Price__c = 83.00, 
                                     Running_License_Total__c = 500, 
                                     Org__c = 'CRM Innovation', 
                                     License_Year__c = '2017');
        newlic17.add(ln3);
        
         //create a license Record
        License_Numbers__c ln4 = new License_Numbers__c(
                                     Business_Unit__c = 'GCI', 
                                     X2016_Starting_Amount__c = 5, 
                                     BMS_Code__c = 'X1357', 
                                     License_Type__c = 'Analytics Cloud - Wave Analytics Platform', 
                                     Monthly_Unit_Price__c = 52.50, 
                                     Org__c = 'CRM Innovation', 
                                     License_Year__c = '2017');
        newlic17.add(ln4);
    
 Test.startTest();
ApexPages.StandardSetController con2 = new ApexPages.StandardSetController(newlic17);
LicenseList controller = new LicenseList(con2);
controller.save();
Test.stopTest();


}


}

 
StaciStaci

@SFDC_BigDog

I have almost everything covered excep the OutPutString lines.  How do I get those covered?
 
Public string OutPutString{get;set;}

 Public String SelectOption;

public void displayDescription(){

if (discountScheduleID == 'Value1')

{

OutPutString = 'No Description Available' ;

}else if (discountScheduleID == 'Value2')

    {

        OutPutString = 'No Description Available' ;

}}


 
@isTest 
private class LicenseListTest{
   
static testMethod void LicenseListTest(){
        Profile p = [SELECT Id FROM Profile WHERE Name='Community Super User' limit 1]; 
        //create a user
        UserRole r = [SELECT Id FROM userrole WHERE name = 'CM User' limit 1];
        
        
        
        User u1 = new User(LastName = 'Test Class',
                                    Username = 'CRMUser@Test.com',
                                    UserRoleId = r.Id,
                                    Email = 'CRMUser@Test.com',
                                    Alias = 'tclass',
                                    CommunityNickname = 'tclass',
                                    TimeZoneSidKey = 'Asia/Kolkata',
                                    LocaleSidKey = 'en_US',
                                    EmailEncodingKey = 'UTF-8', 
                                    ProfileId = p.Id,
                                    LanguageLocaleKey = 'en_US',
                                    Business_Unit__c = 'GCI');
        insert u1;
        
        

}
static testMethod void LicenseList_test() {
List<License_Numbers__c> newlic16 = new List<License_Numbers__c>();
//create a license Record
        License_Numbers__c ln1 = new License_Numbers__c(
                                     Business_Unit__c = 'GCI', 
                                     X2016_Starting_Amount__c = 5, 
                                     BMS_Code__c = 'X1357', 
                                     License_Type__c = 'Service Cloud - Unlimited Edition', 
                                     Monthly_Unit_Price__c = 83.00, 
                                     Running_License_Total__c = 500, 
                                     Org__c = 'CRM Innovation', 
                                     License_Year__c = '2016');
        newlic16.add(ln1);
        
         //create a license Record
        License_Numbers__c ln2 = new License_Numbers__c(
                                     Business_Unit__c = 'GCI', 
                                     X2016_Starting_Amount__c = 5, 
                                     BMS_Code__c = 'X1357', 
                                     License_Type__c = 'Analytics Cloud - Wave Analytics Platform', 
                                     Monthly_Unit_Price__c = 52.50, 
                                     Org__c = 'CRM Innovation', 
                                     License_Year__c = '2016');
         newlic16.add(ln2);
           
  
Test.startTest();
ApexPages.StandardSetController con1 = new ApexPages.StandardSetController(newlic16);
LicenseList controller = new LicenseList(con1);

controller.saveRecord();
Test.stopTest();  
  
  
         
}        
        //Test.startTest();
        List<License_Numbers__c> lntypes16 = [select id FROM License_Numbers__c where License_Year__c = '2016' ORDER BY Order_Number__c, License_Type__c limit 1];
        //Test.stopTest();
        
        
        List<License_Numbers__c> selectedTypes=[select id, Name, BU_Agrees_to_Pay__c, Annualized_Running_Total__c, X2017_Cost__c, Business_Unit__c,X2017_Total_Needed__c, X2016_Starting_Amount__c, X2017_Subtotal__c, BMS_Code__c, License_Type__c, Monthly_Unit_Price__c, Org__c FROM License_Numbers__c where  License_Year__c = '2017' ORDER BY Order_Number__c, License_Type__c]; 
     




static testMethod void LicenseList17_test() {

List<License_Numbers__c> newlic17 = new List<License_Numbers__c>();
//create a license Record
        License_Numbers__c ln3 = new License_Numbers__c(
                                     Business_Unit__c = 'GCI', 
                                     X2016_Starting_Amount__c = 5, 
                                     BMS_Code__c = 'X1357', 
                                     License_Type__c = 'Service Cloud - Unlimited Edition', 
                                     Monthly_Unit_Price__c = 83.00, 
                                     Running_License_Total__c = 500, 
                                     Org__c = 'CRM Innovation', 
                                     License_Year__c = '2017');
        newlic17.add(ln3);
        
         //create a license Record
        License_Numbers__c ln4 = new License_Numbers__c(
                                     Business_Unit__c = 'GCI', 
                                     X2016_Starting_Amount__c = 5, 
                                     BMS_Code__c = 'X1357', 
                                     License_Type__c = 'Analytics Cloud - Wave Analytics Platform', 
                                     Monthly_Unit_Price__c = 52.50, 
                                     Org__c = 'CRM Innovation', 
                                     License_Year__c = '2017');
        newlic17.add(ln4);
        
          
    
Test.startTest();

ApexPages.StandardSetController con2 = new ApexPages.StandardSetController(newlic17);
LicenseList controller = new LicenseList(con2);
controller.getLicenseList();
controller.getLicenseList17();
controller.save();
controller.saveRecord();
controller.displayDescription();
controller.getSchedules();

      
Test.stopTest();


}


static testMethod void displayDescription_test() {




}




}