-1

I have this calculator .java from an online practice and I need to test it in JUnit in Eclipse;

package calculator;
import java.util.Scanner;

public class Calculator {

    private double accumulator;

    public Calculator() { }
    public Calculator(double initialValue) { accumulator = initialValue; }

    public void add(double number) { accumulator += number; }
    public void subtract(double number) { accumulator -= number; }
    public void multiply(double number) { accumulator *= number; }
    public void divide(double number) { accumulator /= number; }
    public void sqrt() { accumulator = Math.sqrt(accumulator); }
    public void setAccumlator(double accumulator) { this.accumulator = accumulator; }
    public double getAccumulator() { return accumulator; }

    @Override
    public String toString() { return "Result:" + accumulator; }
}

I've been pouring through documentation (I'm rather new to programming in general) and unsure of how to actually do this. I have JUnit set up and a test file set up, like;

@Test
public void testAdd(){

} 
@Test
public void testDivideByZero(){

}

etc.

I've tried a few things and the syntax was wrong, stuff like

The method add(double) in the type Calculator is not applicable for the arguments (double, double)

or

Cannot make a static reference to the non static method add(double) from the type Calculator

Any suggestions?

Pang
  • 9,564
  • 146
  • 81
  • 122
Binngo
  • 1
  • 1
  • 5

1 Answers1

0

Example of a test

private Calculator mCalculatorInstance;

@Before
public void setupTestEnvironment() {
    // This method will be call before every single test.
    mCalculatorInstance = new Calculator(2.0);
}

@Test
public void testAdd(){
    mCalculatorInstance.add(2.0); // add 2 to the initial value of 2 which I instanitiate above should return 4.
    assertEquals("Adding 2 to 2 should give me 4.", 4.0, c.getAccumulator());
}

In order to do testing, you need to know the expected output of the test. Let use the above test as scenario. I declared that I'm initializing a calculator object with accumulator value of 2.

mCalculatorInstance = new Calculator(2.0); 

I know that using the add method of the instance will add the parameter to the accumulator. Hence I call the add method.

mCalculatorInstance.add(2.0);

So now I'm adding 2.0 to the accumulator which already have a value of 2.0

2.0 + 2.0 = 4.0

Since the object provide a method to get back the accumulator, I use the method to get back accumulator and check whether the addition is correct, the value of accumulator should be 4.0.

assertEquals("Adding 2 to 2 should give me 4.", 4.0, c.getAccumulator());
Ophitect
  • 543
  • 4
  • 18
  • Perfect! Although assertEquals(String, double, double) is deprecated, requires an epsilon/buffer number now – Binngo Jan 29 '16 at 03:21
  • @Binngo There was a stackoverthread about this. http://stackoverflow.com/questions/5686755/meaning-of-epsilon-argument-of-assertequals-for-double-values – Ophitect Jan 29 '16 at 03:25