2014-10-06 74 views
0

我第一次使用JUnit。不知道該怎麼辦 始終未能得到第17行 有1次失敗: 1)testBigRealNumberSum(BigRealNumberUtilityTest) java.lang.AssertionError:預期:< 1>卻被:java.lang.AssertionError:expected:<1>但是:<BigRealNumber @ 67424e82>

FAILURES! 試驗運行:1,失敗:1

import org.junit.Assert; 
import static org.junit.Assert.*; 
import org.junit.Before; 
import org.junit.Test; 


public class BigRealNumberUtilityTest { 




@Test 
public void testBigRealNumberSum(){ 
    BigRealNumber test1 = new BigRealNumber("5"); 
    BigRealNumber test2 = new BigRealNumber("4"); 
    BigRealNumber result = BigRealNumberUtility.subtract(test1,test2); 
    Assert.assertEquals("1", result); 




} 

}

/* 

*這個類是用於 * BigRealNumber類的實例的工具類。它包含用於BigRealNumber類實例的算術運算 *的方法。 **/

公共類BigRealNumberUtility {

/* 
*This method computes for the sum of an array of BigRealNumber instances. 
* 
*@param numbers This is an array of big decimal real numbers. 
*@return realNum This is the BigRealNumber value used to represent the sum of all 
*the instances of the BigRealNumber class in the method parameter. 
**/ 

公共靜態BigRealNumber總和(BigRealNumber ...數字){ 雙總和= 0;

for(int i = 0; i < numbers.length; i++){ 
     sum = sum + numbers[i].decimalValue; 
    } 

    String s = Double.toString(sum); 
    BigRealNumber realNum = new BigRealNumber(s); 

    return realNum; 

}

/* 
*This method computes for the difference of two BigRealNumber instances 
*used in its parameter. 
* 
*@param n1 This is the minuend used in the calculation 
*@param n2 This is the subtrahend used in the calculation 
* 
*@return realNum This is the BigRealNumber value used to represent 
*the calculated difference between the decimal values used in the method. 
**/ 

公共靜態BigRealNumber減法(BigRealNumber N1,N2 BigRealNumber){ 雙差= n1.decimalValue - n2.decimalValue;

String s = Double.toString(difference); 
    BigRealNumber realNum = new BigRealNumber(s); 

    return realNum; 

}

/* *此方法計算兩個BigRealNumber實例的產物。 * * @ param n1這是一個因子,用於計算兩個十進制小數的乘積*實數。 * @ param n2這是一個因子,用於計算兩個十進制小數的乘積*實數。 * * @ return realNum這是在方法中使用的十進制值之間計算的產品 *的BigRealNumber表示形式。 **/ public static BigRealNumber product(BigRealNumber n1,BigRealNumber n2){ double product = n1.decimalValue * n2.decimalValue;

String s = Double.toString(product); 
    BigRealNumber realNum = new BigRealNumber(s); 

    return realNum; 

}}

回答

0

你是比較字符串 「1」 與類BigRealNumber的對象。對於這種比較,等於總會產生「假」。你應該在BigRealNumber上做一個toString()或者實例化一個新的值爲「1」的值。

相關問題