I can run this code in Sandbox, and I see the chart with all the results, how ever when I deploy it , it says fail to upload into production since 72 percent code coverage. why does this gives me no error in sandbox but it does in production. How do I write a test case for coverage.

 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;

Test case

private class CloseStageUnitTest {

    @isTest static void test_method_one() {
        // Implement test code
        ClosedLostStages c = new ClosedLostStages();
        Opportunity CL = new Opportunity();
        CL.StageName = 'Closed Lost';
        insert CL;
        Opportunity CW  = new Opportunity();
        CW.StageName = 'Closed Won';
        insert CW;
        Opportunity OI  = new Opportunity();
        OI.StageName = 'Opportunity Identified';
        insert OI;
        Opportunity QS  = new Opportunity();
        QS.StageName = 'Quote Sent';
        insert QS;



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


1 Answer 1


your overall code coverage in production might be less, so increase the code coverage and then deploy. While deploying all your test classes will run and it will fail if the over all code coverage is less than 75%, even though the current class you are deploying is having above 75% code coverage.

Here is the link you can gain knowledge on test case coverage.


  • Hi, Thank you so much for the replay. I updated the question with my test case, could you take a look at it? how does it know which class does my test case belongs too?
    – Metawaa
    Apr 10, 2015 at 16:54
  • If this is a trigger it will invoke when you run your test class with proper test data and if it is a class create a instance of that class in the test method inbetween the test.start() and test.stop(). Add some system.assert() statements to check weather the expected results comming or not. if it fails you should rethink about it.
    – Suresh
    Apr 10, 2015 at 18:44
  • this is a test class that I want to have it for that class public class ClosedLostStages {
    – Metawaa
    Apr 10, 2015 at 18:50
  • how do you insert date CL.CloseDate = '4/21/2015';
    – Metawaa
    Apr 10, 2015 at 19:00
  • I wrote all of it but I can get the GetPieData() to work :(
    – Metawaa
    Apr 10, 2015 at 23:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .