Spring Boot--junit的使用

SpringBoot单元测试详解:依赖、基类、断言及问题解决
本文介绍了SpringBoot中使用JUnit进行单元测试的实践,包括添加测试依赖、创建测试基类、编写测试类、打包测试、忽略测试方法以及断言的使用。同时,针对私有方法测试、Controller层测试、数据回滚和WebSocket测试问题给出了解决方案。此外,还提到了在maven打包时排除测试代码的方法。

juint

参考文章1
待补充

1. 依赖

修改pom.xml 增加依赖,一般Spring Boot项目有引入

	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-test</artifactId>
		<scope>test</scope>
	</dependency>

2.增加测试类基类(避免每个类都需要注解)

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.web.WebAppConfiguration;

@RunWith(SpringRunner.class)
@SpringBootTest
@WebAppConfiguration
public class SpringbootdemoApplicationTests {

    @Before
    public void init() {
        System.out.println("开始测试-----------------");
    }

    @After
    public void after() {
        System.out.println("测试结束-----------------");
    }

}

3.测试类

public class TestServices extends SpringbootdemoApplicationTests {

    @Autowired
    Service service;

    @Test
    //回滚数据操作
    @Transactional
    public void testDelete() {
        service.deleteById("111111");
    }

    

4.打包测试

@Suite.SuiteClasses({TestServices.class,TestServices2.class})
//@Ignore("not ready yet")
@RunWith(Suite.class)
@Suite.SuiteClasses({TestServices.class,TestServices2.class})
public class TestSuits {
}

5.忽略方法

@Ignore("not ready yet")

断言

待补充

常见问题解决

私有方法测试

参考2利用反射获取方法,调用方法

private Double format(Double fileSize){
	Double size = fileSize;
	size = size / 1024 / 1024;
	size = (int)(size.doubleValue() * 10 + 0.5) / 10.0;
	return size;
}
@Test
public void testFormat(){
	ClientDownloadAction action = new ClientDownloadAction();
	double size = 3732930;
	Class class1 = action.getClass();
	try {
		Method format = class1.getDeclaredMethod("format", Double.class);
		format.setAccessible(true);//设为可见
		Double result = (Double)format.invoke(action, size);
		Double expect = 3.6;
		Assert.assertEquals(expect, result);
	} catch (Exception e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
}

controller 层测试

待补充

数据回滚

根据参考3在方法上增加注解 @Transactional

websocket导致测试类启动失败

加载需要加载servlet容器,根据
参考4中的解决方法
修改类上面的注解

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)

maven打包时去除 test

在这里插入图片描述

参考


  1. SpringBoot使用Junit单元测试 ↩︎

  2. 如何给一个私有方法做单元测试 ↩︎

  3. Spring Boot(五):如何在单元测试中自动回滚数据 ↩︎

  4. springboot整合websocket后运行测试类报javax.websocket.server.ServerContainer not available ↩︎

### 解决 Spring Boot 测试依赖中 JUnit 报错的方法 遇到 `The import org.springframework.test.context.junit4.SpringJUnit4ClassRunner cannot be resolved` 的错误通常是因为缺少必要的测试依赖项或版本不兼容。 #### 添加正确的依赖到 pom.xml 文件 为了确保能够正常使用 `SpringJUnit4ClassRunner`,需要确认项目的 `pom.xml` 中包含了 spring-boot-starter-test 依赖。此依赖不仅提供了 JUnit 支持还集成了其他有用的库用于集成测试[^1]。 ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> ``` #### 更新 IDE 和 Maven 设置 有时即使添加了正确的依赖也可能因为本地仓库缓存或其他原因导致问题未解决。尝试更新 Eclipse 或者 IntelliJ IDEA 的索引以及执行 maven clean install 来刷新项目结构和下载最新的 jar 包。 #### 使用最新版的 JUnit 版本 如果仍然存在无法解析的问题,则可能是由于使用JUnit 版本过旧造成的冲突。可以考虑升级至更现代的 JUnit Jupiter (即 JUnit 5),并相应调整代码中的导入路径: 对于 JUnit 5 及以上版本应改为使用如下方式引入 runner 类: ```java import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; // or other specific test annotations based on your needs. ``` 并且在测试类上方声明新的运行器注解形式: ```java @DataJpaTest // Or another appropriate annotation depending on what you're testing public class MyTestClass { ... } ``` #### 配置文件检查 另外还需核查 application.properties 或 application.yml 是否有影响测试环境配置的内容,特别是当涉及到不同 profile 下的不同行为时。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值