+ Start a Discussion
ShreyankaShreyanka 

Test class for the below apex class

Hi everyone,
Please help me in writting test class for the below apex code:

public class xyz {

        public abc objabc{get;set;}
        public Boolean sendEmail {get; set;}
        public Task objtask{get;set;}
        public boolean isChecked{get;set;}
    public xyz() {
        
        
        String objabcId = ApexPages.currentPage().getParameters().get('id');
                
            objabc = new abc();
        
             if(objabcId !=null && objabcId !=''){
                 
                 objabc = [Select id,OwnerId,name from abc 
                            where id=:ApexPages.currentPage().getParameters().get('id')];
             }
              
            if(objabc.id!=null){
                sendEmail = true;
                objtask= new task();
                objtask.whatid=objabc.id;
                objtask.RecordTypeId = [select Id,Name from RecordType where name='abcd' and SOBjectType='Task' limit 1].Id;
                objtask.status = 'Completed';
                objtask.type = 'Assigned';
                objtask.OwnerID = UserInfo.getUserId();
                objtask.Subject='Assignment';
            }
    }
      
        public pagereference save() {
            try {
                if(objtask.subject!=null){
                    insert objtask;
                    update objabc;
                    if(sendEmail){                       
                        Database.DMLOptions dlo = new Database.DMLOptions();
                        dlo.EmailHeader.triggerUserEmail = true;
                        database.update(objabc,dlo);                        
                    }
                }else{
                    Apexpages.addMessage(new Apexpages.message(ApexPages.Severity.Error,'Subject cannot be Null !!!'));
                    return null;
                }
                string abcobjurl = objabc.id;
                abcobjurl = abcobjurl.substring(0,3);
                PageReference orderPage = new PageReference('/' + abcobjurl);
                return orderPage;
            } catch (Exception e) {
                system.debug('---inside Exception---'+e.getMessage());
                system.debug('---inside Exception Line---'+e.getLineNumber());
                Apexpages.addMessage(new Apexpages.message(ApexPages.Severity.Error,e.getMessage()));
                return null;
            }
        }
        
}


Thanks in advance!
Best Answer chosen by Shreyanka
CharuDuttCharuDutt
Hii Shreya
Try Below Test Class
@IsTest
Public class TestShareFilesWithCommunityUsers {
    @IsTest
    Public static void testmethod1(){
        Test.startTest();
        abc oAbc =new abc (Name='Demo');//Fill All Required Fields
        insert oAbc ; 
       ApexPages.currentPage().getParameters().put('id',oAbc .id);
       xyz abc= new xyz();
        abc.save();
        abc.objabc = oAbc;
        abc.sendEmail = true;
        Test.stopTest();
        
    }
}
Please Mark It As Best Answer If It Helps
Thank You!

All Answers

CharuDuttCharuDutt
Hii Shreya
Try Below Test Class
@IsTest
Public class TestShareFilesWithCommunityUsers {
    @IsTest
    Public static void testmethod1(){
        Test.startTest();
        abc oAbc =new abc (Name='Demo');//Fill All Required Fields
        insert oAbc ; 
       ApexPages.currentPage().getParameters().put('id',oAbc .id);
       xyz abc= new xyz();
        abc.save();
        abc.objabc = oAbc;
        abc.sendEmail = true;
        Test.stopTest();
        
    }
}
Please Mark It As Best Answer If It Helps
Thank You!
This was selected as the best answer
ShreyankaShreyanka
Hi CharuDutt,

The test class got worked. It gave code coverage 83%.

Thank you!!