John Terry
John Terry

Reputation: 61

J Unit Testing for Multiplication in Java

public static int multiply(int a, int b) {
    int product = a * b;
    return product;
}

I am trying to write a J Unit test for this code. Right now it passes, but I am not fully sure if I have it correct. I am also not fully sure if the code is correct to begin with. The code is suppose to take two rational numbers as parameters and return a Rational number as their product.

@Test   
public void multiplyTest() {        
    int product = Rational.multiply(5/7,2/3);       
    assertEquals(product, Rational.multiply(5/7, 2/3));     
}

Update

Here is my Rational class with my actual code:

public class Rational {

    private int num;
    private int den;

    public Rational(int numIn, int denIn) {
        num = numIn;
        den = denIn;

    }

    public int getNum() {
        return num;
    }

    public int getDen() {
        return den;
    }

    public String toString() {
        return num + "/" + den;
    }

    public String reciprocal() {
        return den + "/" + num;
    }

    public static int multiply(int a, int b) {
        int product = a * b;
        return product;
    }

    public int divide(int a) {
        int number = num / den;
        return number / a;
    }

    public int add(int number) {
        int sum = ((this.num * den) + (num * this.den)) / (this.den * den);
        return sum;
    }

}

Upvotes: 4

Views: 2842

Answers (2)

Spyros K
Spyros K

Reputation: 2605

The solution is not correct nor is the test.

Your method takes as inputs two integers and returns an integer. You need to create a Rational class with nominator and denominator fields. Use it as the type of arguments and return type.

Also you need to tell the result to the test which is 10/21 and the test will determine if the method under test can get the correct result . The given junit uses the same method to calculate the same thing twice and then verifies that the results are the same. They are of course the same but this proves nothing.

Update

Based on your update I provide an updated version of your Rational class. Similar changes can be done to the other methods. Notice that it would be better for reciprocal to return a rational so that the programmer can also use it. You can still print it by writting rational.reciprocal() as toString will be automatically called for example in System.out.println(rational.reciprocal());

public class Rational {

    private final int num;
    private final int den; 

    public Rational(int numIn, int denIn) {
        num = numIn;
        den = denIn;

    }

    public int getNum() {
        return num;
    }

    public int getDen() {
        return den;
    }

    public String toString() {
        return num + "/" + den;
    }


    public Rational reciprocal() {
        return new Rational(den,num);
    }

    public static Rational multiply(Rational a, Rational b) {
        return new Rational(a.num * b.num , a.den * b.den );
    }

    public Rational divide(int a) {
        return new Rational(this.num,a*this.den);
    }


}

Upvotes: 3

Robert
Robert

Reputation: 8683

Your code just calls the code under test twice. That would test that multiply is idempotent (at least when called twice). It does not test that it actually multiplies its parameters. If it just added them and returned the sum, your test would not notice (i.e., fail).

Your test is also quite complicated; why the division in the code?

Do something like this instead:

@Test
public void multiplyInts() {        
    assertEquals(Integer.valueOf(35), Rational.multiply(5, 7));     
}

Repeat with real or long, if that is important to your code under test.

Upvotes: 0

Related Questions