需要帮助将测试覆盖率提高到75% - Apex Class

时间:2013-10-04 05:24:36

标签: salesforce package apex-code

我无法上传我的软件包,因为测试覆盖率低于75%。 如何提高覆盖率。如何编写触发器的测试方法。 请帮忙。

我试过这段代码: 这是我的控制器类:

    public class MyController{
         public List<Merchandise__c> merch{get;set;}
         public Merchandise__c toDelete{get;set;}


         public MyController(){


 merch = [SELECT ID,Merchandise__c.name,Price__c,Description__c,Total_Inventory__c FROM Merchandise__c];
    }

    public pageReference del(){
        string id = Apexpages.currentpage().getParameters().get('merchandiseId');
        toDelete = [SELECT Id,Merchandise__c.name,Price__c,Description__c,Total_Inventory__c FROM Merchandise__c WHERE Id =: id];     
        delete toDelete;
        return null;
    }

     public pageReference add(){
       Merchandise__c  item = new Merchandise__c(
           Name = 'Enter Name',
           Description__c = 'Enter Description',
           Price__c = 0.00,
           Total_Inventory__c = 0
       );
        merch.add(item);
        insert item;
        return null;
    }

    public PageReference save(){   
        string id = Apexpages.currentpage().getParameters().get('merchandiseId');        
        Merchandise__c toUpdate = [SELECT Name, Description__c, Price__c FROM Merchandise__c WHERE ID =: id];          
        update toUpdate;  
        return null;
      }

}

我曾经试过这个测试类:

 @isTest

Public Class demoTest{
     static testMethod void checkDatatableData() {         
          List<Merchandise__c> merch;

          Test.startTest();
          merch = [SELECT ID,Merchandise__c.name,Price__c,Description__c,Total_Inventory__c FROM Merchandise__c];
          Test.stopTest();
   }


    static testMethod void checkAdd() {

           Merchandise__c  item = new Merchandise__c(
           Name = 'Enter Name',
           Description__c = 'Enter Description',
           Price__c = 0.00,
           Total_Inventory__c = 0
        );

            Test.startTest(); 
            insert item;
            System.assertNotEquals(null,item.Id);
            List<Merchandise__c> merchItem = [SELECT Id FROM Merchandise__c WHERE Id =: item.Id];
            System.assertEquals(1,merchItem.size());
            Test.stopTest();
      }   


      static testMethod void checkUpdateDelete() { 

               Merchandise__c  testmerch = new Merchandise__c(
               Name = 'NewProduct',
               Description__c = 'this is anew product',
               Price__c = 20.00,
               Total_Inventory__c = 100
               );

              Test.startTest(); 
              insert testmerch;
              system.assertNotEquals(null,testmerch.Id);
              testmerch.Price__c = 40.00;
              update testmerch;

              Merchandise__c updatedmerch = [SELECT Id,Price__c FROM Merchandise__c WHERE Id =: testmerch.Id];          
              system.assertEquals(40.00,updatedmerch.Price__c);

              Merchandise__c  toDelete = [SELECT ID FROM Merchandise__c WHERE Id =: testmerch.Id];
              system.assertNotEquals(null,toDelete.Id);
              delete toDelete;
              Test.stopTest(); 
         }    

  }

您能否告诉我需要做些哪些改进才能提高覆盖率?

1 个答案:

答案 0 :(得分:1)

此链接应告诉您需要了解的内容。

http://wiki.developerforce.com/page/An_Introduction_to_Apex_Code_Test_Methods

基本上你需要在测试方法中复制触发器的功能,例如,如果你有一个在从Open变为Closed时运行的After update触发器,你首先需要为你的触发器创建一个测试方法然后在测试方法中插入用于测试的案例记录,这可能还需要联系人或帐户等,然后您将案例的值更改为关闭,然后运行更新方法。这非常简单,但这就是主意。因此,在编写测试类时,请尝试考虑触发器和类的作用,然后尝试自动化用户在测试方法中执行的操作,确保覆盖所有基础,包括顶点应该触发异常的位置。

尽量避免使用硬编码的Id。

这些链接也可以提供帮助

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_testing_best_practices.htm http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_qs_test.htm