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

相关推荐
ybq1951334543115 分钟前
javaEE-SpringBoot日志
java·spring boot·后端
PyAIGCMaster25 分钟前
第二周补充:Go语言中&取地址符与fmt函数详解
开发语言·后端·golang
Dongwoo Jeong28 分钟前
缓存基础解释与缓存友好型编程基础
后端·c·cache·cache friendly
Gy-1-__33 分钟前
【springcloud】快速搭建一套分布式服务springcloudalibaba(一)
后端·spring·spring cloud
硬件人某某某41 分钟前
基于Django的手办交易平台~源码
后端·python·django
找了一圈尾巴1 小时前
Spring Boot 日志管理(官网文档解读)
java·spring boot
闲猫6 小时前
go orm GORM
开发语言·后端·golang
丁卯4047 小时前
Go语言中使用viper绑定结构体和yaml文件信息时,标签的使用
服务器·后端·golang
Tirzano7 小时前
springsecurity自定义认证
spring boot·spring
bing_15810 小时前
简单工厂模式 (Simple Factory Pattern) 在Spring Boot 中的应用
spring boot·后端·简单工厂模式