+ Start a Discussion
iyappan kandasamy 4iyappan kandasamy 4 

For addition of 2 numbers want to know the test class is correct or not because my code coverage is only 0% .....

Actually this is my apex class for addition of 2 numbers 
public class Addition         //define class
{
  public integer a=5;        //define variables
  public integer b=4;
  public integer c;

  public integer add()               //define function or method
  {
  c=a+b;
  system.debug('the result is '+c);
        return c;
  }
}// end of class


My Test class
----------------
@isTest
public class Additiontest
{
    static testmethod void testadd()
    {
     integer a=5;
     integer b=7;
     integer c=12;
     system.assertEquals(12,12); 
    }  
    
}

and when executing the test class it is showing as pass...

Please need help on this....thanks
Best Answer chosen by iyappan kandasamy 4
SFDC Dev 2269SFDC Dev 2269
Hey @iyappan kandasamy 4,
Test class is used to test your functionality(class you have writter), and in order to test your functionality you need to invoke the Addition class and its methods.
In your test class you have just added the two integers. you need to invoke your class as below in your test class
 
@isTest
public class Additiontest
{
	static testmethod void testadd()
    {
		Addition addObj = new Addition();
		Integer result = addObj.add();
		System.assertEquals(9,result); // As value of a and b in Addition class are 5,4 the expected value is 9
    }  
    
}

Refer: https://developer.salesforce.com/page/An_Introduction_to_Apex_Code_Test_Methods
 

All Answers

SFDC Dev 2269SFDC Dev 2269
Hey @iyappan kandasamy 4,
Test class is used to test your functionality(class you have writter), and in order to test your functionality you need to invoke the Addition class and its methods.
In your test class you have just added the two integers. you need to invoke your class as below in your test class
 
@isTest
public class Additiontest
{
	static testmethod void testadd()
    {
		Addition addObj = new Addition();
		Integer result = addObj.add();
		System.assertEquals(9,result); // As value of a and b in Addition class are 5,4 the expected value is 9
    }  
    
}

Refer: https://developer.salesforce.com/page/An_Introduction_to_Apex_Code_Test_Methods
 
This was selected as the best answer
iyappan kandasamy 4iyappan kandasamy 4
Thanks a lot
iyappan kandasamy 4iyappan kandasamy 4
Hi,
one more doubt I have 
For fetching custom object records I have written Apex code as:

Apex class for fetching Bank object custom records
-------------------------------------------------------------------
public class RetBank
{
public void retrievedata()
{
    banks__c[] bk=[select Id,Name from banks__c];
    for(banks__c bnk:bk)
    {
        system.debug('The records are:'+bnk);
    }
}
}

Test class
-------------
@isTest
public class RetBanktest 
{
static testmethod void banks()
{
    RetBank fb=new RetBank();
    fb.retrievedata();
}
}


It is WOrking properly but the test class is giving only 75% ..Y it is not giving 100%....Any guidance please....THanks in advance
SFDC Dev 2269SFDC Dev 2269
Hey  iyappan kandasamy,
Sorry for the delay! May be you have already solved your query, but still here is the answer to your query:
In your apex code you are querying(fetching) banks__c records and then you are iterating over the list of records fetched.
Also, in your test class you have invoked a method of class. But when you code is running under test execution it does not refer any records from your Salesforce Org and you do not get any records in the list you have fetched, So your code will not go inside the for loop to debug your record and hence that part in your test class is uncovered. Your test class would be simulation which would test scenarios.
Now to test your apex code which requires records of object you need to create them in your Test code as below:
  
  Test class
-------------
@isTest
public class RetBanktest 
{
static testmethod void banks()
{
    banks__c bankObj = new  banks__c();
    bankObj.Name = 'Test bank';       // you can give the field values as per the scenario or your code wants
    insert bankObj;
    RetBank fb=new RetBank();
    fb.retrievedata();           // this method would now fetch the record which you just inserted and would go inside the for loop 
}
}



please refer the Apex Test method guide for reference

Thanks!
 
iyappan kandasamy 4iyappan kandasamy 4
Thanks for your reply.... Sent from Yahoo Mail on Android
iyappan kandasamy 4iyappan kandasamy 4
Thanks for your response...will implement this also.. thanks Sent from Yahoo Mail on Android