如何编写testclass

时间:2011-03-21 09:30:28

标签: salesforce

如何为这个类编写测试类。

公共类InventoryDe​​tails {

 /* Constructor does not do anything */

public InventoryDetails(ApexPages.StandardController controller) {

}

/* The method getInventory returns an array of inventory objects that meet certain criteria */

public Inventory__c[] getInventoryDetails() {

    Inventory__c [] inventoryList;

    inventoryList = [select Inventory__c.Rooms_Available__c, Inventory__c.Room_Type__c from Inventory__c];

    return inventoryList;

}

} 如果有人知道请告诉我这个问题。

2 个答案:

答案 0 :(得分:2)

创建单独的类以包含测试方法的一个优点是您可以使用@isTest annotation标记它。

使用@isTest注释定义的类不计入所有Apex脚本的组织大小限制。

答案 1 :(得分:0)

你不需要编写一个特殊的类来进行测试(尽管你可以),为了简单和减少混乱,我们使用一种特殊的静态测试方法将测试保存在同一个类中:

public with sharing class InventoryDetails {
    public InventoryDetails(ApexPages.StandardController controller) {}

    public Inventory__c[] getInventoryDetails() {
        Inventory__c [] inventoryList;
        inventoryList = [select Inventory__c.Rooms_Available__c, Inventory__c.Room_Type__c from Inventory__c];
        return inventoryList;
    }

    // *********************************************************
    // TESTS 
    static testMethod void test_InventoryDetails() {
        // replace Object123 with entity name for the 
        // object for which this extension is built
        // don;t worry about insert o, SF rollbacks all test DMLs
        Object123 o = new Object123();
        insert o;
        ApexPages.StandardController ctrl = new ApexPages.StandardController(o);

        InventoryDetails i = new InventoryDetails(o);
        List<Inventory__c> invs = i.getInventoryDetails();
        // do asserts and test and whatever needs to be tested
    }
}