function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Hiral Shah 16Hiral Shah 16 

Hello friendss....Please help me out to write test class...

Hii Experts,
Please help me to write apex test class...
Apex class is here...
public Pagereference save(){
       system.debug('2');
       tax.Resource__c=user[0].id;
       try{
           insert tax;
           //statusM = true; 
           ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO,'Thank you!Record saved'));
           
       }
       catch(Exception ex){
           ApexPages.addMessages(ex);
           return null;
       }
       
       ITax=[select id,Rent_paid_to_landlord__c,Leave_travel_concessions_or_assistance__c,Employees_PF__c,
                           Interest_payable_paid_to_lender__c,Children_s_Education_Fee__c,PPF__c,Insurance_Premium__c,
                           NSC__c,Deduction_under_Pension_scheme__c,Housing_loan_principal_repayment__c,ELSS_Mutual_Fund__c,
                           NPS__c,Payment_for_Medical_Insurance_Premium__c,Medical_treatment_for_handicapped__c,
                           Medical_for_specified_diseases__c,Education_Loan_Interest_Repayment__c,Interest_on_loan_taken_for_house__c,
                           Donation__c,Rent_deduction_only_if_HRA_not_received__c,Saving_interest__c,Rebate_of_Rs_2000__c,RGESS__c,
                           Royalty__c,Professional_Development_Allowance__c,Car_Maintenance_Allowance__c,
                           Conveyance_Allowance__c,Helper_Allowance__c,Telephone_Internet_allowance__c,
                           Tax_by_previous_employer__c,TDS_already_deducted_by_Current_Company__c,Donation_to_Other__c
                           from Income_Tax__c where id=:tax.id];
       If(ITax.Rent_paid_to_landlord__c!=null){
           Attachment a = new Attachment(parentid=ITax.id, Name = 'Rent paid to landlord.jpeg' , Body = myfile.body);
           try {insert a;}
           catch(Exception ex){
               ApexPages.addMessages(ex);
               return null;
           }
       }
       
       If(ITax.Leave_travel_concessions_or_assistance__c!=null){
           Attachment a1 = new Attachment(parentid=tax.id, Name = 'Leave travel concessions or assistance.jpeg', Body = myfile1.body);
           try {insert a1;}
           catch(Exception ex){
               ApexPages.addMessages(ex);
               return null;
           }
       }
       
       If(ITax.Interest_payable_paid_to_lender__c!=null){
           Attachment a2 = new Attachment(parentid=tax.id, Name = 'Interest payable paid to lender.jpeg' , Body = myfile2.body);
           try {insert a2;}
           catch(Exception ex){
               ApexPages.addMessages(ex);
               return null;
           }
       }


The code which i have written not covering if condition please help me.
@isTest
   public static void rentPaidToLord() {
      
       system.debug('hii=====');
       Resource__c res=new Resource__c();
       res.Name='Test';
       res.Emp_ID__c=201;
       res.Joining_Date__c=Date.newInstance(2019,1,1);
       insert res;
       system.debug('====res===='+res);
       
       Income_Tax__c rentPaidToLordTax = new Income_Tax__c();
       rentPaidToLordTax.Rent_paid_to_landlord__c = 12345;
       rentPaidToLordTax.Resource__c=res.Id;
       rentPaidToLordTax.Leave_travel_concessions_or_assistance__c=600;
       Insert rentPaidToLordTax;
       
       system.debug('++++++dcbsd++++++'+rentPaidToLordTax);
       system.debug('=======rent======'+rentPaidToLordTax.Rent_paid_to_landlord__c);
       test.startTest();
       PageReference pageRef = Page.IncomeTaxPage;
       Test.setCurrentPage(pageRef);
       pageRef.getParameters().put('name',res.name);
       ApexPages.StandardController sc = new ApexPages.standardController(rentPaidToLordTax);
       Taxclass tc = new Taxclass (sc);
       system.debug('success==============');
       tc.save();
       List<Attachment> at = [SELECT id FROM Attachment WHERE parentid=:rentPaidToLordTax.id AND Name = :'Rent paid to landlord.jpeg'];
       //System.assert(at.isEmpty()!=null);
       system.assert(Apexpages.hasMessages());

       test.stopTest();
   }

Thank you
Regards,
Hiral
AbhishekAbhishek (Salesforce Developers) 
Code coverage is a measurement of how many unique lines of your code are executed while the automated tests are running. Code coverage percentage is the number of covered lines divided by the sum of the number of covered lines and uncovered lines.

For purposes of calculating code coverage, only executable lines of code are counted, whether covered or uncovered. Comments and blank lines are not counted, and System.debug() statements and curly braces that appear alone on one line are also not counted.

While writing unit tests, the main emphasis should be given on actually testing the logic and behavior of the block by designing input data to ensure the code path executes and writing assertions to make sure the code's behavior is as expected. Code coverage is a side effect of this testing process.
Fundamentally, to increase your code coverage, you must write functional unit tests for code paths that are not currently covered. For more community resources on writing high-quality unit tests

Go through the below blog which will guide you how to write the test class easily,

https://salesforce.stackexchange.com/questions/244794/how-do-i-increase-my-code-coverage-or-why-cant-i-cover-these-lines

I hope you find the above information is helpful. If it does, please mark as Best Answer to help others too.

Thanks.