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
iyappan kandasamy 4iyappan kandasamy 4 

test class for addition of two numbers

Hi,
I have written the apex class for addition of two numbers. but for writing the test class i have issue.
Program is
--------------
public class Add 
{    
public integer a;
public integer b;
public integer c;
public integer addt()
{
    c=a+b; 
    system.debug('the result is'+c);
    return c;
}      
}

My test class
-----------------
@istest
public class Addtest
{
static testmethod void testadd()
{
    Add ad=new Add();
    integer res=ad.addt();
    system.assertEquals(res);
}
}
the test class is throwing error as
"Method does not exist or incorrect signature: void assertEquals(Integer) from the type System"

Any guidance please

Thanks in advance.
Best Answer chosen by iyappan kandasamy 4
v varaprasadv varaprasad
hi iyappan,

try this : 
 
@istest
public class Addtest
{
static testmethod void testadd()
{

public integer a;
public integer b;
public integer c;

    Add ad=new Add();
	ad.a = 10;
	ad.b = 20;
	
    integer res=ad.addt();
    system.assertEquals(30,res);
}
}

Hope this helps you!
If my answer helps resolve your query, please mark it as the 'Best Answer' & upvote it to benefit others.

Thanks
Varaprasad
@For Salesforce Project Support: varaprasad4sfdc@gmail.com

Salesforce latest interview questions  :
https://www.youtube.com/channel/UCOcam_Hb4KjeBdYJlJWV_ZA?sub_confirmation=1

All Answers

Steven NsubugaSteven Nsubuga
make these changes in order for your code to be more meaningful.

Class
public class Add 
{    
    public integer a;
    public integer b;
    public integer c;
    public integer addt(integer a, integer b)
    {
        c=a+b; 
        system.debug('the result is'+c);
        return c;
    }      
}


Test Class
@istest
public class Addtest
{
    static testmethod void testadd()
    {
        Add ad=new Add();
        integer res=ad.addt(2, 5);
        system.assertEquals(res, 7);
    }
}


 
v varaprasadv varaprasad
hi iyappan,

try this : 
 
@istest
public class Addtest
{
static testmethod void testadd()
{

public integer a;
public integer b;
public integer c;

    Add ad=new Add();
	ad.a = 10;
	ad.b = 20;
	
    integer res=ad.addt();
    system.assertEquals(30,res);
}
}

Hope this helps you!
If my answer helps resolve your query, please mark it as the 'Best Answer' & upvote it to benefit others.

Thanks
Varaprasad
@For Salesforce Project Support: varaprasad4sfdc@gmail.com

Salesforce latest interview questions  :
https://www.youtube.com/channel/UCOcam_Hb4KjeBdYJlJWV_ZA?sub_confirmation=1
This was selected as the best answer
iyappan kandasamy 4iyappan kandasamy 4
Great...Thanks .a lot for both of them...Steven Nsubuga and varaprasad...It worked out for me...
 
Akash HanchateAkash Hanchate
How to write a apex class for Add  sub multiply and div  and how to test from debug apex code ?
Navdeep Singh 95Navdeep Singh 95
@isTest
public class SampleTest {
 
    @isTest
 static  void Summary() {
        Sample s = new Sample();
       s.xvalue= 5;
          s.yvalue = 4;
        s.add();
        s.div();
        s.mul();
        s.sub();
       
      
    }
}

Apex class
public class Sample  {
    public Integer xvalue {get;set;}
    public Integer yvalue {get;set;}
    public Integer result {get;set;}
    public string operation {get;set;}
 
    public Integer sub() {
        result = xvalue-yvalue;
        operation = 'Subtraction';
        return null;
    }
    public Integer add() {
        result = xvalue+yvalue;
        operation = 'Addition';
        return null;
    }
    public Integer mul() {
        result = xvalue*yvalue;
        operation = 'Multiplication';
        return null;
    }
        public Integer div() {
        result = xvalue/yvalue;
        operation = 'Divison';
        return null;
    }
    
   
}
If my answer helps resolve your query, please mark it as the 'Best Answer' & upvote it to benefit others.