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
Behzad Bahadori 18Behzad Bahadori 18 

create a test case for PieWdgeData

I dont know why but this code works perfectly fine in Sandbox, but once I imported to production its says only 72 percent of it passes. I dont know how to write test cases 
 
public class ClosedLostStages {
    public List<Opportunity> Stages {get; private set;}
    public Integer counterLost {get;private set;}
    public Integer counterWon {get;private set;}
    public Integer counterQuoteSent {get;private set;}
    public Integer counterOpportunityIdentified {get;private set;}
    public Integer counterBizOrderFormSigned {get;private set;}
    public Integer counterRTS {get;private set;}
    public Decimal Sum {get;private set;}
    public Double perLost {get;private set;}
    public Double perWon {get;private set;} 

    public ClosedLostStages() {
        Stages = [Select Id, StageName, Amount, Name, Owner.Name
                  FROM Opportunity
                  WHERE   StageName = 'Closed Lost'];
        counterLost = [ Select count() 
                       FROM Opportunity
                       WHERE   StageName = 'Closed Lost' ];

        counterWon = [ Select count() 
                      FROM Opportunity
                      WHERE   StageName = 'Closed Won' ];

        counterQuoteSent = [ Select count() 
                      FROM Opportunity
                      WHERE   StageName = 'Quote Sent' ];

        counterOpportunityIdentified = [ Select count() 
                      FROM Opportunity
                      WHERE   StageName = 'Opportunity Identified' ];

        counterBizOrderFormSigned = [ Select count() 
                      FROM Opportunity
                      WHERE   StageName = 'Biz Order Form Signed' ];


        counterRTS = [ Select count() 
                      FROM Opportunity
                      WHERE   StageName = 'RTS' ];

        

        Sum = counterWon + counterLost ;
        perLost = ((counterLost / Sum) * 100) ;
        System.debug(perLost + 'Lost percentage');
        perWon = ((counterWon / Sum) * 100) ;  
        System.debug(perWon + 'Won percentage');
    }
     public List<PieWedgeData> getPieData() {
        List<PieWedgeData> data = new List<PieWedgeData>();
        data.add(new PieWedgeData('Lost ', counterLost));
        data.add(new PieWedgeData('Won', counterWon));
        data.add(new PieWedgeData('Quote Sent', counterQuoteSent));
        data.add(new PieWedgeData('Opportunity Identified', counterOpportunityIdentified ));
        data.add(new PieWedgeData('RTS', counterRTS));
        data.add(new PieWedgeData('Biz Order Form Signed', counterBizOrderFormSigned));
        return data;
    }
      public class PieWedgeData {

        public String name { get; private set; }
        public Integer data { get; private set; }

        public PieWedgeData(String name, Integer data) {
            this.name = name;
            this.data = data;
        }
    }

}

 
Rahul BorgaonkarRahul Borgaonkar
Hi,

Check this link to create sample test class for opportuniy.
http://salesforce.stackexchange.com/questions/36680/how-to-write-test-class-for-below-apex-class

Also you need to create a opportunity records for different counters you are getting using SOQL in constructor.
Then call function getPieData.
This should cover you code.

These are just logical steps mentioned here, if you find any error while writing please let me know and will help to sort it out.

Best Regars,

Rahul
Behzad Bahadori 18Behzad Bahadori 18
I made this class , is this wrong?
@isTest
private class CloseStageUnitTest {
	
	@isTest static void test_method_one() {
		// Implement test code
		List<string> testStringList = ClosedLostStages.ClosedLostStages();
		Opportunity CL = new Opportunity();
		o.StageName = 'Closed Lost'
		insert CL;
		Opportunity CW  = new Opportunity();
		o.StageName = 'Closed Won'
		insert CW;
		Opportunity OI  = new Opportunity();
		o.StageName = 'Opportunity Identified'
		insert OI;
		Opportunity QS  = new Opportunity();
		o.StageName = 'Quote Sent'
		insert QS;


		

	}
	
	@isTest static void test_method_two() {
		// Implement test code
	}
	
}

 
Rahul BorgaonkarRahul Borgaonkar
Hi,

I am not sure if your opportunity has any required fields. You need to populate those fields similar to name field. Please find code below. I have not tested it.There may be errors. Let me know how it goes. Also if you could paste Visualforce page related to this class I can help you more.

You will see this class when you go to develop > App Test Execution.

Execute CloseStageUnitTest class from App Test Execution to get see code coverage and success.
@isTest
private class CloseStageUnitTest 
{
	     
	@isTest static void test_method_one() 
	{
	        // Implement test code
	        List<string> testStringList = ClosedLostStages.ClosedLostStages();
	        Opportunity CL = new Opportunity(name = 'Opp1');
	        o.StageName = 'Closed Lost'
	        insert CL;
	        Opportunity CW = new Opportunity(name = 'Opp2');
	        o.StageName = 'Closed Won'
	        insert CW;
	        Opportunity OI = new Opportunity(name = 'Opp3');
	        o.StageName = 'Opportunity Identified'
	        insert OI;
	        Opportunity QS = new Opportunity(name = 'Opp4');
	        o.StageName = 'Quote Sent'
	        insert QS;

		ClosedLostStages cls = new ClosedLostStages ();
		cls.getPieData();
	}
}

Regards,

Rahul
Behzad Bahadori 18Behzad Bahadori 18

Hi  Rahul,

even with the code above, it only passes 35 percent. in the production it fails and says only 73 percent was passed. I dotn understand when the code gives me no error what is the issue. here is the VF page 

<apex:page showHeader="true" sidebar="true" controller="ClosedLostStages">
  <style type="text/css">
   .outBorder {
    border:3px outset black;
   }
   .inBorder{
    border-top:2px dotted blue;
    border-left:2px dotted blue;
   }
  </style>
  

  			<h1>Counter Lost</h1>
         <apex:outputText value="{!counterLost}"/>
         <br/>
         <h1>Counter won</h1>
         <apex:outputText value="{!counterWon}"/> 
   		<br/>
        <!--  <h1>Counter Sum </h1>
         <apex:outputText value="{!Sum}"/> 
         <br/>
         <h1>Lost percentage</h1>
         <apex:outputText value="{!perLost}"/>
         <br/>
         <h1>Won percentage</h1>
         <apex:outputText value="{!perWon}"/>  
         <br/> -->

          <apex:chart height="350" width="450" data="{!pieData}">
        <apex:pieSeries dataField="data" labelField="name"/>
        <apex:legend position="right"/>
    </apex:chart>
Behzad Bahadori 18Behzad Bahadori 18
test case 
ClosedLostStages c = new ClosedLostStages();
		Opportunity CL = new Opportunity();
		CL.StageName = 'Closed Lost';
		CL.Name = 'Test1';
		CL.CloseDate = system.today();
		
		insert CL;
		Opportunity CW  = new Opportunity();
		CW.StageName = 'Closed Won';
		CW.Name = 'Test2';
		CW.CloseDate = system.today();
		
		insert CW;
		Opportunity OI  = new Opportunity();
		OI.StageName = 'Opportunity Identified';
		OI.Name = 'Test3';
		OI.CloseDate = system.today();
		
		insert OI;
		Opportunity QS  = new Opportunity();
		QS.StageName = 'Quote Sent';
		QS.Name = 'Test4';
		QS.CloseDate = system.today();
		
		insert QS;

		c.getPieData();
Rahul BorgaonkarRahul Borgaonkar
Have used system.debug to check how the flow of code is moving.

Please add system.debug after each statement to check it.
example
 
Opportunity CL = new Opportunity();
        CL.StageName = 'Closed Lost';
	 CL.Name = 'Test1';
	 CL.CloseDate = system.today();
	 insert CL;
        system.debug('Opportunity CL - ' + CL); // example debug

	Opportunity CW = new Opportunity();
	        CW.StageName = 'Closed Won';
	        CW.Name = 'Test2';
	        CW.CloseDate = system.today();
	        insert CW;

	        Opportunity OI = new Opportunity();
	        OI.StageName = 'Opportunity Identified';
	        OI.Name = 'Test3';
	        OI.CloseDate = system.today();
	        insert OI;

	        Opportunity QS = new Opportunity();
	        QS.StageName = 'Quote Sent';
	        QS.Name = 'Test4';
	        QS.CloseDate = system.today();
	        insert QS;

// moved this statement here as first you should insert recs and the use it in class
               ClosedLostStages c = new ClosedLostStages();
	        c.getPieData();

Let me know how it goes.

Thanks
Behzad Bahadori 18Behzad Bahadori 18
How do you run the system debug on this thing? I never see any results from this test class
Behzad Bahadori 18Behzad Bahadori 18
do you think this is wrong , that the test case doesnt cover it?
public List<Opportunity> Stages {get; private set;}
    public Integer counterLost {get;private set;}
    public Integer counterWon {get;private set;}
    public Integer counterQuoteSent {get;private set;}
    public Integer counterOpportunityIdentified {get;private set;}
    public Integer counterBizOrderFormSigned {get;private set;}
    public Integer counterRTS {get;private set;}
    public Decimal Sum {get;private set;}
    public Double perLost {get;private set;}
    public Double perWon {get;private set;} 

    public ClosedLostStages() {
        Stages = [Select Id, StageName, Amount, Name, Owner.Name
                  FROM Opportunity
                  WHERE   StageName = 'Closed Lost'];
        counterLost = [ Select count() 
                       FROM Opportunity
                       WHERE   StageName = 'Closed Lost' ];

        counterWon = [ Select count() 
                      FROM Opportunity
                      WHERE   StageName = 'Closed Won' ];

        counterQuoteSent = [ Select count() 
                      FROM Opportunity
                      WHERE   StageName = 'Quote Sent' ];

        counterOpportunityIdentified = [ Select count() 
                      FROM Opportunity
                      WHERE   StageName = 'Opportunity Identified' ];

        counterBizOrderFormSigned = [ Select count() 
                      FROM Opportunity
                      WHERE   StageName = 'Biz Order Form Signed' ];


        counterRTS = [ Select count() 
                      FROM Opportunity
                      WHERE   StageName = 'RTS' ];

        

        Sum = counterWon + counterLost ;
       perLost = ((counterLost / Sum) * 100) ;
        System.debug(perLost + 'Lost percentage');
        perWon = ((counterWon / Sum) * 100) ;  
        System.debug(perWon + 'Won percentage');
    }
     public List<PieWedgeData> getPieData() {
        List<PieWedgeData> data = new List<PieWedgeData>();
        data.add(new PieWedgeData('Lost ', counterLost));
        data.add(new PieWedgeData('Won', counterWon));
        data.add(new PieWedgeData('Quote Sent', counterQuoteSent));
        data.add(new PieWedgeData('Opportunity Identified', counterOpportunityIdentified ));
        data.add(new PieWedgeData('RTS', counterRTS));
        data.add(new PieWedgeData('Biz Order Form Signed', counterBizOrderFormSigned));
        
        return data;
    }
      public class PieWedgeData {

        public String name { get;  set; }
        public Integer data { get; set; }

        public PieWedgeData(String name, Integer data) {
            this.name = name;
            this.data = data;
        }
    }
Rahul BorgaonkarRahul Borgaonkar
Hi,

Check this article to monitor debug logs
https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_debugging_debug_log.htm
This will help you how to debug your code.

Regards
 
Behzad Bahadori 18Behzad Bahadori 18
There is nothing wrong wtiht he code, I just dont know why I cant deploy it.. it is perfectly working fine in sandbox