การยืนยันผลและการดำเนินการของ JUnit
เรียนรู้การใช้เมธอดยืนยันผลของ JUnit เพื่อตรวจสอบผลลัพธ์การทดสอบและเรียกใช้การทดสอบอย่างมีประสิทธิภาพภายใน IDE
การยืนยันผลและการดำเนินการของ JUnit เป็นบทเรียน Testing Mastery: JUnit, Mockito & Integration Tests ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Testing Mastery: JUnit, Mockito & Integration Tests และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Testing Mastery: JUnit, Mockito & Integration Tests มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
What are Assertions?
Assertions are the checks that decide pass or fail: they verify a condition is true, and if it isn’t, the test fails and flags the problem.
Checking for Equality
assertEquals() is the workhorse: pass it the expected value and the actual value, and the test passes only when they match.
`assertEquals` in Action
Here’s assertEquals verifying a sum: compare the expected result against what your method returns. Match means pass.
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class MathUtilsTest {
// A simple method to test
int add(int a, int b) {
return a + b;
}
@Test
void testAddMethod() {
// Expected result: 5
int expected = 5;
// Actual result from our method
int actual = add(2, 3);
// Assert that expected and actual are the same
assertEquals(expected, actual, "2 + 3 should be 5");
}
}Checking Boolean Conditions
To check logical outcomes, use assertTrue and assertFalse — they pass when the condition is true or false respectively.
`assertTrue` in Practice
Here assertTrue confirms a number is positive — a clean way to validate any boolean condition.
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class NumberCheckerTest {
// A simple method to test
boolean isPositive(int number) {
return number > 0;
}
@Test
void testIsPositive() {
int num = 10;
// Assert that num is positive
assertTrue(isPositive(num), num + " should be positive");
}
}Handling Null Values
For null checks, use assertNotNull and assertNull — crucial for catching NullPointerExceptions before they bite.
`assertNotNull` Demo
Here assertNotNull and assertNull verify a lookup that returns a user or null when none is found.
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
public class UserServiceTest {
// Dummy method simulating fetching a user
String getUserById(int id) {
if (id == 1) {
return "Alice"; // User found
}
return null; // User not found
}
@Test
void testExistingUser() {
String user = getUserById(1);
assertNotNull(user, "User with ID 1 should exist");
}
@Test
void testNonExistingUser() {
String user = getUserById(99);
assertNull(user, "User with ID 99 should not exist");
}
}Executing JUnit Tests
Running tests is easy: most IDEs have built-in JUnit support — right-click a class or method, hit Run, and read the results window.
Understanding Pass/Fail
Reading results is simple: green means all tests passed, red means an assertion failed and there’s likely a bug. Click a failure for details.
Assertion Challenge
Consider the following simple method and test. What will be the outcome of running this test?
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class SimpleCalculatorTest {
int subtract(int a, int b) {
return a - b;
}
@Test
void testSubtract() {
assertEquals(10, subtract(15, 5));
}
}Recap: Assertions & Execution
You can now validate code with JUnit assertions — assertEquals, assertTrue, assertNull and friends — and run tests right in your IDE.
คำถามที่พบบ่อย
บทเรียน “การยืนยันผลและการดำเนินการของ JUnit” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การยืนยันผลและการดำเนินการของ JUnit” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Testing Mastery: JUnit, Mockito & Integration Tests ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Testing Mastery: JUnit, Mockito & Integration Tests มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การยืนยันผลและการดำเนินการของ JUnit”
เรียนรู้การใช้เมธอดยืนยันผลของ JUnit เพื่อตรวจสอบผลลัพธ์การทดสอบและเรียกใช้การทดสอบอย่างมีประสิทธิภาพภายใน IDE คุณปฏิบัติ Testing Mastery: JUnit, Mockito & Integration Tests ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Testing Mastery: JUnit, Mockito & Integration Tests หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Testing Mastery: JUnit, Mockito & Integration Tests บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน
บทเรียน “การยืนยันผลและการดำเนินการของ JUnit” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Testing Mastery: JUnit, Mockito & Integration Tests นี้ได้ไหม
ได้ บทเรียน Testing Mastery: JUnit, Mockito & Integration Tests ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- แนะนำการทดสอบหน่วย
- คำกำกับพื้นฐานของ JUnit 5
- การยืนยันผลและการดำเนินการของ JUnit
- การจัดระเบียบการทดสอบด้วย @DisplayName และการซ้อนกลุ่ม