흥미로운 문제가 발생했습니다. 나는 햄버거를 판매하는 응용 프로그램을 만들고 있습니다. 지금까지 JUnit의 테스트가 통과되었습니다. 그러나 jUnit의 결과가 실제로 같은 번호의 인쇄 된 버전과 일치하지 않는다는 것을 알았습니다.검증 된 테스트의 double이 out.prinln 버전과 일치하지 않습니다.
또한 계산 결과는 매우 단순한 복식을 더하고 빼는 것과 같은 간단한 작업에서 비롯된 것입니다. 11.90 대신 11.8999999999? 시험 방법
Income so far is 11.899999999999999. Transaction value: 11.899999999999999
Income so far is 20.9. Transaction value: 9.0
테스트 방법
public void completeTransaction(){
double value = 0.0;
for(Burger product : this.transaction.keySet()){
for(int i=0; i<(int)(this.transaction.get(product)); i++){
double price = calculateBurgerPrice(product);
this.income += (double) price;
value += price;
this.soldBurgers += 1;
}
}
System.out.println("Income so far is " + getIncome() + ". Transaction
value: " + value);
this.processedTransactions += 1;
}
는 감사의
@Test
public void canGenerateAndCountProccessedTransactions2(){
till1.newTransaction();
till1.addProduct(burger1);//3.00
till1.addProduct(burger2);//4.60
till1.addProduct(burger3);//4.30
till1.completeTransaction();
assertEquals(11.90, till1.getIncome(), 0.1);
till1.newTransaction();
till1.addProduct(burger1);//3.00
till1.addProduct(burger1);//3.00
till1.addProduct(burger1);//3.00
till1.completeTransaction();
assertEquals(20.90, till1.getIncome(), 0.1);
assertEquals(2, till1.numOfProcessedTransactions());
}
인쇄 결과를 전달
테스트 파일!
11.90 이중와 비트의 유한 수에 표시 할 수 없습니다. 이는 부동 소수점 값이 (음수) 2의 합이기 때문입니다. 하나는'new BigDecimal ("11.90")'에서와 같이 BigDecimal을 사용하거나 반올림 및 반올림을 계속해야합니다. –
나는 그것에 대해 더 자세히 읽을 필요가있다. 나는 그것을 줄 것이다! 감사합니다 :) – bwielk
'assertEquals (20.90, till1.getIncome(), 0.1)'에는 매우 큰 델타가 있습니다. 그것은 당신이 정말로 소수점 뒤에 첫 번째 자릿수에 대해서 신경 쓰지 않는다는 것을 의미합니다. 그것은 1.25와 1.5를 동일하게 취급 할 것입니다. 어설 션이 실패하지 않고도 작게 만들 수 있습니다. –