IntelliJ IDEA 2017版 Spring5 java.lang.NoSuchMethodError: org.springframework.boot.SpringApplication.<init>([Ljava/lang/Object;)V

错误是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 }
View Code

  Pom.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 4     <modelVersion>4.0.0</modelVersion>
 5 
 6     <groupId>com.springboot</groupId>
 7     <artifactId>baseone</artifactId>
 8     <version>0.0.1-SNAPSHOT</version>
 9     <packaging>jar</packaging>
10 
11     <name>baseone</name>
12     <description>Demo project for Spring Boot</description>
13     <url>http://maven.apache.org</url>
14 
15     <parent>
16         <groupId>org.springframework.boot</groupId>
17         <artifactId>spring-boot-starter-parent</artifactId>
18         <version>2.0.1.RELEASE</version>
19         <relativePath/> <!-- lookup parent from repository -->
20     </parent>
21 
22     <properties>
23         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
24         <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
25         <java.version>1.8</java.version>
26     </properties>
27 
28     <dependencies>
29         <dependency>
30             <groupId>org.springframework</groupId>
31             <artifactId>spring-context</artifactId>
32             <version>5.0.5.RELEASE</version>
33         </dependency>
34         <dependency>
35             <groupId>org.springframework.boot</groupId>
36             <artifactId>spring-boot-starter-web</artifactId>
37         </dependency>
38 
39         <dependency>
40             <groupId>org.springframework.boot</groupId>
41             <artifactId>spring-boot-starter-test</artifactId>
42             <scope>test</scope>
43         </dependency>
44         <dependency>
45             <groupId>org.springframework.boot</groupId>
46             <artifactId>spring-boot-test</artifactId>
47             <version>2.0.1.RELEASE</version>
48             <scope>test</scope>
49         </dependency>
50         <dependency>
51             <groupId>org.springframework.boot</groupId>
52             <artifactId>spring-boot-test</artifactId>
53             <version>2.0.1.RELEASE</version>
54             <scope>test</scope>
55         </dependency>
56         <dependency>
57             <groupId>org.springframework.boot</groupId>
58             <artifactId>spring-boot-test</artifactId>
59             <version>2.0.1.RELEASE</version>
60             <scope>test</scope>
61         </dependency>
62     </dependencies>
63 
64     <build>
65         <plugins>
66             <plugin>
67                 <groupId>org.springframework.boot</groupId>
68                 <artifactId>spring-boot-maven-plugin</artifactId>
69             </plugin>
70         </plugins>
71     </build>
72 
73 
74 </project>
View Code
原文地址:https://www.cnblogs.com/liuyangfirst/p/9040187.html