1.编写HelloController
@RestController public class HelloController { @RequestMapping("hello") public String main() { return "hello word"; } }2.编写测试用例
@RunWith(SpringRunner.class) @SpringBootTest @AutoConfigureMockMvc public class HelloControllerTests { @Autowired private MockMvc mockMvc; @Test public void helloTest() throws Exception { mockMvc.perform(MockMvcRequestBuilders.get("/hello").accept(MediaType.APPLICATION_JSON)) .andExpect(status().isOk()) .andExpect(content().string(equalTo("hello word"))); } }注意:
添加@AutoConfigureMockMvc注解,否则MockMvc无法注入
修改gradle-wrapper.properties文件
#Tue Feb 06 12:27:20 CET 2018 distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists distributionUrl=https\://services.gradle.org/distributions/gradle-4.9.0-bin.zip主要修改最后一行,将其修改为需要的版本
然后在项目目录下,就可以使用gradlew build命令进行构建
gradlew build