การทดสอบเว็บคอนโทรลเลอร์ด้วย MockMvc
เขียนการทดสอบแบบเจาะจงสำหรับจุดปลาย REST ด้วย MockMvc เพื่อจำลองคำขอ HTTP โดยไม่ต้องเปิดเซิร์ฟเวอร์จริง
การทดสอบเว็บคอนโทรลเลอร์ด้วย MockMvc เป็นบทเรียน Spring Boot 4 Complete Guide ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Spring Boot 4 Complete Guide และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Spring Boot 4 Complete Guide มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Why Test Controllers in Isolation?
Controller tests verify routing, request mapping, status codes, and JSON serialization without the cost of a full server. MockMvc makes this fast and precise.
Introducing @WebMvcTest
The @WebMvcTest annotation loads only the web layer, your controllers and their supporting beans, keeping the test slice small and quick.
@WebMvcTest(UserController.class)
class UserControllerTest {
}Injecting MockMvc
Inside a @WebMvcTest, Spring provides an auto-configured MockMvc instance you autowire to perform simulated requests.
@Autowired
MockMvc mockMvc;Performing a GET Request
Use the fluent API to perform a request and chain expectations on the response.
mockMvc.perform(get("/users/1"))
.andExpect(status().isOk());Mocking the Service Layer
Controllers depend on services, so you replace them with mocks using @MockBean. This isolates the controller from real business logic.
@MockBean
UserService userService;Stubbing Behavior
Combine Mockito stubbing with the request to control what the service returns.
when(userService.find(1L)).thenReturn(new User("Ada"));Asserting JSON Content
Use jsonPath to verify specific fields in the response body.
mockMvc.perform(get("/users/1"))
.andExpect(jsonPath("$.name").value("Ada"));Testing POST with a Body
Send a JSON payload and assert the created status, verifying your endpoint accepts and processes input correctly.
mockMvc.perform(post("/users")
.contentType(MediaType.APPLICATION_JSON)
.content("{\"name\":\"Ada\"}"))
.andExpect(status().isCreated());Verifying Validation Errors
Send invalid input and assert a 400 Bad Request to confirm your validation rules actually fire.
mockMvc.perform(post("/users")
.contentType(MediaType.APPLICATION_JSON)
.content("{}"))
.andExpect(status().isBadRequest());Verifying Interactions
Use Mockito's verify to confirm the controller actually called the service the expected number of times.
verify(userService).find(1L);When to Use Full Integration Tests
MockMvc slice tests are fast but mock the service. For end-to-end confidence across the real stack, complement them with full @SpringBootTest integration tests.
Quick Check
Test your understanding of MockMvc controller testing.
Recap
You used @WebMvcTest with MockMvc to test endpoints, mocked services with @MockBean, and asserted status codes and JSON. These slice tests are a fast first line of defense.
คำถามที่พบบ่อย
บทเรียน “การทดสอบเว็บคอนโทรลเลอร์ด้วย MockMvc” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การทดสอบเว็บคอนโทรลเลอร์ด้วย MockMvc” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Spring Boot 4 Complete Guide ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Spring Boot 4 Complete Guide มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การทดสอบเว็บคอนโทรลเลอร์ด้วย MockMvc”
เขียนการทดสอบแบบเจาะจงสำหรับจุดปลาย REST ด้วย MockMvc เพื่อจำลองคำขอ HTTP โดยไม่ต้องเปิดเซิร์ฟเวอร์จริง คุณปฏิบัติ Spring Boot 4 Complete Guide ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Spring Boot 4 Complete Guide หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Spring Boot 4 Complete Guide บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน
บทเรียน “การทดสอบเว็บคอนโทรลเลอร์ด้วย MockMvc” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Spring Boot 4 Complete Guide นี้ได้ไหม
ได้ บทเรียน Spring Boot 4 Complete Guide ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การทดสอบหน่วยด้วย JUnit และ Mockito
- การทดสอบการผสานรวมด้วย Spring Boot
- การทดสอบแบบแยกส่วนและ TestContainers
- การทดสอบเว็บคอนโทรลเลอร์ด้วย MockMvc