sales force中编写测试类

salesforce中定义Apex测试类和测试方法:

首先放一个定义的例子:

@isTest
private class myClass {
     static testMethod void myTest() {
        // code_block
    }
}

》》》》Here below is the same test class as in the previous example but it defines the test method with the @isTest annotation instead.《《《《《

@isTest
private class myClass {
    @isTest static void myTest() {
        // code_block
    }
}

注意:The testMethod keyword is now deprecated. Use the @isTest annotation on classes and methods instead.所以我们现在都是用注解@isTest来定义测试方法。

多个测试方法定义方法不改变
This is an example of a test class that contains two test methods.

@isTest
private class MyTestClass {
   // Methods for testing
   @isTest static void test1() {
      // Implement test code
   }
   @isTest static void test2() {
      // Implement test code
   }
}

下面来看一套完整的测试代码:
EXAMPLE:
This example shows a class and its corresponding test class. This is the class to be tested. It contains two methods and a constructor.

public class TVRemoteControl {
    // Volume to be modified
    Integer volume;
    // Constant for maximum volume value
    static final Integer MAX_VOLUME = 50;   
    // Constructor
    public TVRemoteControl(Integer v) {
        // Set initial value for volume
        volume = v;
    }
    public Integer increaseVolume(Integer amount) {
        volume += amount;
        if (volume > MAX_VOLUME) {
            volume = MAX_VOLUME;
        }
        return volume;
    }
    public Integer decreaseVolume(Integer amount) {
        volume -= amount;
        if (volume < 0) {
            volume = 0;
        } 
        return volume;
    }   
    public static String getMenuOptions() {
        return 'AUDIO SETTINGS - VIDEO SETTINGS';
    }
}

This is the corresponding test class. It contains four test methods. Each method in the previous class is called. Although this would have been enough for test coverage, the test methods in the test class perform additional testing to verify boundary conditions.

@isTest
class TVRemoteControlTest {
    @isTest static void testVolumeIncrease() {
        TVRemoteControl rc = new TVRemoteControl(10);
        Integer newVolume = rc.increaseVolume(15);
        System.assertEquals(25, newVolume);
    }
    
    @isTest static void testVolumeDecrease() {
        TVRemoteControl rc = new TVRemoteControl(20);
        Integer newVolume = rc.decreaseVolume(15);
        System.assertEquals(5, newVolume);        
    } 
        
    @isTest static void testVolumeIncreaseOverMax() {
        TVRemoteControl rc = new TVRemoteControl(10);
        Integer newVolume = rc.increaseVolume(100);
        System.assertEquals(50, newVolume);        
    }
    
    @isTest static void testVolumeDecreaseUnderMin() {
        TVRemoteControl rc = new TVRemoteControl(10);
        Integer newVolume = rc.decreaseVolume(100);
        System.assertEquals(0, newVolume);        
    }
    
    @isTest static void testGetMenuOptions() {
        // Static method call. No need to create a class instance.
        String menu = TVRemoteControl.getMenuOptions();
        System.assertNotEquals(null, menu);
        System.assertNotEquals('', menu);
    }
}

单元测试的注意事项:

  • 从Salesforce API 28.0开始,测试方法不能再驻留在非测试类中,并且必须是带注释的类的一部分 isTest。
  • 测试方法不能用于测试Web服务标注。相反,使用模拟标注。请参阅测试Web服务标注和测试HTTP标注。
  • 您无法从测试方法发送电子邮件。
  • 由于测试方法不提交在测试中创建的数据,因此您不必在完成时删除测试数据。
  • 如果测试类包含静态成员变量,并且在testSetup或test方法中更改了变量的值,则不会保留新值。此类中的其他测试方法获取静态成员变量的原始值。当静态成员变量在另一个类中定义并在测试方法中访问时,此行为也适用。
  • 对于某些具有唯一约束的字段的sObject,插入重复的sObject记录会导致错误。例如,插入具有相同名称的CollaborationGroup sObject会导致错误,因为CollaborationGroup记录必须具有唯一的名称。
  • 当测试方法修改关联记录时,Chatter源中记录的跟踪更改(FeedTrackedChange记录)不可用。FeedTrackedChange记录要求更改与其关联的父记录,以便在创建之前将其提交到数据库。由于测试方法不提交数据,因此不会导致创建FeedTrackedChange记录。同样,无法在测试方法中创建字段历史记录跟踪记录(如AccountHistory),因为它们需要首先提交其他sObject记录(例如,Account)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值