java - 关于assertEquals的问题

标签 java testcase

好吧,我的任务是为 Fraction 类创建一个测试类,其中包括加法、减法、除法和乘法。我首先从 testAdd 方法开始,但似乎无论我如何尝试测试它,它总是失败。一个例子是我将预期的答案放入assertEquals(“10/8”,f2.add(f1));中它会说它失败并带有警告标志,指出预期 <10/8> 但实际为 <10/8>。我什至会使用另一个分数实例变量通过输入assertEquals(f3,f2.add(f1));来补充答案。仍然给我同样的答复。

这是 Fraction 类(已经提供了 Fraction 类):

    // import ArithmeticException class for use in division method
import java.lang.ArithmeticException;



public class Fraction
{

        /**
         *  Instance Variables
         **/
 private int numerator;          // Stores the numerator of the fraction
 private int denominator;        // Stores the denominator of the fraction



        /**
         * Constructor
         * Takes two integer arguments, the numerator and denominator for the fraction
         * being created.
         **/
 public Fraction(int numerator, int denominator)
 {
                // initialize instance variables
  this.numerator = numerator;
  this.denominator = denominator;
 }
        // end constructor



        /**
         * Additon Method
         * Takes one Fraction argument, calculates the sum of the
         * calling Fraction object and its argument, constructs a new Fraction
         * object that stores the sum, and returns this new Fraction object.
         **/
 public Fraction add( Fraction otherFraction )
 {

                // declare and initialize local variables for the numerator and denominator
  int commonDenominator  = this.denominator * otherFraction.denominator;
  int newNumerator = ( this.numerator * otherFraction.denominator ) + ( otherFraction.numerator * this.denominator );

                // Declare and initialize resulting Fraction object using the above numerator and denominator
  Fraction result = new Fraction( newNumerator, commonDenominator );

  return result;

 }
        // end add method



        /**
         * Subtraction  Method
         * Takes one Fraction argument, calculates the difference of the
         * calling Fraction object and its argument, constructs a new Fraction
         * object that stores the difference, and returns this new Fraction object.
         **/
 public Fraction subtract( Fraction otherFraction )
 {

                // declare and initialize local variables for the numerator and denominator
  int commonDenominator = this.denominator * otherFraction.denominator;
  int newNumerator = ( this.numerator * otherFraction.denominator ) - ( otherFraction.numerator * this.denominator );

                // Declare and initialize resulting Fraction object using the above numerator and denominator
  Fraction result = new Fraction( newNumerator, commonDenominator );

  return result;

 }
        // end subtract method



        /**
         * Multiplication Method
         * Takes one Fraction argument, calculates the multiple of the
         * calling Fraction object and its argument, constructs a new Fraction
         * object that stores the multiple, and returns this new Fraction object.
         **/
 public Fraction multiply( Fraction otherFraction )
 {

                // declare and initialize local variables for the numerator and denominator
                int newNumerator = this.numerator * otherFraction.numerator;
                int newDenominator = this.denominator * otherFraction.denominator;

                // Declare and initialize resulting Fraction object using the above numerator and denominator
                Fraction result = new Fraction( newNumerator, newDenominator );

  return result;

 }
        // end multiply method



        /**
         * Division Method
         * Takes one Fraction argument, calculates the dividend of the
         * calling Fraction object and its argument, constructs a new Fraction
         * object that stores the dividend, and returns this new Fraction object.
         **/
 public Fraction divide( Fraction otherFraction ) throws ArithmeticException
 {

                // If the nominator of the divisor is zero throw a division by zero exception
                if( otherFraction.numerator == 0 )
                {

                    throw new ArithmeticException( "Division by Zero" );

                }

                // Construct a new Fraction object that is the inverse of the divisor
  Fraction inverse = new Fraction( otherFraction.denominator, otherFraction.numerator );

                // Calculate the result of the division by multiplying by the inverse of the divisor
                // and store the result in a new Fraction object.
  Fraction result = this.multiply( inverse);

  return result;

 }
        // end divide method



        /**
         * String Conversion Method
         * Uses the state of the object (the numerator and denominator), converts
         * them to strings and the returns a String representation of the Fraction
         * object.
         **/
 public String toString()
 {
  String text = Integer.toString(this.numerator) + "/" + Integer.toString(this.denominator);

  return text;
 }
        // end toString method

}
// END CLASS FRACTION

我的类中也有这个包,所以这不是问题。 这是我尝试创建的测试类:

import junit.framework.TestCase;


public class TestFraction extends TestCase{

  Fraction f1;
  Fraction f2;
  Fraction f3;
  Fraction f4;
  Fraction result1;
  Fraction result2;
  Fraction result3;
  Fraction result4;

  protected void setUp(){

    f1 = new Fraction(1,2);
    f2 = new Fraction(3,4);
    f3 = new Fraction(10,8);
    f4 = new Fraction(2,3);

  }

  public void testAdd(){
    assertEquals(,f2.add(f1));
  }

  public void testSubtract(){

  }

  public void testDivide(){

  }

  public void testMultiply(){

  }



}

最佳答案

Fraction 类实现 equals()hashcode() 后,执行 assertEquals(new Fraction(10,8) , f2.add(f1));

或者,如果您不确定 equals()hashcode() 方法,只需破解即可 assertEquals("10/8", f2.add(f1).toString());

add 方法返回 Fraction,但是当您编写 assertEquals("10/8", f2.add(f1)) 时, "10/8"String 类型,但 f2.add(f1) 返回 Fraction 所以因为有类型不匹配,测试失败。

关于java - 关于assertEquals的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21515190/

相关文章:

testing - Perl、C#、C、Java 的 Lorem ipsum?

java - session bean 中的 EntityManager 异常处理

java - Redis 中的范围查询 - Spring Data Redis

java - 如何仅检查 Arraylist.Contains(Object) 方法的少数属性应返回 true?

java - java的AtomicBitSet实现

android - 如何创建仅在一个或多个类中运行指定测试的 Android 测试套件?

java - Hibernate中如何进行保存点和回滚?

c# - 将 nunit 与 Jenkins 一起使用时获取 C# 应用程序的执行路径

python - 带有回滚仿真的 Django TransactionTestCase

java - 表单验证错误验证未处理