+ Start a Discussion
uuuu 

Help me to write test class for following code.

Hii Developers,
Please help to write test class for this part of code.
code
Income_Tax__c 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=tax.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;
            }
        }
        
        If(ITax.Employees_PF__c!=null){
            Attachment a3 = new Attachment(parentid=tax.id, Name = 'Employees Provident Fund.jpeg' , Body = myfile3.body);
            try {insert a3;}
            catch(Exception ex){
                ApexPages.addMessages(ex);
                return null;
            }
        }
        
        If(ITax.Children_s_Education_Fee__c!=null){
            Attachment a4 = new Attachment(parentid=tax.id, Name = 'Children Education Fee.jpeg' , Body = myfile4.body);
            try {insert a4;}
            catch(Exception ex){
                ApexPages.addMessages(ex);
                return null;
            }
        }

Please help me
Thank you.
Herish SurendranHerish Surendran
Hi buddy. You have to write one testmethod and one insert statement on Income_Tax__c object for each If statement in your class.

Like you can write test class for the following If statement 
If(ITax.Rent_paid_to_landlord__c!=null){
            Attachment a = new Attachment(parentid=tax.id, Name = 'Rent paid to landlord.jpeg' , Body = myfile.body);
            try {insert a;}
            catch(Exception ex){
                ApexPages.addMessages(ex);
                return null;
            }
        }

as follow 
Lets say class to be tested is Tax Class and the test class for this is TestTaxClass
 
isTest 
private class TestTaxClass{
    static testMethod void rentPaidToLord() {
      Income_Tax__c rentPaidToLordTax = new Income_Tax__c();
	  rentPaidToLordTax.Rent_paid_to_landlord__c = 12345;
	  // give values to any other required fields like rentPaidToLordTax.field2 = 'value';
	  Insert rentPaidToLordTax;
	  TaxClass tc = new TaxClass();
	  tc.methodName();
	  Attachment at = [SELECT id FROM Attachment WHERE parentid=rentPaidToLordTax.id AND Name = 'Rent paid to landlord.jpeg'];
	  System.assert(at.size() == 1);
	   
  }
}

Like this write test class for all If condition.

If this solves you query. mark this as solved.​​​​​​ 
Herish SurendranHerish Surendran
Use @isTest annotation instead of isTest before the TestTaxClass
uuuu
Hello Herish,

Thank you for ur reply.

I have written below code but still not working.
Please tell me what's wrong with this code.
static testMethod void rentPaidToLord() {
        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;

        Income_Tax__c rentPaidToLordTax = new Income_Tax__c();
        rentPaidToLordTax.Rent_paid_to_landlord__c = 12345;
        rentPaidToLordTax.Resource__c=res.Id;
        
        // give values to any other required fields like rentPaidToLordTax.field2 = 'value';
        Insert rentPaidToLordTax;
        
       // Controller class name
        IncomeTaxPageController  tc = new IncomeTaxPageController();

      // calling save method which holds above if condition code in controller
        tc.save();

        Attachment at = [SELECT id FROM Attachment WHERE parentid=:rentPaidToLordTax.id AND Name = :'Rent paid to landlord.jpeg'];
        //System.assert(at.size() == 1);
    }

Thank you.