Pembuatan Direktori test untuk Unit Testing pada fungsi calculateBMI dan calculateBMICategory

This commit is contained in:
202310715297 RAIHAN ARIQ MUZAKKI 2025-11-07 00:14:53 +07:00
parent b5db44764b
commit 42ff96c754
3 changed files with 96 additions and 6 deletions

View File

@ -73,8 +73,11 @@ dependencies {
implementation("androidx.compose.ui:ui-tooling-preview")
implementation("androidx.core:core-ktx:1.15.0")
implementation("androidx.lifecycle:lifecycle-runtime-ktx:2.8.7")
implementation("junit:junit:4.12")
// Unit Testing
testImplementation("junit:junit:4.13.2")
testImplementation("org.junit.jupiter:junit-jupiter:5.8.1")
androidTestImplementation(platform("androidx.compose:compose-bom:2024.12.01"))
androidTestImplementation("androidx.compose.ui:ui-test-junit4")

View File

@ -65,8 +65,6 @@ import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.example.bmicalculator.ui.theme.BMICalculatorTheme
import java.text.DecimalFormat
import java.text.NumberFormat
import java.util.Locale
import kotlin.math.pow
class MainActivity : ComponentActivity() {
@ -208,9 +206,9 @@ fun UnitUSCFormulaRow(
*
* Catatan: tambahkan unit test untuk kalkulasi BMI ini
*/
private fun calculateBMI(BmiHeight: Double, BmiWeight: Double, unitUSC: Boolean): String {
fun calculateBMI(BmiHeight: Double, BmiWeight: Double, unitUSC: Boolean): String {
if (BmiHeight <= 0 || BmiWeight <= 0){
return "0"
return "0.0"
}
val heightInMeter = BmiHeight/100 // konversi centimeter ke meter
@ -223,6 +221,7 @@ private fun calculateBMI(BmiHeight: Double, BmiWeight: Double, unitUSC: Boolean)
val df = DecimalFormat("#.#")
return df.format(bmi)
}
/**
* Calculates the BMI Category
* bmi < 18.5 -> Underweight
@ -232,7 +231,7 @@ private fun calculateBMI(BmiHeight: Double, BmiWeight: Double, unitUSC: Boolean)
*
* Catatan: tambahkan unit test untuk kalkulasi BMI ini
*/
private fun calculateBMICategory(bmi: String): String {
fun calculateBMICategory(bmi: String): String {
val bmiValue = bmi.replace(",", ".").toDoubleOrNull() ?: return ""
return when {
@ -246,7 +245,7 @@ private fun calculateBMICategory(bmi: String): String {
@Preview(showBackground = true)
@Composable
fun TipTimeLayoutPreview() {
fun BMICalculatorLayoutPreview() {
BMICalculatorTheme {
BMICalculatorLayout()
}

View File

@ -0,0 +1,88 @@
package com.example.bmicalculator
import org.junit.Assert.*
import org.junit.Test
class CalculateBMITest {
// Test BMI SI Metric (Kg, Cm)
@Test
fun testCalculateBMI_SI_Normal() {
val result = calculateBMI(170.0, 65.0, false) // 170cm, 65kg
assertEquals("22.5", result) // Expected BMI ≈ 22.5
}
@Test
fun testCalculateBMI_SI_Zero() {
val result = calculateBMI(0.0, 0.0, false)
assertEquals("0.0", result)
}
@Test
fun testCalculateBMI_SI_Underweight() {
val result = calculateBMI(170.0, 50.0, false) // 170cm, 50kg
assertEquals("17.3", result) // Expected BMI ≈ 17.3
}
@Test
fun testCalculateBMI_SI_Overweight() {
val result = calculateBMI(170.0, 80.0, false) // 170cm, 80kg
assertEquals("27.7", result) // Expected BMI ≈ 27.7
}
// Test BMI USC Units (Pound, Inch)
@Test
fun testCalculateBMI_USC_Normal() {
val result = calculateBMI(70.0, 154.0, true) // 70 inch, 154 lbs
assertEquals("22.1", result) // Expected BMI ≈ 22.1
}
@Test
fun testCalculateBMI_USC_Zero() {
val result = calculateBMI(0.0, 0.0, true)
assertEquals("0.0", result)
}
// Test BMI Category
@Test
fun testCalculateBMICategory_Underweight() {
val result = calculateBMICategory("17.5")
assertEquals("Underweight", result)
}
@Test
fun testCalculateBMICategory_Normal() {
val result = calculateBMICategory("22.5")
assertEquals("Normal", result)
}
@Test
fun testCalculateBMICategory_Overweight() {
val result = calculateBMICategory("27.5")
assertEquals("Overweight", result)
}
@Test
fun testCalculateBMICategory_Obesity() {
val result = calculateBMICategory("32.0")
assertEquals("Obesity", result)
}
@Test
fun testCalculateBMICategory_Zero() {
val result = calculateBMICategory("0.0")
assertEquals("", result)
}
@Test
fun testCalculateBMICategory_EmptyString() {
val result = calculateBMICategory("")
assertEquals("", result)
}
@Test
fun testCalculateBMICategory_WithComma() {
val result = calculateBMICategory("22,5") // Test dengan koma
assertEquals("Normal", result)
}
}