实验报告6-项目测试与部署
实验报告4-实现分页功能
一、实现思路
打包运行商品分页项目。要求:
1、测试项目service层的分页功能,
2、controller层添加first()并测试
3、将项目打为JAR包并运行,
4、将项目打为WAR包并运行。
二、实验步骤
1、测试项目service层的分页功能
service层,GoodsService.java,paging()添加测试
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE)
class GoodsServiceTest {
@Autowired
private GoodsService goodsService;
@Test
void paging() {
Integer page = 0;
Integer size = 6;
Page<Goods> paging = goodsService.paging(page, size);
long totalElements = paging.getTotalElements();
int totalPages = paging.getTotalPages();
List<Goods> goodsList = paging.getContent();
System.out.println("totalElements="+totalElements);
System.out.println("totalPages="+totalPages);
for (Goods goods : goodsList) {
System.out.println(goods);
}
}
}
2、测试项目service层的分页功能
controller层,GoodsController.java,first()
@ResponseBody
@RequestMapping("/first")
public String first(){
System.out.println("first method is running");
return "Welcome to Spring Boot Application";
}
controller层,GoodsController.java,测试first()
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@AutoConfigureMockMvc
class GoodsControllerTest {
@Test
void page(@Autowired MockMvc mvc) throws Exception {
//创建虚拟请求
MockHttpServletRequestBuilder builder
= MockMvcRequestBuilders.get("/goods/first");
//执行请求
mvc.perform(builder);
}
}
3、打为jar包
添加maven打包插件
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
打包。Maven→项目名→Lifecycle→clean→package
运行。java -jar PageBySpringBoot.jar
4、打为war包
设置项目路径
server:
servlet:
context-path: /goodsPage
解决静态资源访问失效问题,修改为thymeleaf的th属性动态访问
设置打包方式为war
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
排除内置Tomcat
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
添加Tomcat依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<!--仅在编译和测试阶段使用-->
<scope>provided</scope>
</dependency>
添加maven打包插件
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
修改启动类
@SpringBootApplication
@ServletComponentScan
public class PageBySpringBootApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(PageBySpringBootApplication.class,args);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(PageBySpringBootApplication.class);
}
}
打包。Maven→项目名→Lifecycle→clean→package
tomcat运行war tomcat\bin目录,startup.bat
原文地址:https://blog.csdn.net/u010288901/article/details/143667971
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!