Salesforce测试类-System.NullPointerException:尝试取消引用null对象



Salesforce测试类-面向-System.NullPointerException:尝试取消引用null对象在尝试运行测试类时出错

APEX级

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;
}
}

测试等级

@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:尝试取消引用空对象

StackTrace:Class.NewAccountCreation.save:第6行第1列Class.TestNewAccountCreation.TestNewAccountCreationMethod:第9行,第1列


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

最新更新