SpringBoot对Controller进行单元测试的实现代码 附乱码解决方案
程序员文章站
2024-01-08 16:57:58
controller代码package com.keafmd.controller;import org.springframework.web.bind.annotation.requestmapp...
controller代码
package com.keafmd.controller; import org.springframework.web.bind.annotation.requestmapping; import org.springframework.web.bind.annotation.restcontroller; import java.util.hashmap; import java.util.map; /** * keafmd * * @classname: hellocontroller * @description: * @author: 牛哄哄的柯南 * @date: 2021-04-02 9:42 * @blog: https://keafmd.blog.csdn.net/ */ @restcontroller public class hellocontroller { @requestmapping("/hello") map hello(){ map map = new hashmap(); map.put("keafmd","牛哄哄的柯南"); map.put("success",true); return map; } }
单元测试代码
package com.keafmd; import com.keafmd.springboot02application; import com.keafmd.controller.hellocontroller; import org.junit.jupiter.api.beforeeach; import org.junit.jupiter.api.test; 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.http.mediatype; import org.springframework.test.context.contextconfiguration; import org.springframework.test.context.web.webappconfiguration; 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.mockmvcresulthandlers; import org.springframework.test.web.servlet.result.mockmvcresultmatchers; import org.springframework.test.web.servlet.setup.mockmvcbuilders; import org.springframework.web.context.webapplicationcontext; /** * keafmd * * @classname: mvctest * @description: * @author: 牛哄哄的柯南 * @date: 2021-04-02 10:59 * @blog: https://keafmd.blog.csdn.net/ */ @springboottest(classes = springboot02application.class) @autoconfiguremockmvc //相当于是使用 context 上下文构造一个 mvc对象 public class mvctest { //模拟访问 controller @autowired mockmvc mvc; @test public void test() throws exception { mvcresult result = mvc.perform( mockmvcrequestbuilders.get("/hello"). accept(mediatype.application_json)). andexpect(mockmvcresultmatchers.status().isok()). anddo(mockmvcresulthandlers.print()).andreturn(); } }
测试结果
乱码解决
把注解替换为:↓@requestmapping(value = "/hello",produces = {"application/json;charset=utf-8"})
hellocontroller:
package com.keafmd.controller; import org.springframework.web.bind.annotation.getmapping; import org.springframework.web.bind.annotation.postmapping; import org.springframework.web.bind.annotation.requestmapping; import org.springframework.web.bind.annotation.restcontroller; import java.util.hashmap; import java.util.map; /** * keafmd * * @classname: hellocontroller * @description: * @author: 牛哄哄的柯南 * @date: 2021-04-02 9:42 * @blog: https://keafmd.blog.csdn.net/ */ @restcontroller public class hellocontroller { @requestmapping(value = "/hello",produces = {"application/json;charset=utf-8"}) //@requestmapping("/hello") map hello(){ map map = new hashmap(); map.put("keafmd","牛哄哄的柯南"); map.put("success",true); return map; } }
解决乱码后的效果:
到此这篇关于springboot对controller进行单元测试的实现代码 附乱码解决方案的文章就介绍到这了,更多相关springboot controller单元测试内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!