function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
mahe_developermahe_developer 

Test methods

Dear All,

 

 I am not able to Increase the code coverage for following code Indiacted with Red color, over all i got 45 % of test coverage.Can somebody help me...

 

Thanks 

 

Mahesh

 

public class metric4 {
 2	  List<Opportunity> OppList = new List<Opportunity>();
 3	   List<User> UserList = new List<User>();
 4	   public Id selectedUser{set;get;}
 5	   public List<SelectOption> usersList{set;get;}
 6	   public list<opportunity>oppolist{set;get;}
 7	   public String selectedopportunity{set;get;}
 8	   public List<SelectOption>opportunityList{set;get;}
 9	   public boolean showOpportunity{set;get;}
 10	   public boolean showRFQ{set;get;}
 11	   public boolean showProposalSent{set;get;}
 12	   public boolean showNegotiation{set;get;}
 13	   public boolean showClosedLost{set;get;}
 14	   public boolean showOpportunityScrapped{set;get;}
 15	   public boolean showOnHold{set;get;}
 16	   public boolean showClosedWon{set;get;}
 17	   public list<opportunity> listOpp1{set;get;}
 18	   public String strSelectedUser;
 19	   public String strSelectedStage;
 20	  
 21	   public Metric4()
 22	   {
 23	   }
 24	  public List<SelectOption>getUsers()
 25	   {
 26	   List<User> users = [select Id,name from User where isActive=true];
 27	   usersList = new List<SelectOption>();
 28	   usersList.add(new SelectOption('','-None-'));
 29	   for(User u:users)
 30	   {
 31	   usersList.add(new SelectOption(u.Id,u.name));
 32	   }
 33	   return usersList;
 34	   }
 35	   public List<SelectOption>getopportunities()
 36	   {
 37	   opportunityList = new List<SelectOption>();
 38	   opportunityList.add(new SelectOption('','-None-'));
 39	   opportunityList.add(new SelectOption('Opportunity','Opportunity'));
 40	   opportunityList.add(new SelectOption('RFQ','RFQ'));
 41	   opportunityList.add(new SelectOption('Proposal Sent','Proposal Sent'));
 42	   opportunityList.add(new SelectOption('Negotiation/Review','Negotiation/Review'));
 43	   opportunityList.add(new SelectOption('Closed Lost','Closed Lost'));
 44	   opportunityList.add(new SelectOption('Opportunity Scrapped','Opportunity Scrapped'));
 45	   opportunityList.add(new SelectOption('Closed Won','Closed Won'));
 46	   opportunityList.add(new SelectOption('On Hold','On Hold'));
 47	   return opportunityList;
 48	   }
 49	  
 50	   public void submit()
 51	   {
 52	   strSelectedUser = selectedUser;
 53	   System.Debug('----ANITHA----------Selected User------------------------'+strSelectedUser);
 54	   strSelectedStage = selectedopportunity;
 55	   System.Debug('----ANITHA----------Selected Stage-----------------------'+strSelectedStage);
 56	   System.Debug('----ANITHA----------Selected Proposal Sent TRUE/FALSE----'+showProposalSent);
 57	   getOpt();
 58	   listOpp1 = [select Id,Description,name,Opportunity_Days__c,Opportunity_Duration__c,
 59	   RFQ__c,RFQ_Days__c,RFQ_Duration__c,Proposal_Sent_Days__c,Proposal_Sent_Duration__c,
 60	   Negotiation_Duration__c,Negotiation_review__c,Closed_Lost__c,Closed_Lost_Duration__c,
 61	   Opportunity_Scrapped_Dyas__c,Opportunity_Scrapped_Duration__c,
 62	   Closed_Won__c,Closed_Won_Duration__c,On_Hold_Days__c,On_Hold_Duration__c
 63	   from opportunity
 64	   where OwnerId =: strSelectedUser AND stagename =: strSelectedStage];
 65	   system.debug('----ANITHA----------Oppartunity Onject Values-------------'+listOpp1);
 66	   }
 67	   public List<Opportunity> getOpt()
 68	   {
 69	   listopp1 = [select Id,Description,name,Opportunity_Days__c,RFQ_Days__c,Proposal_Sent_Days__c, Negotiation_review__c,Closed_Lost__c, Opportunity_Scrapped_Dyas__c, Closed_Won__c,On_Hold_Days__c from opportunity where OwnerId =: selectedUser AND stagename =: selectedopportunity];
 70	   if(selectedopportunity == 'Opportunity')
 71	   {
 72	   showOpportunity = true;
 73	   showRFQ = false;
 74	   showProposalSent = false;
 75	   showNegotiation = false;
 76	   showClosedLost= false;
 77	   showOpportunityScrapped= false;
 78	   showOnHold= false;
 79	   showClosedWon = false;
 80	   }
 81	   else if (selectedopportunity == 'RFQ')
 82	   {
 83	   showRFQ = true;
 84	   showOpportunity = false;
 85	   showProposalSent = false;
 86	   showNegotiation = false;
 87	   showClosedLost= false;
 88	   showOpportunityScrapped= false;
 89	   showOnHold= false;
 90	   showClosedWon = false;
 91	   }
 92	   else if (selectedopportunity == 'Proposal Sent')
 93	   {
 94	   showProposalSent = true;
 95	   showOpportunity = false;
 96	   showRFQ = false;
 97	   showNegotiation = false;
 98	   showClosedLost= false;
 99	   showOpportunityScrapped= false;
 100	   showOnHold= false;
 101	   showClosedWon = false;
 102	   }
 103	   else if (selectedopportunity == 'Negotiation/Review')
 104	   {
 105	   showNegotiation = true;
 106	   showProposalSent = false;
 107	   showOpportunity = false;
 108	   showRFQ = false;
 109	   showClosedLost= false;
 110	   showOpportunityScrapped= false;
 111	   showOnHold= false;
 112	   showClosedWon = false;
 113	   }
 114	   else if (selectedopportunity == 'Closed Lost')
 115	   {
 116	   showClosedLost = true;
 117	   showNegotiation = false;
 118	   showProposalSent = false;
 119	   showOpportunity = false;
 120	   showRFQ = false;
 121	   showOpportunityScrapped= false;
 122	   showOnHold= false;
 123	   showClosedWon = false;
 124	   }
 125	   else if (selectedopportunity == 'Opportunity Scrapped')
 126	   {
 127	   showOpportunityScrapped = true;
 128	   showClosedLost = false;
 129	   showNegotiation = false;
 130	   showProposalSent = false;
 131	   showOpportunity = false;
 132	   showRFQ = false;
 133	   showOnHold= false;
 134	   showClosedWon = false;
 135	   }
 136	   else if (selectedopportunity == 'Closed Won')
 137	   {
 138	   showClosedWon = true;
 139	   showOpportunityScrapped = false;
 140	   showClosedLost = false;
 141	   showNegotiation = false;
 142	   showProposalSent = false;
 143	   showOpportunity = false;
 144	   showRFQ = false;
 145	   showOnHold= false;
 146	   }
 147	   else if (selectedopportunity == 'On Hold'){
 148	   showOnHold = true;
 149	   showClosedWon = false;
 150	   showOpportunityScrapped = false;
 151	   showClosedLost = false;
 152	   showNegotiation = false;
 153	   showProposalSent = false;
 154	   showOpportunity = false;
 155	   showRFQ = false;
 156	   }
 157	   return listOpp1;
 158	   }
 159	  
 160	  
 161	  
 162	   //*********************************** Test Method ********************************
 163	  
 164	  
 165	  
 166	  
 167	  static testmethod void tt(){
 168	   metric4 mm = new metric4();
 169	   mm.getopportunities();
 170	   mm.getUsers();
 171	   mm.submit();
 172	   List<Opportunity> opt = mm.getOpt();
 173	   mm.showOpportunity = True ;
 174	   mm.showRFQ = true;
 175	   mm.showProposalSent = false;
 176	   mm.showNegotiation = false;
 177	   mm.showClosedLost= false;
 178	   mm.showOpportunityScrapped= false;
 179	   mm.showOnHold= false;
 180	   mm.showClosedWon = false;
 181	  
 182	  
 183	   Account a = new Account ();
 184	   a.Name = 'Bodhtree';
 185	   a.CurrencyIsoCode = 'USD';
 186	   insert a ;
 187	   String showOpportunity = 'Opportunity' ;
 188	   Boolean showOpportunityb ;
 189	   Opportunity o = new opportunity();
 190	   o.name = 'Mahendra';
 191	   o.CloseDate = system.today();
 192	   o.AccountId = a.Id ;
 193	   o.stagename = 'Opportunity';
 194	   if(showOpportunity == 'Opportunity')
 195	   {
 196	  
 197	   showOpportunityb = True;
 198	   }
 199	   insert o;
 200	  
 201	   Opportunity o1 = new opportunity();
 202	   o1.stagename = 'RFQ';
 203	   o1.name = 'Mahendra';
 204	   o1.CloseDate = system.today();
 205	   o1.AccountId = a.Id ;
 206	   insert o1;
 207	  
 208	   System.currentPageReference().getParameters().put('tabName',o1.Id);

 

 

 

ReidCReidC

I would take a two pronged approach. 

 

First, simplify the code.  You have a lot of values that you are setting in every code block, and they could more easily all be set to "false" and then only set the single value to "true" that you need.  Make sense? 

 

Second, in order to test that the single value you need set to true is in fact true, you'll have to create matching data, run it through your method, and then System.assert() for the values you think you should be getting.

 

HTH