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
rlupurlupu 

Cannot save a class because the test coverage is less than 75%

I have to write some code for a Salesforce Enterprise edition that doesn’t allow me to create/modify/drop triggers or Apex classes by using the interface.

I created in Eclipse the class which code is written below. When attempting to save it, I get this message:'Test coverage of selected Apex Class and Trigger is 46%, at least 75% test coverage is required'. I know I’m missing something here, but I cannot realize what.

Can someone help please? Thank you.

global class test_records

{

    WebService static String create_test_records()

    {

        String mystr;

        try

       {

           Account a=new Account(name='test_acc');

           insert a;

           ID a_id=[select id from account where name='test_acc'].id;

           Purchases__c p=new Purchases__c(accountid__c=a_id);

           insert p;

           ID p_id=[select id from purchases__c where accountid__c=:a_id].id;

           PurchaseItems__c i1=new PurchaseItems__c(purchaseid__c=p_id, package__c='Item A', price__c=10, quantity__c=1,

                                                                               Barcode_Email_Status__c='Not Sent',

                                                                               Barcode_Email_Recipient__c='x@y.com');

           insert i1;

           PurchaseItems__c i2=new PurchaseItems__c(purchaseid__c=p_id, package__c='Item B', price__c=20, quantity__c=2,

                                                                              Barcode_Email_Status__c='Not Sent',

                                                                              Barcode_Email_Recipient__c='x@y.com');

            insert i2;

            mystr='done';

       }

       catch (System.DMLException e)

       {

          mystr='dml error';

       }

       return mystr;

    }

    static testMethod void test_create_test_records()

    {

        System.assertEquals('done',create_test_records());

    }

}

paul-lmipaul-lmi

static testMethod void test_create_test_records()

    {

        System.assertEquals('done',create_test_records());

    }



can be


static testMethod void test_create_test_records()

    {

        create_test_records();

    }


however, to properly cover the entire class, you need to write test methods that can trigger every line of code, which neither your or my example do properly.  just an example though.

rlupurlupu

Thank you, Paul

You must be right.

However, I'm a bit annoyed of having to write a test method as rich in code as the other method of the class. I wonder why things in Salesforce have been designed like that.