精选列表

搜索[SpringBoot],共4121篇文章
优秀的个人博客,低调大师

SpringBoot入门系列: Spring Boot的测试

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 plaincopy packagecom.example.service; importjava.util.List; importorg.junit.Assert; importorg.junit.Test; importorg.junit.runner.RunWith; importorg.springframework.beans.factory.annotation.Autowired; importorg.springframework.boot.test.SpringApplicationConfiguration; importorg.springframework.test.context.junit4.SpringJUnit4ClassRunner; importorg.springframework.test.context.web.WebAppConfiguration; importcom.example.Application; importcom.example.domain.TestPOJO; importcom.example.dto.HotelDto; @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes=Application.class) @WebAppConfiguration publicclassTestServicesTest{ @Autowired TestServicestestServices; @Test publicvoidtestShow(){ StringexpectedResult="helloworld!"; Stringresult=testServices.show(); Assert.assertTrue("数据一致",expectedResult.equals(result)); Assert.assertFalse("数据不一致",!expectedResult.equals(result)); } @Test publicvoidtestShowDao(){ List<TestPOJO>testPOJOList=testServices.showDao(10); Assert.assertTrue("数据集不对",testPOJOList.size()==1); Assert.assertTrue("数据一致",testPOJOList.get(0).getName().equals("nice")); } @Test publicvoidtestFindByCountry(){ List<HotelDto>testPOJOList=testServices.findByCountry("US"); Assert.assertTrue("数据集不对",testPOJOList.size()==1); Assert.assertTrue("数据一致",testPOJOList.get(0).getCityName().equals("SanFrancisco")); } } 2、controller类的 [java]view plaincopy packagecom.example.controller; importjava.util.List; importorg.junit.Assert; importorg.junit.Before; importorg.junit.Test; importorg.junit.runner.RunWith; importorg.springframework.beans.factory.annotation.Autowired; importorg.springframework.boot.test.SpringApplicationConfiguration; importorg.springframework.http.MediaType; importorg.springframework.test.context.junit4.SpringJUnit4ClassRunner; importorg.springframework.test.context.web.WebAppConfiguration; importorg.springframework.test.web.servlet.MockMvc; importorg.springframework.test.web.servlet.MvcResult; importorg.springframework.test.web.servlet.request.MockMvcRequestBuilders; importorg.springframework.test.web.servlet.setup.MockMvcBuilders; importorg.springframework.web.context.WebApplicationContext; importcom.example.Application; importcom.example.domain.TestPOJO; importcom.example.dto.HotelDto; importcom.example.service.TestServices; importcom.fasterxml.jackson.core.JsonProcessingException; importcom.fasterxml.jackson.databind.ObjectMapper; @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes=Application.class) @WebAppConfiguration publicclassTestControllerTest{ MockMvcmvc; @Autowired WebApplicationContextwebApplicationConnect; @Autowired TestServicestestServices; StringexpectedJson; @Before publicvoidsetUp()throwsJsonProcessingException{ mvc=MockMvcBuilders.webAppContextSetup(webApplicationConnect).build(); } @Test publicvoidtestShow()throwsException{ StringexpectedResult="helloworld!"; Stringuri="/show"; MvcResultmvcResult=mvc.perform(MockMvcRequestBuilders.get(uri).accept(MediaType.APPLICATION_JSON)) .andReturn(); intstatus=mvcResult.getResponse().getStatus(); Stringcontent=mvcResult.getResponse().getContentAsString(); Assert.assertTrue("错误,正确的返回值为200",status==200); Assert.assertFalse("错误,正确的返回值为200",status!=200); Assert.assertTrue("数据一致",expectedResult.equals(content)); Assert.assertFalse("数据不一致",!expectedResult.equals(content)); } protectedStringObj2Json(Objectobj)throwsJsonProcessingException{ ObjectMappermapper=newObjectMapper(); returnmapper.writeValueAsString(obj); } @Test publicvoidtestShowDaoInt()throwsException{ List<TestPOJO>testPOJOList=testServices.showDao(10); expectedJson=Obj2Json(testPOJOList); Stringuri="/showDao?age=10"; MvcResultmvcResult=mvc.perform(MockMvcRequestBuilders.get(uri).accept(MediaType.APPLICATION_JSON)).andReturn(); intstatus=mvcResult.getResponse().getStatus(); Stringcontent=mvcResult.getResponse().getContentAsString(); Assert.assertTrue("错误,正确的返回值为200",status==200); Assert.assertFalse("错误,正确的返回值为200",status!=200); Assert.assertTrue("数据一致",expectedJson.equals(content)); Assert.assertFalse("数据不一致",!expectedJson.equals(content)); } @Test publicvoidtestShowDaoString()throwsException{ List<HotelDto>hotelDtoList=testServices.findByCountry("US"); expectedJson=Obj2Json(hotelDtoList); Stringuri="/country/US"; MvcResultmvcResult=mvc.perform(MockMvcRequestBuilders.get(uri).accept(MediaType.APPLICATION_JSON)).andReturn(); intstatus=mvcResult.getResponse().getStatus(); Stringcontent=mvcResult.getResponse().getContentAsString(); Assert.assertTrue("错误,正确的返回值为200",status==200); Assert.assertFalse("错误,正确的返回值为200",status!=200); Assert.assertTrue("数据一致",expectedJson.equals(content)); Assert.assertFalse("数据不一致",!expectedJson.equals(content)); } } controller类的,为了MockMvc,注入了WebApplicationContext。 原文地址http://www.bieryun.com/833.html

资源下载

更多资源
Mario,低调大师唯一一个Java游戏作品

Mario,低调大师唯一一个Java游戏作品

马里奥是站在游戏界顶峰的超人气多面角色。马里奥靠吃蘑菇成长,特征是大鼻子、头戴帽子、身穿背带裤,还留着胡子。与他的双胞胎兄弟路易基一起,长年担任任天堂的招牌角色。

Apache Tomcat7、8、9(Java Web服务器)

Apache Tomcat7、8、9(Java Web服务器)

Tomcat是Apache 软件基金会(Apache Software Foundation)的Jakarta 项目中的一个核心项目,由Apache、Sun 和其他一些公司及个人共同开发而成。因为Tomcat 技术先进、性能稳定,而且免费,因而深受Java 爱好者的喜爱并得到了部分软件开发商的认可,成为目前比较流行的Web 应用服务器。

Eclipse(集成开发环境)

Eclipse(集成开发环境)

Eclipse 是一个开放源代码的、基于Java的可扩展开发平台。就其本身而言,它只是一个框架和一组服务,用于通过插件组件构建开发环境。幸运的是,Eclipse 附带了一个标准的插件集,包括Java开发工具(Java Development Kit,JDK)。

Sublime Text 一个代码编辑器

Sublime Text 一个代码编辑器

Sublime Text具有漂亮的用户界面和强大的功能,例如代码缩略图,Python的插件,代码段等。还可自定义键绑定,菜单和工具栏。Sublime Text 的主要功能包括:拼写检查,书签,完整的 Python API , Goto 功能,即时项目切换,多选择,多窗口等等。Sublime Text 是一个跨平台的编辑器,同时支持Windows、Linux、Mac OS X等操作系统。