การทดสอบชั้นเว็บด้วย MockMvc
ทดสอบคอนโทรลเลอร์ด้วย @WebMvcTest
การทดสอบชั้นเว็บด้วย MockMvc เป็นบทเรียน Spring Boot 4 Microservices & REST APIs ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Spring Boot 4 Microservices & REST APIs และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Spring Boot 4 Microservices & REST APIs มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Testing the Web Layer
Web layer tests verify your controllers — request mapping, validation, serialization, and status codes — without starting a full server.
Spring Boot provides @WebMvcTest and MockMvc for this.
@WebMvcTest
@WebMvcTest loads only the web slice: controllers, filters, and converters. Services and repositories are not loaded — you mock them.
@WebMvcTest(UserController.class)
class UserControllerTest {
@Autowired
MockMvc mockMvc;
@MockBean
UserService userService;
}MockMvc.perform
MockMvc simulates HTTP requests against your controller without a network.
mockMvc.perform(get("/users/1"))
.andExpect(status().isOk());Mocking the Service
Use @MockBean to replace the service in the context, then stub it with Mockito.
@Test
void returnsUser() throws Exception {
when(userService.getById("1"))
.thenReturn(new User("1", "Alice"));
mockMvc.perform(get("/users/1"))
.andExpect(status().isOk());
}Asserting JSON with jsonPath
jsonPath verifies fields in the JSON response body.
mockMvc.perform(get("/users/1"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.name").value("Alice"))
.andExpect(jsonPath("$.id").value("1"));Testing POST Requests
Send a JSON body with content and contentType.
mockMvc.perform(post("/users")
.contentType(MediaType.APPLICATION_JSON)
.content("{\"name\":\"Bob\"}"))
.andExpect(status().isCreated());Asserting Error Statuses
Verify 404 or 400 responses for missing resources or invalid input.
when(userService.getById("99"))
.thenThrow(new NotFoundException());
mockMvc.perform(get("/users/99"))
.andExpect(status().isNotFound());Testing Validation
When a request fails bean validation, expect a 400 Bad Request.
mockMvc.perform(post("/users")
.contentType(MediaType.APPLICATION_JSON)
.content("{\"name\":\"\"}"))
.andExpect(status().isBadRequest());Checking Headers
Assert response headers such as Location after a create.
mockMvc.perform(post("/users")
.contentType(MediaType.APPLICATION_JSON)
.content("{\"name\":\"Bob\"}"))
.andExpect(header().string("Location",
"/users/2"));Query Parameters
Add request parameters with param.
mockMvc.perform(get("/users")
.param("name", "Alice"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.length()").value(1));Printing the Result
Use andDo(print()) to log the full request and response while debugging.
mockMvc.perform(get("/users/1"))
.andDo(print())
.andExpect(status().isOk());Quick Check
Test your understanding of MockMvc.
Recap
You learned to test controllers in isolation:
@WebMvcTestloads only the web sliceMockMvc.performsimulates requests@MockBeanreplaces servicesjsonPath,status(),header()for assertions
เรียนรู้ Java ด้วย AI tutor — ฟรี
เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป
- คอร์ส
- 24
- บทเรียน
- 93
คำถามที่พบบ่อย
บทเรียน “การทดสอบชั้นเว็บด้วย MockMvc” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การทดสอบชั้นเว็บด้วย MockMvc” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Spring Boot 4 Microservices & REST APIs ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Spring Boot 4 Microservices & REST APIs มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การทดสอบชั้นเว็บด้วย MockMvc”
ทดสอบคอนโทรลเลอร์ด้วย @WebMvcTest คุณปฏิบัติ Spring Boot 4 Microservices & REST APIs ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Spring Boot 4 Microservices & REST APIs หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Spring Boot 4 Microservices & REST APIs บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน
บทเรียน “การทดสอบชั้นเว็บด้วย MockMvc” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Spring Boot 4 Microservices & REST APIs นี้ได้ไหม
ได้ บทเรียน Spring Boot 4 Microservices & REST APIs ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การทดสอบหน่วยด้วย JUnit และ Mockito
- การทดสอบชั้นเว็บด้วย MockMvc
- การทดสอบการผสานรวมด้วย @SpringBootTest
- ส่วนพึ่งพาจริงด้วย Testcontainers