SpringBoot MockMvc单元测试的示例代码
为何使用mockmvc?
对模块进行集成测试时,希望能够通过输入url对controller进行测试,如果通过启动服务器,建立http client进行测试,这样会使得测试变得很麻烦,比如,启动速度慢,测试验证不方便,依赖网络环境等,所以为了可以对controller进行测试,我们引入了mockmvc。
mockmvc实现了对http请求的模拟,能够直接使用网络的形式,转换到controller的调用,这样可以使得测试速度快、不依赖网络环境,而且提供了一套验证的工具,这样可以使得请求的验证统一而且很方便。
mockmvcbuilder
mockmvc是spring测试下的一个非常好用的类,他们的初始化需要在setup中进行。
mockmvcbuilder是用来构造mockmvc的构造器,其主要有两个实现:standalonemockmvcbuilder和defaultmockmvcbuilder,前者继承了后者。
① mockmvcbuilders.webappcontextsetup(webapplicationcontext context):指定webapplicationcontext,将会从该上下文获取相应的控制器并得到相应的mockmvc;
② mockmvcbuilders.standalonesetup(object... controllers):通过参数指定一组控制器,这样就不需要从上下文获取了,比如this.mockmvc = mockmvcbuilders.standalonesetup(this.controller).build();
这些builder还提供了其他api,可以自行百度
1.添加测试依赖
<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/xsd/maven-4.0.0.xsd"> <modelversion>4.0.0</modelversion> <groupid>top.ytheng</groupid> <artifactid>springboot-demo</artifactid> <version>0.0.1-snapshot</version> <packaging>jar</packaging> <parent> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-parent</artifactid> <version>2.0.5.release</version> <relativepath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceencoding>utf-8</project.build.sourceencoding> <project.reporting.outputencoding>utf-8</project.reporting.outputencoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-web</artifactid> </dependency> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-test</artifactid> <scope>test</scope> </dependency> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-thymeleaf</artifactid> </dependency> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-devtools</artifactid> <optional>true</optional> <scope>true</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-maven-plugin</artifactid> </plugin> </plugins> </build> </project>
2.添加mocmvc测试类
package top.ytheng.demo; import org.junit.test; import org.junit.runner.runwith; import org.springframework.beans.factory.annotation.autowired; import org.springframework.boot.test.autoconfigure.web.servlet.autoconfiguremockmvc; import org.springframework.boot.test.context.springboottest; import org.springframework.test.context.junit4.springrunner; import org.springframework.test.web.servlet.mockmvc; import org.springframework.test.web.servlet.mvcresult; import org.springframework.test.web.servlet.request.mockmvcrequestbuilders; import org.springframework.test.web.servlet.result.mockmvcresultmatchers; //底层用junit springjunit4classrunner @runwith(springrunner.class) //启动整个springboot工程 @springboottest(classes= {demoapplication.class}) //鼠标选中springboottestdemo后执行run as -> junit test可以同时执行多个测试 @autoconfiguremockmvc public class mockmvctestdemo { @autowired private mockmvc mockmvc; @test public void mocktest() throws exception { mvcresult mockresult = mockmvc.perform(mockmvcrequestbuilders.get("/file/testpath")). andexpect(mockmvcresultmatchers.status().isok()).andreturn(); int status = mockresult.getresponse().getstatus(); system.out.println(status); } }
3.添加测试控制器
package top.ytheng.demo.controller; import org.springframework.stereotype.controller; import org.springframework.web.bind.annotation.requestmapping; import org.springframework.web.bind.annotation.responsebody; @controller @requestmapping("/file") public class filecontroller { @requestmapping("/testpath") @responsebody private object testpath() { return filepath; } }
4.添加启动类
package top.ytheng.demo; import org.springframework.boot.springapplication; import org.springframework.boot.autoconfigure.springbootapplication; @springbootapplication //等于下面3个 //@springbootconfiguration //@enableautoconfiguration //@componentscan public class demoapplication { public static void main(string[] args) { springapplication.run(demoapplication.class, args); } }
5.鼠标选中mockmvctestdemo类名,使用快捷键启动junit测试(shift + alt + x,t),进行测试
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。