当前位置: 首页 > news >正文

专业网站建设设计装饰/保定网站建设方案优化

专业网站建设设计装饰,保定网站建设方案优化,工业设计产品设计案例,网站对接qq群 虚拟主机http://blog.csdn.net/sosfnima/article/details/51993700 出于对作者的尊重和感谢,原文地址为 http://blog.csdn.net/lxhjh/article/details/51769968 spring Boot的测试,和普通项目的测试类同,可以继续使用我们熟悉的测试工具。当然&#…

http://blog.csdn.net/sosfnima/article/details/51993700

出于对作者的尊重和感谢,原文地址为 http://blog.csdn.net/lxhjh/article/details/51769968


spring Boot的测试,和普通项目的测试类同,可以继续使用我们熟悉的测试工具。当然,这里的测试,主要还是后台代码的测试。

主要需要注意的地方仅有三点:

1、依赖包的引入:pom.xml中仅依赖spring-boot-starter-test,它把相关的依赖全部引入。

2、在测试类上的注解,常用的注解有三个

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
@WebAppConfiguration

这三个注解,只要注意第二个注解括号内的Application.class就行,把它替换为项目的启动类即可。

我们前面spring-boot-sample-MySQL工程的启动类为SpringBootSampleMysqlApplication,那么测试类上的注解就是

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = SpringBootSampleMysqlApplication.class)
@WebAppConfiguration

3、测试类的文件结构,保持src/test/Java和src/main/java结构一直,即:包+文件夹。

     如:com.example包service中类的测试,那么在src/test/java也是建立com.example包,再在包下建立文件夹service.


注:由于我们测试的启动类用的是项目的启动类,所以Spring Boot项目的测试配置文件任然用src/main/resources的。

下面贴两个个测试类代码

1、服务类的

[java] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. package com.example.service;  
  2.   
  3. import java.util.List;  
  4.   
  5. import org.junit.Assert;  
  6. import org.junit.Test;  
  7. import org.junit.runner.RunWith;  
  8. import org.springframework.beans.factory.annotation.Autowired;  
  9. import org.springframework.boot.test.SpringApplicationConfiguration;  
  10. import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;  
  11. import org.springframework.test.context.web.WebAppConfiguration;  
  12.   
  13. import com.example.Application;  
  14. import com.example.domain.TestPOJO;  
  15. import com.example.dto.HotelDto;  
  16.   
  17. @RunWith(SpringJUnit4ClassRunner.class)  
  18. @SpringApplicationConfiguration(classes = Application.class)  
  19. @WebAppConfiguration  
  20. public class TestServicesTest {  
  21.   
  22.     @Autowired  
  23.     TestServices testServices;  
  24.       
  25.     @Test  
  26.     public void testShow() {  
  27.         String expectedResult="hello world!";  
  28.         String result=testServices.show();  
  29.         Assert.assertTrue("数据一致", expectedResult.equals(result));  
  30.         Assert.assertFalse("数据不一致", !expectedResult.equals(result));  
  31.     }  
  32.   
  33.     @Test  
  34.     public void testShowDao() {  
  35.         List<TestPOJO> testPOJOList=testServices.showDao(10);  
  36.         Assert.assertTrue("数据集不对", testPOJOList.size()==1);  
  37.         Assert.assertTrue("数据一致", testPOJOList.get(0).getName().equals("nice"));  
  38.     }  
  39.   
  40.     @Test  
  41.     public void testFindByCountry() {  
  42.         List<HotelDto> testPOJOList=testServices.findByCountry("US");  
  43.         Assert.assertTrue("数据集不对", testPOJOList.size()==1);  
  44.         Assert.assertTrue("数据一致", testPOJOList.get(0).getCityName().equals("San Francisco"));  
  45.     }  
  46.   
  47. }  
2、controller类的

[java] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. package com.example.controller;  
  2.   
  3. import java.util.List;  
  4.   
  5. import org.junit.Assert;  
  6. import org.junit.Before;  
  7. import org.junit.Test;  
  8. import org.junit.runner.RunWith;  
  9. import org.springframework.beans.factory.annotation.Autowired;  
  10. import org.springframework.boot.test.SpringApplicationConfiguration;  
  11. import org.springframework.http.MediaType;  
  12. import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;  
  13. import org.springframework.test.context.web.WebAppConfiguration;  
  14. import org.springframework.test.web.servlet.MockMvc;  
  15. import org.springframework.test.web.servlet.MvcResult;  
  16. import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;  
  17. import org.springframework.test.web.servlet.setup.MockMvcBuilders;  
  18. import org.springframework.web.context.WebApplicationContext;  
  19.   
  20. import com.example.Application;  
  21. import com.example.domain.TestPOJO;  
  22. import com.example.dto.HotelDto;  
  23. import com.example.service.TestServices;  
  24. import com.fasterxml.jackson.core.JsonProcessingException;  
  25. import com.fasterxml.jackson.databind.ObjectMapper;  
  26.   
  27. @RunWith(SpringJUnit4ClassRunner.class)  
  28. @SpringApplicationConfiguration(classes = Application.class)  
  29. @WebAppConfiguration  
  30. public class TestControllerTest {  
  31.   
  32.     MockMvc mvc;  
  33.   
  34.     @Autowired  
  35.     WebApplicationContext webApplicationConnect;  
  36.   
  37.     @Autowired  
  38.     TestServices testServices;  
  39.   
  40.     String expectedJson;  
  41.   
  42.     @Before  
  43.     public void setUp() throws JsonProcessingException {  
  44.         mvc = MockMvcBuilders.webAppContextSetup(webApplicationConnect).build();  
  45.   
  46.     }  
  47.   
  48.     @Test  
  49.     public void testShow() throws Exception {  
  50.         String expectedResult = "hello world!";  
  51.         String uri = "/show";  
  52.         MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.get(uri).accept(MediaType.APPLICATION_JSON))  
  53.                 .andReturn();  
  54.         int status = mvcResult.getResponse().getStatus();  
  55.         String content = mvcResult.getResponse().getContentAsString();  
  56.   
  57.         Assert.assertTrue("错误,正确的返回值为200", status == 200);  
  58.         Assert.assertFalse("错误,正确的返回值为200", status != 200);  
  59.         Assert.assertTrue("数据一致", expectedResult.equals(content));  
  60.         Assert.assertFalse("数据不一致", !expectedResult.equals(content));  
  61.     }  
  62.   
  63.     protected String Obj2Json(Object obj) throws JsonProcessingException {  
  64.         ObjectMapper mapper=new ObjectMapper();  
  65.         return mapper.writeValueAsString(obj);  
  66.     }  
  67.   
  68.     @Test  
  69.     public void testShowDaoInt() throws Exception {  
  70.         List<TestPOJO> testPOJOList = testServices.showDao(10);  
  71.         expectedJson = Obj2Json(testPOJOList);  
  72.   
  73.         String uri="/showDao?age=10";  
  74.         MvcResult mvcResult=mvc.perform(MockMvcRequestBuilders.get(uri).accept(MediaType.APPLICATION_JSON)).andReturn();  
  75.         int status=mvcResult.getResponse().getStatus();  
  76.         String content=mvcResult.getResponse().getContentAsString();  
  77.           
  78.         Assert.assertTrue("错误,正确的返回值为200", status == 200);  
  79.         Assert.assertFalse("错误,正确的返回值为200", status != 200);  
  80.         Assert.assertTrue("数据一致", expectedJson.equals(content));  
  81.         Assert.assertFalse("数据不一致", !expectedJson.equals(content));  
  82.     }  
  83.   
  84.     @Test  
  85.     public void testShowDaoString() throws Exception {  
  86.         List<HotelDto> hotelDtoList=testServices.findByCountry("US");  
  87.         expectedJson = Obj2Json(hotelDtoList);  
  88.           
  89.         String uri="/country/US";  
  90.         MvcResult mvcResult=mvc.perform(MockMvcRequestBuilders.get(uri).accept(MediaType.APPLICATION_JSON)).andReturn();  
  91.         int status=mvcResult.getResponse().getStatus();  
  92.         String content=mvcResult.getResponse().getContentAsString();  
  93.           
  94.         Assert.assertTrue("错误,正确的返回值为200", status == 200);  
  95.         Assert.assertFalse("错误,正确的返回值为200", status != 200);  
  96.         Assert.assertTrue("数据一致", expectedJson.equals(content));  
  97.         Assert.assertFalse("数据不一致", !expectedJson.equals(content));  
  98.     }  
  99.   
  100. }  
controller类的,为了MockMvc,注入了WebApplicationContext。
http://www.lbrq.cn/news/930061.html

相关文章:

  • 国外哪些做问卷的网站/it培训学校
  • 自媒体人15种赚钱方法/宁波seo推荐
  • 驻马店重点项目建设网站/广告联盟app下载
  • 酒网站建设/重庆百度seo公司
  • 网站发的文章怎么做的/搜索引擎广告案例
  • 伪静态网站如何做/上海网络推广平台
  • 中石油网站建设/济南seo优化公司
  • 泰安平台公司/新余seo
  • wordpress 无法搜索结果/抖音seo怎么做
  • 校园网站建设策划书/抖音seo排名优化
  • 推广普通话的绘画作品有哪些/无锡网络优化推广公司
  • 网站免费一站二站四站/今日腾讯新闻最新消息
  • 深圳网站建设三把火/网站推广营销
  • 制作网站需要注意的细节/对网络营销的认识
  • 郑州网站网络营销/网站推广网络营销方案
  • 网站建设捌金手指花总六/广告推送平台
  • 公司免费网站建设/合肥seo公司
  • 做文案策划需要看什么网站/什么是网络营销与直播电商
  • 网站开发用什么技术asp/对seo的理解
  • 软件下载网站地址/上海网站制作公司
  • 爱润妍网站开发/湖南靠谱关键词优化
  • 大连住建部官网/外包seo公司
  • 本溪 网站建设 做网站/微信广告平台推广
  • 线上网站制作/巩义关键词优化推广
  • 网站建设需要哪些技术人员/网站排名推广
  • 上海网站建设公司费用/河北seo技术
  • 用阳寿做交易的网站/百度经验怎么赚钱
  • 卖钢材做哪个宣传网站/搜索引擎优化seo是什么
  • 快速搭建网站视频教程/报个电脑培训班要多少钱
  • 主题资源网站建设反思/互联网seo是什么
  • RabbitMQ的特点和消息可靠性保障
  • 基于 Amazon Nova Sonic 和 MCP 构建语音交互 Agent
  • ECMAScript2024(ES15)新特性
  • Android调用python库和方法的实现
  • 【ESP32设备通信】-LAN8720与ESP32集成
  • 永磁同步电机FOC控制----电流采样的实现