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

相关推荐
VX:Fegn08952 小时前
计算机毕业设计|基于ssm + vue超市管理系统(源码+数据库+文档)
前端·数据库·vue.js·spring boot·后端·课程设计
Mr.朱鹏3 小时前
Nginx路由转发案例实战
java·运维·spring boot·nginx·spring·intellij-idea·jetty
VX:Fegn08954 小时前
计算机毕业设计|基于springboot + vue酒店管理系统(源码+数据库+文档)
vue.js·spring boot·课程设计
Java天梯之路6 小时前
Spring Boot 钩子全集实战(七):BeanFactoryPostProcessor详解
java·spring boot·后端
wr2005146 小时前
第二次作业,渗透
java·后端·spring
短剑重铸之日6 小时前
《SpringCloud实用版》生产部署:Docker + Kubernetes + GraalVM 原生镜像 完整方案
后端·spring cloud·docker·kubernetes·graalvm
爬山算法7 小时前
Hibernate(67)如何在云环境中使用Hibernate?
java·后端·hibernate
女王大人万岁8 小时前
Go标准库 io与os库详解
服务器·开发语言·后端·golang
露天赏雪8 小时前
Java 高并发编程实战:从线程池到分布式锁,解决生产环境并发问题
java·开发语言·spring boot·分布式·后端·mysql
短剑重铸之日9 小时前
《SpringCloud实用版》 Seata 分布式事务实战:AT / TCC / Saga /XA
后端·spring·spring cloud·seata·分布式事务