Salesforce 测试 Class - System.NullPointerException:尝试取消引用空对象

Salesforce Test Class - System.NullPointerException: Attempt to de-reference a null object

Salesforce 测试 Class - 面对 - System.NullPointerException:尝试取消引用空对象时出错 运行 测试 Class

顶点class

public class NewAccountCreation {
public Account account{get;set;}
public void save(){
Account acc = new Account();
// User enter values in vf page and we are capturing and creating account
acc.name = account.Name;
//acc.address = account.adress;
Insert acc;
}
}

测试Class

@isTest
public class TestNewAccountCreation {
    @isTest static void TestNewAccountCreationMethod(){
        NewAccountCreation testAccount = new NewAccountCreation ();
        Account acc = new Account(Name='TestAcct');
        Insert acc;
        system.debug(''+acc);
        testAccount.save();
        System.assert([Select Id From Account].size()==1);
    }
}

错误: System.NullPointerException: 尝试取消引用空对象

堆栈跟踪: Class.NewAccountCreation.save:第 6 行,第 1 列 Class.TestNewAccountCreation.TestNewAccountCreationMethod:第 9 行,第 1 列


您从未设置 testAccount.account,所以它是 null。您 需要填充此变量,而您不需要 在测试代码中创建和插入 Account。事实上,这样做会导致你的断言失败。