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

相关推荐
绝无仅有1 分钟前
某大厂MySQL面试之SQL注入触点发现与SQLMap测试
后端·面试·github
绝无仅有6 分钟前
某互联网大厂的面试go语言从基础到实战的经验和总结
后端·面试·github
澡点睡觉6 分钟前
【golang长途旅行第38站】工厂模式
开发语言·后端·golang
小蒜学长10 分钟前
基于SpringBoot+Vue的健身房管理系统的设计与实现(代码+数据库+LW)
java·数据库·vue.js·spring boot·后端
这里有鱼汤12 分钟前
你以为 FastAPI 足够强?其实 Litestar 能让你的项目更轻量高效
后端·python
勇往直前plus14 分钟前
Milvus快速入门以及用 Java 操作 Milvus
java·spring boot·embedding·milvus
菜鸟谢28 分钟前
windows vscode go 编译速度慢问题
后端
彭于晏Yan34 分钟前
Spring Boot中策略模式结合依赖注入的实现方式
spring boot·策略模式
Victor3561 小时前
Redis(51)如何监控Redis哨兵的状态?
后端
IT_陈寒1 小时前
Python性能优化:5个被低估的魔法方法让你的代码提速50%
前端·人工智能·后端