+ Start a Discussion
kevinedelmannkevinedelmann 

Newbie Test Help Needed

Hello All Mighty & Powerful force.com developers :smileyhappy:
 
I beseach you to help this lowly Admin who knows little of the coding you all do and for me it would take forever to decipher how to go about doing what is needed whilst you will surely be able to rattle off the answer quickly.
 
I saw an example of how to show other won opportunities on the current opportunity where the Current Carrier field values are the same.  I was able to get the code to work in the sandbox but cannot transfer it over to the live environment as I need some tests before I can.  I have no idea how to create tests so need your help badly! 
 
Below is the controller and Apex page:
 
Controller

public class OpportunityCompetitionController {Opportunity opp;
    private Boolean show = false;
    private Boolean hasCompetitor = false;
    private Boolean hasQueried = false;
    private List<Opportunity> wins;
    private final Set<String> IGNORE_SET = new Set<String>{'Other', 'Unknown', 'No Competition','None - Existing Customer'};
                                                                                                                 
    public OpportunityCompetitionController(ApexPages.StandardController stdController) {
        this.opp = (Opportunity)stdController.getRecord();
        this.opp = [Select id, Current_Carrier__c from Opportunity where id = :opp.id];
        initCompetitiveWins();
    }
    
    //getters (no setters, we don't let anyone set our values)
    public Boolean getShow()
    {
        return show;    
    }
     
    public Boolean getHasCompetitor()
    {
        return hasCompetitor;   
    }   
    
    public Boolean getHasQueried()
    {
        return hasQueried;  
    }
    
    public Boolean getHasWins()
    {
        return (wins != null && wins.size() > 0);   
    }      
    
    public List<Opportunity> getWins () 
    {
        return wins;   
    }  
            
    public String getNoCompetitorText() 
    {
        return 'Fill in Current Carrier above to get a list of our biggest wins against that competitor.';   
    }
    
    public String getNoWinsText() 
    {
        return 'There are no matching competitive wins against '+opp.Current_Carrier__c+'.';   
    }        
        
    public void initCompetitiveWins() {
 
        if (opp.Current_Carrier__c == null || IGNORE_SET.contains(opp.Current_Carrier__c))
        {
            hasCompetitor = false;
        }
        else
        {
            hasCompetitor = true;
        }
     }
    public void queryOpportunityWins()
    {
        if (opp.Current_Carrier__c != null && !IGNORE_SET.contains(opp.Current_Carrier__c))
        {
            try 
            {
                wins = [select id,
                           OwnerId,
                           Owner.Name,
                           AccountId,
                           Account.Name,
                           Name,
                           Amount,
                           Effective_Date__c,
                           Current_Carrier__c,
                           Producer_Account__c 
                        from Opportunity 
                        where   Current_Carrier__c = :opp.Current_Carrier__c 
                        and id != :opp.Id 
                        and IsWon = TRUE 
                        order by Amount desc limit 15];
            }
            catch (Exception e)
            {
                ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.FATAL, 
                   'Error retrieving competitive wins: ' + e.getMessage());
                ApexPages.addMessage(myMsg);                
            }
            hasQueried = true;                  
        }            }     }

Apex Page
 
<apex:page standardController="Opportunity" extensions="OpportunityCompetitionController">
<apex:form id="form">
<apex:pageBlock id="message" rendered="{!NOT(hasCompetitor)}">
       <!-- <apex:image id="messageIcon" value="{!$Resource.icon_information}"/>-->
       <span>{!noCompetitorText}</span>

</apex:pageBlock>
<apex:commandLink rendered="{!AND(hasCompetitor,NOT(hasQueried))}"

                  value="View Competitive Wins" action="{!queryOpportunityWins}" reRender="form"

                  status="queryStatus"/>

<apex:actionStatus id="queryStatus" startText="Searching..."/>

<apex:messages />

<apex:pageBlock id="block" rendered="{!hasQueried}">

     <apex:panelGroup rendered="{!NOT(hasWins)}">

       <!--  <apex:image id="messageIcon" value="{!$Resource.icon_information}"/> -->

         <span>{!noWinsText}</span>

     </apex:panelGroup>

      <apex:pageBlockTable rendered="{!hasWins}" value="{!wins}" var="win">

           <apex:column >

           <apex:outputLink value="/{!win.Id}" target="_top">{!win.Name}</apex:outputLink>

           <apex:facet name="header">Opportunity Name</apex:facet>

           </apex:column>

           <!-- <apex:column >
           <apex:outputLink value="/{!win.AccountId}" target="_top">{!win.Account.Name}</apex:outputLink>
           <apex:facet name="header">Account Name</apex:facet>          
           </apex:column> -->

           <apex:column >

           <apex:outputLink value="/{!win.OwnerId}" target="_top">{!win.Owner.Name}</apex:outputLink>

           <apex:facet name="header">Owner Name</apex:facet>

           </apex:column>/>

           <apex:column ><apex:outputField value="{!win.Amount}" />

           <apex:facet name="header">Amount</apex:facet>

           </apex:column>

           <apex:column >

           <apex:outputField value="{!win.Current_Carrier__c}" />

           <apex:facet name="header">Competition</apex:facet>
           </apex:column>
           <apex:column ><apex:outputField value="{!win.Effective_Date__c}" />
           <apex:facet name="header">Eff. Date</apex:facet>
           </apex:column>
           <apex:column ><apex:outputField value="{!win.Producer_Account__c}" />
           <apex:facet name="header">Producer Co.</apex:facet>
           </apex:column>

       </apex:pageBlockTable>
</apex:pageBlock> 
</apex:form>    
</apex:page>

Again, I beseach thee for any and all help that can be bestowed up this humble admin. 

Thanks in advance,

Kevin

RickyGRickyG
Kevin -

First of all, you should look at the Apex doc for information about testing in general.  Then take a look at the Visualforce guide for an example of how to instantiate a page and controller.  Once you get this down, you can simply do some simple setting of variables in the test to exercise the get methods in your controller.

Hope this helps - my guess is that this is enough guidance for you to get going, considering that you could put together a controller and Visualforce page, even as a "lowly admin".  :smileywink: