错误是java.lang.NoSuchMethodError: org.springframework.boot.SpringApplication.<init>([Ljava/lang/Object;)V:
因为采用了测试方法,所以引用了
@RunWith(SpringRunner.class)
这个注释,和1.4.1版本和2.0.1版本的pom,导致版本冲突,将
@RunWith(SpringRunner.class)这个去除,版本统一成2.0.1样式,就正确了
1 package com.springboot; 2 3 import com.springboot.controller.Controller; 4 import org.junit.Before; 5 import org.junit.Test; 6 import org.junit.runner.RunWith; 7 import org.springframework.boot.test.context.SpringBootTest; 8 import org.springframework.http.MediaType; 9 import org.springframework.mock.web.MockServletContext; 10 import org.springframework.test.context.junit4.SpringRunner; 11 import org.springframework.test.context.web.WebAppConfiguration; 12 import org.springframework.test.web.servlet.MockMvc; 13 import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; 14 import org.springframework.test.web.servlet.setup.MockMvcBuilders; 15 import static org.hamcrest.Matchers.equalTo; 16 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; 17 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; 18 19 //@RunWith(SpringRunner.class) 20 @SpringBootTest(classes = MockServletContext.class) 21 @WebAppConfiguration 22 public class BaseOneApplicationTests { 23 24 25 26 private MockMvc mvc; 27 28 @Before 29 public void setUp() throws Exception { 30 mvc = MockMvcBuilders.standaloneSetup(new Controller()).build(); 31 } 32 33 34 /** 35 * 版本在2.0以下使用,否则会报错误 36 * @throws Exception 37 */ 38 @Test 39 public void getHello() throws Exception { 40 mvc.perform(MockMvcRequestBuilders.get("/hello").accept(MediaType.APPLICATION_JSON)) 41 .andExpect(status().isOk()) 42 .andExpect(content().string(equalTo("Hello World"))); 43 } 44 }