Software Testing Engineering / UNIT TEST DESIGN & AUTOMATION / TST-007
Money Unit Tests
Explore concept graphLatihan soal
Jalankan lab unit test tanpa Spring
Repository menyediakan lab Maven mandiri, lengkap dengan FeeCalculator.java, FeeCalculatorTest.java dan pom.xml. Lab menggunakan Java 21, JUnit Jupiter 5.14.4, dan Maven Surefire 3.5.4, tidak membutuhkan Spring Boot ataupun database.
java -version
mvn -version
mvn -B -ntp -f labs/TST-007/pom.xml clean verifyLihat laporan Maven di labs/TST-007/target/surefire-reports/. Jangan menyimpulkan test berjalan hanya dari pesan BUILD SUCCESS: periksa nilai Tests run dan pastikan jumlahnya nonzero serta tidak ada failures/errors/skipped. CI akan memvalidasi jumlah tepat 20 test secara independen dari log terminal.
Contoh satu test behavior:
@Test
void normalIdrReturnsExactlyOneHundredFifty() {
assertEquals(new BigDecimal("150"),
calculator.calculateFee(new BigDecimal("10000"), "IDR"));
}Untuk skenario yang wajib ditolak:
@Test
void negativeIdrPrincipalIsRejected() {
assertThrows(IllegalArgumentException.class,
() -> calculator.calculateFee(new BigDecimal("-1"), "IDR"));
}Batas bukti: generator V3 hanya mendukung fixture satu berkas Node/Java. Ia menandai JUnit sebagai not-executable untuk runner tersebut. Maven JUnit integration gate yang terpisah menjalankan test yang sebenarnya; ini bukan simulasi, dan tidak membuktikan akurasi tarif bank nyata.
Konsep 1 / 3
Write a JUnit Jupiter unit test using Arrange–Act–Assert, @Test and @BeforeEach without Spring context.
Coba prediksi atau perbaiki
JUnit test returns the correct fee but only asserts calculator is not null. What should you change?
Kalau jawabanmu berbeda
Kesalahan yang mungkin: Testing the class wiring rather than a fee result.
Mengapa tidak bekerja: An instance-creation assertion can pass even when rate, rounding or validation behavior is wrong.
Coba perbaiki: Assert the exact returned monetary amount, using decimal-string BigDecimal values and a public method call.
Periksa hasil sendiri
Hasil yang diharapkan: Maven Surefire discovers and reports the test as passed, with no Spring dependencies or context startup.
Kalau berbeda: Inspect test class naming, junit-jupiter dependency, Surefire plugin and the first assertion failure rather than adding @SpringBootTest.
Konsep 2 / 3
Check zero/negative amounts, exactly-at-cap and above-cap values, and expected exceptions as separate observable outcomes.
Coba prediksi atau perbaiki
Would a test for amount=1000001 alone prove that amount=1000000 is accepted?
Kalau jawabanmu berbeda
Kesalahan yang mungkin: Using one test for a value above max and assuming the inclusive max case is automatically proven.
Mengapa tidak bekerja: The code might incorrectly reject the exact valid limit or accept an amount just above it.
Coba perbaiki: Test both sides and identify expected exceptions as assertions rather than test failures.
Periksa hasil sendiri
Hasil yang diharapkan: Both zero and exact-limit cases pass, while negative and over-limit cases throw the expected exception types.
Kalau berbeda: Inspect the declared fee policy and BigDecimal compareTo semantics; use failure messages rather than loosening assertions.
Konsep 3 / 3
Test rounding near thresholds and enforce IDR/USD minor-unit precision with a reproducible Maven run.
Coba prediksi atau perbaiki
A programmer uses new BigDecimal(0.34) and compares the rounded fee to the expected result. What makes this fragile?
Kalau jawabanmu berbeda
Kesalahan yang mungkin: Assuming BigDecimal is safe even when constructed from a binary double and rounded implicitly.
Mengapa tidak bekerja: Binary double construction can embed an unintended approximation; implicit scale decisions hide the expected monetary rule.
Coba perbaiki: Use BigDecimal from exact strings, setScale with explicit HALF_UP once, and test values on both sides of the rounding threshold.
Periksa hasil sendiri
Hasil yang diharapkan: Surefire reports passed assertions for IDR/USD scale, rounding, invalid precision and independent exception cases.
Kalau berbeda: If Maven finds zero tests, check Surefire reports and Jupiter engine; if a value differs, inspect the exact decimal intermediate result and rounding policy.
Lima soal diagnosis unit test
Jawab tanpa melihat pembahasan dulu. Kamu bisa memperbaiki jawaban dan mencoba lagi. Kuis ini latihan mandiri dan tidak menyimpan skor atau progres.