1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    3. <modelVersion>4.0.0</modelVersion>
    4. <groupId>com.lijunyang</groupId>
    5. <artifactId>YangYang</artifactId>
    6. <version>1.0-SNAPSHOT</version>
    7. <url>http://maven.apache.org</url>
    8. <properties>
    9. <springframework.version>4.3.12.RELEASE</springframework.version>
    10. </properties>
    11. <dependencies>
    12. <dependency>
    13. <groupId>com.fasterxml.jackson.core</groupId>
    14. <artifactId>jackson-core</artifactId>
    15. <version>2.9.8</version>
    16. </dependency>
    17. <dependency>
    18. <groupId>com.fasterxml.jackson.core</groupId>
    19. <artifactId>jackson-databind</artifactId>
    20. <version>2.9.5</version>
    21. </dependency>
    22. <dependency>
    23. <groupId>com.fasterxml.jackson.core</groupId>
    24. <artifactId>jackson-annotations</artifactId>
    25. <version>2.9.5</version>
    26. </dependency>
    27. <dependency>
    28. <groupId>log4j</groupId>
    29. <artifactId>log4j</artifactId>
    30. <version>1.2.17</version>
    31. </dependency>
    32. <dependency>
    33. <groupId>org.apache.logging.log4j</groupId>
    34. <artifactId>log4j-api</artifactId>
    35. <version>2.12.0</version>
    36. </dependency>
    37. <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
    38. <dependency>
    39. <groupId>mysql</groupId>
    40. <artifactId>mysql-connector-java</artifactId>
    41. <version>5.1.26</version>
    42. </dependency>
    43. <dependency>
    44. <groupId>org.mybatis</groupId>
    45. <artifactId>mybatis</artifactId>
    46. <version>3.5.2</version>
    47. </dependency>
    48. <dependency>
    49. <groupId>javax.servlet</groupId>
    50. <artifactId>javax.servlet-api</artifactId>
    51. <version>3.1.0</version>
    52. <scope>provided</scope>
    53. </dependency>
    54. <dependency>
    55. <groupId>redis.clients</groupId>
    56. <artifactId>jedis</artifactId>
    57. <version>3.1.0</version>
    58. </dependency>
    59. <dependency>
    60. <groupId>commons-codec</groupId>
    61. <artifactId>commons-codec</artifactId>
    62. <version>1.3</version>
    63. </dependency>
    64. <!-- spring framework start -->
    65. <dependency>
    66. <groupId>org.springframework</groupId>
    67. <artifactId>spring-core</artifactId>
    68. <version>${springframework.version}</version>
    69. </dependency>
    70. <dependency>
    71. <groupId>org.springframework</groupId>
    72. <artifactId>spring-webmvc</artifactId>
    73. <version>4.3.12.RELEASE</version>
    74. </dependency>
    75. <dependency>
    76. <groupId>org.springframework</groupId>
    77. <artifactId>spring-test</artifactId>
    78. <version>4.2.4.RELEASE</version>
    79. <scope>test</scope>
    80. </dependency>
    81. <!-- https://mvnrepository.com/artifact/org.springframework/spring-mock -->
    82. <dependency>
    83. <groupId>org.springframework</groupId>
    84. <artifactId>spring-mock</artifactId>
    85. <version>2.0.8</version>
    86. <scope>test</scope>
    87. </dependency>
    88. <dependency>
    89. <groupId>org.springframework</groupId>
    90. <artifactId>spring-aop</artifactId>
    91. <version>${springframework.version}</version>
    92. </dependency>
    93. <dependency>
    94. <groupId>org.springframework</groupId>
    95. <artifactId>spring-beans</artifactId>
    96. <version>${springframework.version}</version>
    97. </dependency>
    98. <dependency>
    99. <groupId>org.springframework</groupId>
    100. <artifactId>spring-context</artifactId>
    101. <version>${springframework.version}</version>
    102. </dependency>
    103. <dependency>
    104. <groupId>org.springframework</groupId>
    105. <artifactId>spring-context-support</artifactId>
    106. <version>${springframework.version}</version>
    107. </dependency>
    108. <dependency>
    109. <groupId>org.springframework</groupId>
    110. <artifactId>spring-tx</artifactId>
    111. <version>${springframework.version}</version>
    112. </dependency>
    113. <dependency>
    114. <groupId>org.springframework</groupId>
    115. <artifactId>spring-web</artifactId>
    116. <version>4.3.12.RELEASE</version>
    117. </dependency>
    118. <!-- spring framework end -->
    119. <!-- email start -->
    120. <dependency>
    121. <groupId>com.icegreen</groupId>
    122. <artifactId>greenmail</artifactId>
    123. <version>1.3.1b</version>
    124. <scope>test</scope>
    125. </dependency>
    126. <dependency>
    127. <groupId>javax.mail</groupId>
    128. <artifactId>mail</artifactId>
    129. <version>1.4.1</version>
    130. </dependency>
    131. <!-- email end -->
    132. <dependency>
    133. <groupId>junit</groupId>
    134. <artifactId>junit</artifactId>
    135. <version>4.9</version>
    136. <!-- <scope>test</scope> -->
    137. </dependency>
    138. <dependency>
    139. <groupId>org.apache.maven.plugins</groupId>
    140. <artifactId>maven-shade-plugin</artifactId>
    141. <version>3.2.1</version>
    142. </dependency>
    143. <dependency>
    144. <groupId>org.antlr</groupId>
    145. <artifactId>antlr4-maven-plugin</artifactId>
    146. <version>4.5.3</version>
    147. </dependency>
    148. <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-log4j12 -->
    149. <dependency>
    150. <groupId>org.slf4j</groupId>
    151. <artifactId>slf4j-log4j12</artifactId>
    152. <version>1.7.26</version>
    153. <scope>test</scope>
    154. </dependency>
    155. <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
    156. <dependency>
    157. <groupId>org.slf4j</groupId>
    158. <artifactId>slf4j-api</artifactId>
    159. <version>1.7.26</version>
    160. </dependency>
    161. <dependency>
    162. <groupId>commons-fileupload</groupId>
    163. <artifactId>commons-fileupload</artifactId>
    164. <version>1.4</version>
    165. </dependency>
    166. <dependency>
    167. <groupId>commons-io</groupId>
    168. <artifactId>commons-io</artifactId>
    169. <version>2.6</version>
    170. </dependency>
    171. </dependencies>
    172. <build>
    173. <plugins>
    174. <!-- <plugin>
    175. <groupId>org.apache.maven.plugins</groupId>
    176. <artifactId>maven-dependency-plugin</artifactId>
    177. <executions>
    178. <execution>
    179. <id>copy-dependencies</id>
    180. <phase>prepare-package</phase>
    181. <goals>
    182. <goal>copy-dependencies</goal>
    183. </goals>
    184. <configuration>
    185. <outputDirectory>${project.build.directory}/test-classes/lib</outputDirectory>
    186. <overWriteReleases>false</overWriteReleases>
    187. <overWriteSnapshots>false</overWriteSnapshots>
    188. <overWriteIfNewer>true</overWriteIfNewer>
    189. </configuration>
    190. </execution>
    191. </executions>
    192. </plugin>
    193. <plugin>
    194. <groupId>org.apache.maven.plugins</groupId>
    195. <artifactId>maven-dependency-plugin</artifactId>
    196. <executions>
    197. <execution>
    198. <id>copy-dependencies</id>
    199. <phase>prepare-package</phase>
    200. <goals>
    201. <goal>copy-dependencies</goal>
    202. </goals>
    203. <configuration>
    204. <outputDirectory>${project.build.directory}/classes/lib</outputDirectory>
    205. <overWriteReleases>false</overWriteReleases>
    206. <overWriteSnapshots>false</overWriteSnapshots>
    207. <overWriteIfNewer>true</overWriteIfNewer>
    208. </configuration>
    209. </execution>
    210. </executions>
    211. </plugin> -->
    212. <plugin>
    213. <groupId>org.apache.maven.plugins</groupId>
    214. <artifactId>maven-compiler-plugin</artifactId>
    215. <version>3.1</version>
    216. <configuration>
    217. <encoding>UTF-8</encoding>
    218. <source>1.8</source>
    219. <target>1.8</target>
    220. </configuration>
    221. </plugin>
    222. <plugin>
    223. <groupId>org.antlr</groupId>
    224. <artifactId>antlr4-maven-plugin</artifactId>
    225. <version>4.5.3</version>
    226. <executions>
    227. <execution>
    228. <id>antlr</id>
    229. <goals>
    230. <goal>antlr4</goal>
    231. </goals>
    232. </execution>
    233. </executions>
    234. </plugin>
    235. <!-- <plugin> mvn install 打包jar时使用此插件,可以生成一个可执行的jar文件,利用 | java -jar "fileName.jar" | 命令执行
    236. <groupId>org.apache.maven.plugins</groupId>
    237. <artifactId>maven-shade-plugin</artifactId>
    238. <version>3.2.1</version>
    239. <executions>
    240. <execution>
    241. <phase>package</phase>
    242. <goals><goal>shade</goal></goals>
    243. <configuration>
    244. <transformers>
    245. <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
    246. <mainClass>com.lijunyang.app.App</mainClass>
    247. </transformer>
    248. </transformers>
    249. </configuration>
    250. </execution>
    251. </executions>
    252. </plugin> -->
    253. </plugins>
    254. <resources>
    255. <resource>
    256. <directory>src/main/java</directory>
    257. <includes>
    258. <include>**/*.xml</include>
    259. </includes>
    260. <!-- <filtering>true</filtering> -->
    261. </resource>
    262. <resource>
    263. <directory>src/main/resources</directory>
    264. <filtering>true</filtering>
    265. </resource>
    266. </resources>
    267. <testResources>
    268. <testResource>
    269. <directory>src/main/resources</directory>
    270. <filtering>true</filtering>
    271. </testResource>
    272. </testResources>
    273. </build>
    274. <name>YangYang</name>
    275. <packaging>war</packaging>
    276. </project>

    junit web测试

    1. package com.lijunyang.app;
    2. import org.junit.Test;
    3. import org.junit.Before;
    4. import org.junit.runner.RunWith;
    5. import org.springframework.beans.factory.annotation.Autowired;
    6. import org.springframework.http.MediaType;
    7. import org.springframework.test.context.ContextConfiguration;
    8. import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
    9. import org.springframework.test.context.web.WebAppConfiguration;
    10. import org.springframework.test.web.servlet.MockMvc;
    11. import org.springframework.test.web.servlet.ResultActions;
    12. import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
    13. import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
    14. import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
    15. import org.springframework.test.web.servlet.setup.MockMvcBuilders;
    16. import org.springframework.web.context.WebApplicationContext;
    17. @RunWith(SpringJUnit4ClassRunner.class)
    18. @WebAppConfiguration(value = "src/main/webapp")
    19. @ContextConfiguration(locations={"classpath*:sqlMapConfig.xml","classpath*:springmvc-servlet.xml"})
    20. public class MockTestController
    21. {
    22. protected MockMvc mockMvc;
    23. @Autowired
    24. protected WebApplicationContext wac;
    25. @Before() //这个方法在每个方法执行之前都会执行一遍
    26. public void setup()
    27. {
    28. mockMvc = MockMvcBuilders.webAppContextSetup(wac).build(); //初始化MockMvc对象
    29. }
    30. @Test
    31. public void getAllCategoryTest() throws Exception
    32. {
    33. String responseString = mockMvc.perform
    34. (
    35. MockMvcRequestBuilders.post("/login") //请求的url,请求的方法是get
    36. .content("{ \"userName\": \"18979361605\", \"password\": \"1605\" }")
    37. .contentType(MediaType.APPLICATION_JSON)//数据的格式
    38. )
    39. .andExpect(MockMvcResultMatchers.status().isOk()) //返回的状态是200
    40. .andDo(MockMvcResultHandlers.print()) //打印出请求和相应的内容
    41. .andReturn()
    42. .getResponse()
    43. .getContentAsString(); //将相应的数据转换为字符串
    44. System.out.println("-----返回的json = " + responseString);
    45. }
    46. }