<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.lijunyang</groupId><artifactId>YangYang</artifactId><version>1.0-SNAPSHOT</version><url>http://maven.apache.org</url><properties><springframework.version>4.3.12.RELEASE</springframework.version></properties><dependencies><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-core</artifactId><version>2.9.8</version></dependency><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId><version>2.9.5</version></dependency><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-annotations</artifactId><version>2.9.5</version></dependency><dependency><groupId>log4j</groupId><artifactId>log4j</artifactId><version>1.2.17</version></dependency><dependency><groupId>org.apache.logging.log4j</groupId><artifactId>log4j-api</artifactId><version>2.12.0</version></dependency><!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.26</version></dependency><dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>3.5.2</version></dependency><dependency><groupId>javax.servlet</groupId><artifactId>javax.servlet-api</artifactId><version>3.1.0</version><scope>provided</scope></dependency><dependency><groupId>redis.clients</groupId><artifactId>jedis</artifactId><version>3.1.0</version></dependency><dependency><groupId>commons-codec</groupId><artifactId>commons-codec</artifactId><version>1.3</version></dependency><!-- spring framework start --><dependency><groupId>org.springframework</groupId><artifactId>spring-core</artifactId><version>${springframework.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>4.3.12.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>4.2.4.RELEASE</version><scope>test</scope></dependency><!-- https://mvnrepository.com/artifact/org.springframework/spring-mock --><dependency><groupId>org.springframework</groupId><artifactId>spring-mock</artifactId><version>2.0.8</version><scope>test</scope></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-aop</artifactId><version>${springframework.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-beans</artifactId><version>${springframework.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>${springframework.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context-support</artifactId><version>${springframework.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-tx</artifactId><version>${springframework.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-web</artifactId><version>4.3.12.RELEASE</version></dependency><!-- spring framework end --><!-- email start --><dependency><groupId>com.icegreen</groupId><artifactId>greenmail</artifactId><version>1.3.1b</version><scope>test</scope></dependency><dependency><groupId>javax.mail</groupId><artifactId>mail</artifactId><version>1.4.1</version></dependency><!-- email end --><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.9</version><!-- <scope>test</scope> --></dependency><dependency><groupId>org.apache.maven.plugins</groupId><artifactId>maven-shade-plugin</artifactId><version>3.2.1</version></dependency><dependency><groupId>org.antlr</groupId><artifactId>antlr4-maven-plugin</artifactId><version>4.5.3</version></dependency><!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-log4j12 --><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-log4j12</artifactId><version>1.7.26</version><scope>test</scope></dependency><!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api --><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-api</artifactId><version>1.7.26</version></dependency><dependency><groupId>commons-fileupload</groupId><artifactId>commons-fileupload</artifactId><version>1.4</version></dependency><dependency><groupId>commons-io</groupId><artifactId>commons-io</artifactId><version>2.6</version></dependency></dependencies><build><plugins><!-- <plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-dependency-plugin</artifactId><executions><execution><id>copy-dependencies</id><phase>prepare-package</phase><goals><goal>copy-dependencies</goal></goals><configuration><outputDirectory>${project.build.directory}/test-classes/lib</outputDirectory><overWriteReleases>false</overWriteReleases><overWriteSnapshots>false</overWriteSnapshots><overWriteIfNewer>true</overWriteIfNewer></configuration></execution></executions></plugin><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-dependency-plugin</artifactId><executions><execution><id>copy-dependencies</id><phase>prepare-package</phase><goals><goal>copy-dependencies</goal></goals><configuration><outputDirectory>${project.build.directory}/classes/lib</outputDirectory><overWriteReleases>false</overWriteReleases><overWriteSnapshots>false</overWriteSnapshots><overWriteIfNewer>true</overWriteIfNewer></configuration></execution></executions></plugin> --><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>3.1</version><configuration><encoding>UTF-8</encoding><source>1.8</source><target>1.8</target></configuration></plugin><plugin><groupId>org.antlr</groupId><artifactId>antlr4-maven-plugin</artifactId><version>4.5.3</version><executions><execution><id>antlr</id><goals><goal>antlr4</goal></goals></execution></executions></plugin><!-- <plugin> mvn install 打包jar时使用此插件,可以生成一个可执行的jar文件,利用 | java -jar "fileName.jar" | 命令执行<groupId>org.apache.maven.plugins</groupId><artifactId>maven-shade-plugin</artifactId><version>3.2.1</version><executions><execution><phase>package</phase><goals><goal>shade</goal></goals><configuration><transformers><transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"><mainClass>com.lijunyang.app.App</mainClass></transformer></transformers></configuration></execution></executions></plugin> --></plugins><resources><resource><directory>src/main/java</directory><includes><include>**/*.xml</include></includes><!-- <filtering>true</filtering> --></resource><resource><directory>src/main/resources</directory><filtering>true</filtering></resource></resources><testResources><testResource><directory>src/main/resources</directory><filtering>true</filtering></testResource></testResources></build><name>YangYang</name><packaging>war</packaging></project>
junit web测试
package com.lijunyang.app;import org.junit.Test;import org.junit.Before;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.http.MediaType;import org.springframework.test.context.ContextConfiguration;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import org.springframework.test.context.web.WebAppConfiguration;import org.springframework.test.web.servlet.MockMvc;import org.springframework.test.web.servlet.ResultActions;import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;import org.springframework.test.web.servlet.result.MockMvcResultHandlers;import org.springframework.test.web.servlet.result.MockMvcResultMatchers;import org.springframework.test.web.servlet.setup.MockMvcBuilders;import org.springframework.web.context.WebApplicationContext;@RunWith(SpringJUnit4ClassRunner.class)@WebAppConfiguration(value = "src/main/webapp")@ContextConfiguration(locations={"classpath*:sqlMapConfig.xml","classpath*:springmvc-servlet.xml"})public class MockTestController{protected MockMvc mockMvc;@Autowiredprotected WebApplicationContext wac;@Before() //这个方法在每个方法执行之前都会执行一遍public void setup(){mockMvc = MockMvcBuilders.webAppContextSetup(wac).build(); //初始化MockMvc对象}@Testpublic void getAllCategoryTest() throws Exception{String responseString = mockMvc.perform(MockMvcRequestBuilders.post("/login") //请求的url,请求的方法是get.content("{ \"userName\": \"18979361605\", \"password\": \"1605\" }").contentType(MediaType.APPLICATION_JSON)//数据的格式).andExpect(MockMvcResultMatchers.status().isOk()) //返回的状态是200.andDo(MockMvcResultHandlers.print()) //打印出请求和相应的内容.andReturn().getResponse().getContentAsString(); //将相应的数据转换为字符串System.out.println("-----返回的json = " + responseString);}}
