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

SpringBoot启用web模拟测试(一)

添加依赖
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <version>2.5.10</version>
</dependency>

模拟端口

   虚拟请求测试

@Slf4j
@RestController
@RequestMapping("/books")
public class BookController {


    @Autowired
    private IBookService iBookService;
    @GetMapping("/get/{id}")
    public R getById(@PathVariable Integer id) throws IOException {

        return new R(true,iBookService.getById(id));
    }
    @GetMapping
    public R getAll(){
        return new R(true,iBookService.list());
    }
    @PostMapping
    public R save(@RequestBody Book book) throws IOException {

        boolean flag = iBookService.save(book);
        return new R(flag,flag?"添加成功🙂":"添加失败😂");
    }
    @PutMapping
    public R updateById(@RequestBody Book book){
        boolean flag = iBookService.modify(book);
        return new R(flag,flag?"数据更新成功":"数据更新失败");
    }
    @DeleteMapping("/delete/{integer}")
    public R deleteById(@PathVariable Integer integer){
        boolean flag = iBookService.delete(integer);
        return new R(flag,flag?"数据删除成功":"数据删除失败");
    }

    @GetMapping("{current}/{pageSize}")
    public R getByPage(@PathVariable Integer current,@PathVariable Integer pageSize,String name,Book book){
        System.out.println("name"+name);
        System.out.println("book"+book);
        IPage<Book> ipage=iBookService.getByPage(current,pageSize,book);
        if(current>ipage.getPages()){
            ipage =iBookService.getByPage((int)ipage.getPages(),pageSize,book);
        }

        return new R(true,ipage);
    }
}

测试代码

@ContextConfiguration(classes = Application.class)
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
//开启虚拟MVC调用
@AutoConfigureMockMvc
public class WebTest {
    @Autowired
    private MockMvc mvc;
    @Test
    //http://localhost/testSpringboot/books
    //创建虚拟请求,当前访问books
    public void test() throws Exception {
        MockHttpServletRequestBuilder mockHttpServletRequestBuilder= MockMvcRequestBuilders.get("/books");
        //执行对应的请求
        mvc.perform(mockHttpServletRequestBuilder);
    }

总结


http://www.kler.cn/news/18256.html

相关文章:

  • 固态继电器的优点
  • 增强型PID-自适应-前馈-神经网络控制研究(Matlab代码实现)
  • 网页端操作提示「msg.js」库简介
  • linux实现CP指令
  • LeetCode 2432. 处理用时最长的那个任务的员工
  • 从爆火的“哇呀挖”,思考我软件开发的人生意义何在?
  • JDK17新特性之--JDK9到JDK17 String 新增的新方法
  • 53.MDL、NCNN和 TFLite比较
  • C++Primer 第一章
  • 将数据从 Oracle 加载到 Azure 的框架
  • 68元工业级双核A7,全新T113核心板震撼上市!
  • CSA发布|《洞察2022 云上数据安全与重要事项 》
  • spring-web HandlerAdapter 源码分析
  • 记录每日LeetCode 2432.处理用时最长的那个任务的员工 Java实现
  • Feign组件的使用及开发中使用方式
  • ZC706P试验PL_DDR3内存条的步骤方法
  • 使用SaleSmartly自动化流程的 5 个原因
  • 网络基础学习:什么是网络与网络发展史
  • 接口自动化测试之HTTP协议详解(敢称全网最全)
  • AP360X 可充电多功能LED手电筒与移动照明控制ic和应用方案
  • 【SpringBoot】SpringBoot集成ElasticSearch
  • AQS底层源码解析
  • 如何利用问卷工具助力活动开展,实现高效数据收集?
  • 一起学 WebGL:绘制图片
  • 数据库中全局锁、表级锁、行级锁的区别
  • pyinstaller打包Mediapipe时遇到的问题
  • PCIe物理层弹性缓存机制(详细)解析-PCIe专题知识(四)
  • ( “图“ 之 二分图 ) 785. 判断二分图 ——【Leetcode每日一题】
  • 2023年全国最新保安员精选真题及答案65
  • 获得 随机验证码(以图片为底层)