springboot单元测试配置

java 复制代码
package com.sss.rrr.server;

import lombok.SneakyThrows;
import org.apache.commons.lang3.StringUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;


/**
 * @Author 
 * @Date 
 */
@RunWith(SpringRunner.class)
@SpringBootTest(classes = {WebApplication.class})
public class DemoTest {

    @Autowired
    private PunishmentService punishmentService;

	@SneakyThrows
    @Test
    public void getAllDropdownValues(){
        Map<String, Object> allDropdownValues = punishmentService.getAllDropdownValues();
        System.out.println(JSONObject.toJSONString(allDropdownValues));
    }
	
	@Test
    public void getPunishedPersonByEmployeeIdOrName(){
        PunishmentParams punishmentParams = new PunishmentParams();
        punishmentParams.setPage(0);
        punishmentParams.setPageSize(10);
        Page<PsLxPunishmentVo> punishedPersonByEmployeeIdOrName = punishmentService.getPunishedPersonByEmployeeIdOrName(punishmentParams);
        System.out.println(punishedPersonByEmployeeIdOrName);
    }

    @Test
    public void exportPunishedList(){
        PunishmentParams punishmentParams = new PunishmentParams();
        punishmentParams.setPage(0);
        punishmentParams.setPageSize(10);
        punishmentService.exportPunishedList(punishmentParams);
    }

}

以上就是基础配置,以下是配置讲解:

复制代码
@RunWith(SpringRunner.class):让测试框架使用 Spring Runner 来运行测试。
@SpringBootTest(classes = {WebApplication.class}):指定加载 WebApplication.class 作为 Spring Boot 启动类,启动 Spring 上下文。
@Autowired:自动注入 MyService,这要求 MyService 在 Spring 上下文中注册。
@Test:表示这是一个测试方法。
assertNotNull:检查 myService 是否注入成功。

@SneakyThrows 是 Lombok 提供的一个非常有用的注解,它允许在方法中抛出受检查异常(checked exceptions),而不需要显式地声明它们。这个注解通过反射的方式,让在方法中可以直接抛出异常,而无需在方法签名中写 throws 子句。

作用:

通常,在 Java 中,所有受检查异常(checked exceptions)都必须在方法签名中声明,或者必须被捕获并处理。如果不想每次抛出异常时都在方法签名中加上 throws 关键字,可以使用 @SneakyThrows 来避免显式声明。

示例:

假设有一个方法需要抛出 IOException 这样的受检查异常,通常需要在方法签名中声明:

java 复制代码
public void readFile() throws IOException {
    // Some code that throws IOException
}

如果使用 @SneakyThrows,就不需要显式地声明 throws IOException:

java 复制代码
import lombok.SneakyThrows;

public class FileReader {
    
    @SneakyThrows
    public void readFile() {
        // Some code that throws IOException without needing to declare throws
        throw new java.io.IOException("File not found");
    }
}

在上面的例子中,@SneakyThrows 会自动处理 IOException,不需要在方法签名中声明它。

适用场景:

复制代码
避免冗长的 throws 声明:当方法中有多个受检查异常时,@SneakyThrows 可以避免冗长的 throws 子句。
快速实现简洁代码:对于一些临时的或辅助性的方法,@SneakyThrows 可以帮助减少代码的冗余。

注意事项:

复制代码
@SneakyThrows 并不会实际捕获异常,它只是在代码中省略 throws 声明。异常仍然会按照正常的 Java 异常机制传播。

示例:多种异常

也可以让 @SneakyThrows 处理多个异常类型,以下是一个处理 IOException 和 InterruptedException 的示例:

java 复制代码
import lombok.SneakyThrows;

public class FileProcessor {
    
    @SneakyThrows
    public void processFile() {
        // 可以抛出多个不同类型的异常
        if (someCondition) {
            throw new java.io.IOException("File not found");
        } else {
            throw new InterruptedException("Thread interrupted");
        }
    }
}

总结:

@SneakyThrows 是 Lombok 提供的一个非常方便的注解,用来避免显式地声明受检查异常,简化代码,但要谨慎使用,确保异常处理的合理性和可读性。

相关推荐
qq_12498707533 分钟前
基于springboot框架的小型饮料销售管理系统的设计与实现(源码+论文+部署+安装)
java·spring boot·后端·spring·毕业设计
我命由我1234510 分钟前
Python Flask 开发:在 Flask 中返回字符串时,浏览器将其作为 HTML 解析
服务器·开发语言·后端·python·flask·html·学习方法
IT_陈寒18 分钟前
JavaScript 性能优化:5个被低估的V8引擎技巧让你的代码提速50%
前端·人工智能·后端
想用offer打牌30 分钟前
数据库大事务有什么危害(面试版)
数据库·后端·架构
Jaising66631 分钟前
Spring 错误使用事务导致数据可见性问题分析
数据库·spring boot
踏浪无痕38 分钟前
别再只会用 Feign!手写一个 Mini RPC 框架搞懂 Spring Cloud 底层原理
后端·面试·架构
NMBG2241 分钟前
外卖综合项目
java·前端·spring boot
小徐Chao努力1 小时前
Spring AI Alibaba A2A 使用指南
java·人工智能·spring boot·spring·spring cloud·agent·a2a
用户695619440371 小时前
前后端分离VUE3+Springboot项目集成PageOffice核心代码
后端
rannn_1111 小时前
【Git教程】概述、常用命令、Git-IDEA集成
java·git·后端·intellij-idea