From 42ff96c75480f7059824137faf49eecd6dec5511 Mon Sep 17 00:00:00 2001 From: Raihan Ariq <202310715297@mhs.ubharajaya.ac.id> Date: Fri, 7 Nov 2025 00:14:53 +0700 Subject: [PATCH] Pembuatan Direktori test untuk Unit Testing pada fungsi calculateBMI dan calculateBMICategory --- app/build.gradle.kts | 3 + .../com/example/bmicalculator/MainActivity.kt | 11 ++- app/src/test/java/calculateBMITest.kt | 88 +++++++++++++++++++ 3 files changed, 96 insertions(+), 6 deletions(-) create mode 100644 app/src/test/java/calculateBMITest.kt diff --git a/app/build.gradle.kts b/app/build.gradle.kts index 7107dc2..5aaa38d 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -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") diff --git a/app/src/main/java/com/example/bmicalculator/MainActivity.kt b/app/src/main/java/com/example/bmicalculator/MainActivity.kt index 399901a..f728342 100644 --- a/app/src/main/java/com/example/bmicalculator/MainActivity.kt +++ b/app/src/main/java/com/example/bmicalculator/MainActivity.kt @@ -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() } diff --git a/app/src/test/java/calculateBMITest.kt b/app/src/test/java/calculateBMITest.kt new file mode 100644 index 0000000..ae640c7 --- /dev/null +++ b/app/src/test/java/calculateBMITest.kt @@ -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) + } +} \ No newline at end of file