Implementing Spring MVC Test Infrastructure in XXL-Job
This abstract test class establishes the foundation for Spring MVC controller testing in the XXL-Job admin module. It configures MockMvc with a web application context and provides common test infrastructure for all controller test cases.
Test Coverage Overview
Implementation Analysis
Technical Details
Best Practices Demonstrated
xuxueli/xxl-job
xxl-job-admin/src/test/java/com/xxl/job/admin/controller/AbstractSpringMvcTest.java
package com.xxl.job.admin.controller;
import org.junit.jupiter.api.BeforeEach;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class AbstractSpringMvcTest {
@Autowired
private WebApplicationContext applicationContext;
protected MockMvc mockMvc;
@BeforeEach
public void setup() {
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.applicationContext).build();
}
}