+ Start a Discussion
NNRNNR 

How to Write Unit Test for below class

list<string> rightvalues{set;get;}
  public PageReference updateMarkets(){
        oppsToCreateList.clear();
        selectedMarketsList.clear();
        selectedMarketsList.addall(rightvalues);
        system.debug('000000000000000000000'+rightvalues+selectedMarketsList);
        for (Integer i=0; i<selectedMarketsList.size(); i++){
            String themkt = selectedMarketsList.get(i);
            
            boolean alreadyAdded = false;
            //## see if the Market is already in the oppsToCreateList
            for (Opportunity tmpopp : oppsToCreateList)
            {
                if (themkt == tmpopp.opportunity_multi__c)
                {
                    alreadyAdded = true;
                }   
            }
            
            if (themkt == opp.opportunity_multi__c || alreadyAdded)
            {
                //## don't re-add the original opp to the list, and don't
                //## add the same record to the list multiple times.
            }
            else
            {
                Opportunity newopp = opp.clone(false);
                Newopp.OrderNumber__c = themkt;
                newopp.accountid=acc.id;
                newopp.ownerId=UserInfo.getUserId();
                newopp.stagename='1 - Prospect Evaluation';
                newopp.closedate=date.today();
                newopp.Name =acc.name;
                oppsToCreateList.add(newopp);
   
            }
            
        }
        
      system.debug('1111111111111111111111111111111111111'+oppsToCreateList);
     return null;
       
   }
   public PageReference  SaveRecords(){
      if (oppsToCreateList != null && oppsToCreateList.size() > 0)
        {
            try
            {       
                insert oppsToCreateList;
            }
            catch (Exception e)
            {
                ApexPages.addMessages(e);
                return null;        
            }
        }
      return new PageReference('/'+acc.Id);
   }

}
 
Geoffrey J FlynnGeoffrey J Flynn
Easiest way is to think about how you would create this through clicks in the UI, both to get it to work, and to get it to fail (negative testing)

You need to create an Opportunity for tmpopp including setting the opportunity_multi__c field
You need to at some point have that field equal themkt
You also need to at some point have that field not equal themkt

That should cover most of it.  
More or less:
Opportunity o = new Opportunity();
o.Name = ....
o.StageName = ....
//more mandatory fields
insert o;
o.opportunity_multi__c = something;
update o;
o.opportunity_multi__c = something else;
update o;
//whatever else is needed

 
NNRNNR
Hi Geoffrey
if you don't mind please go throw below link,in that link i written my class and test class but am not getting my code coverage 75% but it is only 70%,please help me out..

https://developer.salesforce.com/forums/ForumsMain?id=906F0000000ApnPIAS

Thanks and Regards
NNR