spring boot单元测试之十四:controller使用html模板时做单元测试(spring boot 2.4.4)
一,演示项目的相关信息
1,地址:
https://github.com/liuhongdi/modelviewtest
2,功能说明:演示controller使用thymeleaf模板而不是返回json时的单元测试
3,项目结构:如图:
说明:刘宏缔的架构森林是一个专注架构的博客,
网站:https://blog.imgtouch.com
本文: https://blog.imgtouch.com/index.php/2023/05/27/spring-boot-dan-yuan-ce-shi-zhi-shi-si-controller-shi-yong/
对应的源码可以访问这里获取: https://github.com/liuhongdi/
说明:作者:刘宏缔 邮箱: 371125307@qq.com
二,配置文件说明
1,pom.xml:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--thymeleaf begin--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <!--thymeleaf end--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency>
2,application.yml
#error server: error: include-stacktrace: always #errorlog logging: level: org: springframework: web: trace #thymeleaf spring: thymeleaf: cache: false encoding: UTF-8 mode: HTML prefix: classpath:/templates/ suffix: .html
三,java代码说明
1,controller/HomeController.java
@Controller @RequestMapping("/home") public class HomeController { //返回单个商品 @GetMapping("/one") public String one(Model model) { Goods goods31 = new Goods(); goods31.setGoodsId(31L); goods31.setGoodsName("纯色真丝睡袍"); //把list传递给模板 model.addAttribute("goodsOne",goods31); return "home/one.html"; } //返回商品列表 @GetMapping("/list") public String list(Model model) { ArrayList<Goods> listGoods1 = new ArrayList<Goods>(); Goods goods1 = new Goods(); goods1.setGoodsId(1L); goods1.setGoodsName("无线智能感应灯"); listGoods1.add(goods1); Goods goods2 = new Goods(); goods2.setGoodsId(2L); goods2.setGoodsName("朱之光落地灯"); listGoods1.add(goods2); Goods goods3 = new Goods(); goods3.setGoodsId(3L); goods3.setGoodsName("儿童抗首菌枕头"); listGoods1.add(goods3); //把list传递给模板 model.addAttribute("list",listGoods1); return "home/list.html"; } }
2,pojo/Goods.java
//商品模型 public class Goods { //商品id Long goodsId; public Long getGoodsId() { return this.goodsId; } public void setGoodsId(Long goodsId) { this.goodsId = goodsId; } //商品名称 private String goodsName; public String getGoodsName() { return this.goodsName; } public void setGoodsName(String goodsName) { this.goodsName = goodsName; } //商品标题 private String subject; public String getSubject() { return this.subject; } public void setSubject(String subject) { this.subject = subject; } //商品价格 private BigDecimal price; public BigDecimal getPrice() { return this.price; } public void setPrice(BigDecimal price) { this.price = price; } //库存 int stock; public int getStock() { return this.stock; } public void setStock(int stock) { this.stock = stock; } //打印 @Override public String toString(){ return " Goods:goodsId=" + goodsId +" goodsName=" + goodsName+" subject=" + subject+" price=" + price+" stock=" + stock; } }
3,controller/HomeControllerTest.java
@AutoConfigureMockMvc @SpringBootTest class HomeControllerTest { @Autowired private MockMvc mockMvc; @Test @DisplayName("测试显示单个商品") void one() throws Exception { ModelAndView mv=mockMvc.perform(get("/home/one")) .andExpect(status().isOk()) .andReturn().getModelAndView(); //view String viewName=mv.getViewName(); assertThat(viewName,equalTo("home/one.html")); //model Goods goods = (Goods)mv.getModel().get("goodsOne"); assertThat(goods.getGoodsId(),equalTo(31L)); assertThat(goods.getGoodsName(),equalTo("纯色真丝睡袍")); } @Test @DisplayName("测试显示商品列表") void list() throws Exception { ModelAndView mv=mockMvc.perform(get("/home/list")) .andExpect(status().isOk()) .andReturn().getModelAndView(); //view String viewName=mv.getViewName(); assertThat(viewName,equalTo("home/list.html")); Map<String, Object> model = mv.getModel(); ArrayList<Goods> list = (ArrayList) model.get("list"); //检查列表数量 assertThat(list.size(),greaterThan(0)); //检查第一个商品的id Goods one = list.get(0); assertThat(one.getGoodsId(),greaterThan(0L)); } }
4,templates/home/one.html
<!DOCTYPE html> <html lang="en"> <head> </head> <body> <div style="width:100%;height:30px;background:#ffffff;font-size: 16px;" ></div> <div id="content" style="width:1040px;"> <div style="width:790px;float:left;margin-left:30px;"> <!--main begin--> <!--显示分类下的商品列表信息--> <div style="width:790px;height:100px;"> <div style="width:250px;height:100px;float: left;background:#eeeeee;margin-left:10px;margin-top:10px;"> <div>商品ID:[[${goodsOne.goodsId}]]</div> <div>商品名称:[[${goodsOne.goodsName}]]</div> </div> </div> <!--main end--> </div> </div> </body> </html>
5,templates/home/list.html
<!DOCTYPE html> <html lang="en"> <head> </head> <body> <div style="width:100%;height:30px;background:#ffffff;font-size: 16px;" ></div> <div id="content" style="width:1040px;"> <div style="width:790px;float:left;margin-left:30px;"> <!--main begin--> <div style="width:790px;height:100px;"> <div th:each="goodsOne:${list}" style="width:250px;height:100px;float: left;background:#eeeeee;margin-left:10px;margin-top:10px;"> <div>商品ID:[[${goodsOne.goodsId}]]</div> <div>商品名称:[[${goodsOne.goodsName}]]</div> </div> </div> <!--main end--> </div> </div> </body> </html>
四,测试效果
1,访问url:
http://127.0.0.1:8080/home/one
返回:
访问:
http://127.0.0.1:8080/home/list
返回:
2,运行单元测试:
五,查看spring boot的版本:
. ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.4.4)