J Unit Testing for Java Multiplication

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. Now it passes, but I'm not quite sure if this is correct. I'm also not quite sure if the code is right for a start. The code should take two rational numbers as parameters and return Rational as its 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;
    }

}
+4
source share
2 answers

The solution is incorrect and not a test.

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

, 10/21, , . junit , , . , , , .

Rational. . , , , . , rational.reciprocal(), toString , , 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);
    }


}
+3

. , multiply ( , ). , . , (.. ).

; ?

- :

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

, .

+1

Source: https://habr.com/ru/post/1695375/


All Articles