You need to sign in to do that
Don't have an account?
divesh khanduja
How to cover Upsert in test class as It is giving me an error Attempted to upsert a null list in Test Class ?
Hi
I am using Standard controller (Case ) with extesion controller I ma upsertingthe case record in amy controller ;
public with sharing Myclass{
//Constructor
public myController(ApexPages.StandardController stdController){
try{
this.caseId= ApexPages.currentPage().getParameters().get('id');
if(caseId != null)
oCase = [select Id, CaseNumber,AccountId, Owner.Name, Status, Priority, Origin, Contact.Name, Account.Name, Type, Reason, Subject, Description,(Select CommentBody from CaseComments order by createddate limit 1) from Case where Id = :caseId];
}
catch(Exception e){
ExceptionLogger.addException(e,'myController','Constructor');
}
}
public PageReference save(){
upsert oCase;
PageReference pg = new PageReference('/apex/Mypage');
pg.getParameters().put('id',oCase.Id);
flag1=true;
flag2= false;
return pg;
}
When I try to cover /run the test class on save() in Test Class , after inserting Case it give me an error
Attempted to upsert a null list
//Test Class Code :
Insert CaseList[with data ];
System.debug('showCas caseList= '+caseList); //Here i can see the case list
ApexPages.Standardcontroller sc = new ApexPages.Standardcontroller(caseList[0]);
myController controller = new myController(sc);
controller.save() // Attempted to upsert a null list
Please help me ! Really needed ,
Thanks in Advance !
I am using Standard controller (Case ) with extesion controller I ma upsertingthe case record in amy controller ;
public with sharing Myclass{
//Constructor
public myController(ApexPages.StandardController stdController){
try{
this.caseId= ApexPages.currentPage().getParameters().get('id');
if(caseId != null)
oCase = [select Id, CaseNumber,AccountId, Owner.Name, Status, Priority, Origin, Contact.Name, Account.Name, Type, Reason, Subject, Description,(Select CommentBody from CaseComments order by createddate limit 1) from Case where Id = :caseId];
}
catch(Exception e){
ExceptionLogger.addException(e,'myController','Constructor');
}
}
public PageReference save(){
upsert oCase;
PageReference pg = new PageReference('/apex/Mypage');
pg.getParameters().put('id',oCase.Id);
flag1=true;
flag2= false;
return pg;
}
When I try to cover /run the test class on save() in Test Class , after inserting Case it give me an error
Attempted to upsert a null list
//Test Class Code :
Insert CaseList[with data ];
System.debug('showCas caseList= '+caseList); //Here i can see the case list
ApexPages.Standardcontroller sc = new ApexPages.Standardcontroller(caseList[0]);
myController controller = new myController(sc);
controller.save() // Attempted to upsert a null list
Please help me ! Really needed ,
Thanks in Advance !
Couple thoughts:
- If oCase is a List<Case>, declare it as "List<Case> oCase = new List<Case>();".
- Since your code only overrides the default value of oCase if caseId != null, it either will be blank list or one for the valid caseId. In either scenario, you can change your upsert to "if (!oCase.isEmpty()) upsert oCase;".
- If you are using caseId, you likely only expect one Case. On that hand, you can define "Case oCase;" and use LIMIT 1 in your select to make sure you get a single Case object. In your upsert, you would use "if (oCase != null) upsert oCase;".
I hope that helps.
Thanks for reply , I have updated my Save() in controller with if (oCase != null) as you suggested now I am geeting error an System.NullPointerException: Attempt to de-reference a null object in my Test Class
I have already declare List <case> in my test class and in my controller .
Fucntionltiy is working fine only problem is my Test Class is falling ! with error System.NullPointerException: Attempt to de-reference a null when I call Save() in my Test class however ,I have already inserted the case in ,my test class before calling Save
public PageReference save(){
"if (oCase != null){
upsert oCase;
}
PageReference pg = new PageReference('/apex/Mypage');
pg.getParameters().put('id',oCase.Id);
flag1=true;
flag2= false;
return pg;
}
Please advice !
Thanks
If oCase is NULL, this also will fail. Therefore, you have to adjust for null case there as well.
My issue is resovled by passing
controller .oCase .caselist[] in test calss
Thanks for your help !