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 提供的一个非常方便的注解,用来避免显式地声明受检查异常,简化代码,但要谨慎使用,确保异常处理的合理性和可读性。

相关推荐
Albert Edison3 小时前
【最新版】IntelliJ IDEA 2025 创建 SpringBoot 项目
java·spring boot·intellij-idea
Piper蛋窝4 小时前
深入 Go 语言垃圾回收:从原理到内建类型 Slice、Map 的陷阱以及为何需要 strings.Builder
后端·go
六毛的毛6 小时前
Springboot开发常见注解一览
java·spring boot·后端
AntBlack6 小时前
拖了五个月 ,不当韭菜体验版算是正式发布了
前端·后端·python
31535669136 小时前
一个简单的脚本,让pdf开启夜间模式
前端·后端
uzong7 小时前
curl案例讲解
后端
开开心心就好8 小时前
免费PDF处理软件,支持多种操作
运维·服务器·前端·spring boot·智能手机·pdf·电脑
一只叫煤球的猫8 小时前
真实事故复盘:Redis分布式锁居然失效了?公司十年老程序员踩的坑
java·redis·后端
猴哥源码8 小时前
基于Java+SpringBoot的农事管理系统
java·spring boot
大鸡腿同学8 小时前
身弱武修法:玄之又玄,奇妙之门
后端