Springboot项目的测试类书写(速通)

本文介绍了在Java开发中如何编写单元测试类和框架测试类,包括使用Maven和JUnit进行单元测试,以及如何利用SpringBootTest和SpringRunner进行集成测试,详细讲解了相关注解的使用方法和场景。
摘要由CSDN通过智能技术生成

前言

在实际开发中,如果只是做一个简单的单元测试(不涉及端到端、数据库交互、API调用、消息队列处理等),我为了方便一般都是找块儿地方写一个main方法来跑一下就行了,当然不推荐这样做,怕被领导发现 。所以还是建议在 /src/test/xxx/ 目录下写一个测试类来做测试。

1. 单元测试的测试类

如果只是为了做一个单元测试,比如说测试一个方法是否能正常跑,那么可以按照以下流程创建一个:

  1. maven依赖

            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.12</version>
                <scope>test</scope>
            </dependency>
    
  2. 测试类

    import org.junit.Test;
    
    public class MyTest {
        @Test
        public void test() {
            long l = System.currentTimeMillis();
            System.out.println(l);
        }
    }
    

要求:

  1. 测试类中的方法需要加上@Test
  2. 访问修饰符必须是public,返回类型必须是void
  3. 不接受任何参数(JUnit5可以用@ParameterizedTest来实现参数化测试,但是很麻烦);
  4. 可用expected来标注需要捕获的异常@Test(expected = ExceptionType.class)

2. 框架测试的测试类

如果需要启动所有上下文进行测试,那么在测试类上面加一个@SpringBootTest就行了,接着就可以使用Bean做测试了,如下:

@SpringBootTest
public class ConfEncTest {
    @Autowired
    private StringEncryptor jasyptStringEncryptor;
	
	@Test
	public void encryptTest() {
		String originPassord = "123456";
        String encryptStr = jasyptStringEncryptor.encrypt( originPassord );
        System.out.println(encryptStr);
    }
 }

@SpringBootTest:启动一个全功能的Spring Boot应用上下文来进行集成测试,可以使用所有的Bean。启动一个测试方法就相当于启动整个项目,比较慢。

如果只是使用几个Bean,那么可以使用 @RunWith(SpringRunner.class) + @ContextConfiguration

@RunWith(SpringRunner.class)
@ContextConfiguration(classes = {StringEncryptor.class})
public class ConfEncTest {
    @Autowired
    private StringEncryptor jasyptStringEncryptor;
	
	@Test
	public void encryptTest() {
		String originPassord = "123456";
        String encryptStr = jasyptStringEncryptor.encrypt( originPassord );
        System.out.println(encryptStr);
    }
 }

@RunWith:用于指定运行测试类的测试运行器(Test Runner),需要搭配@ContextConfiguration使用。

SpringRunner: Spring提供的一个特殊的测试运行器,它负责解析@SpringBootTest、@ContextConfiguration等Spring相关的注解,并在测试执行前创建和配置Spring应用上下文。

@ContextConfiguration:指定包含所需Bean定义的配置类或XML配置文件。这样,SpringRunner在运行测试时,会根据@ContextConfiguration提供的信息来创建一个包含必要Bean的ApplicationContext,从而实现对这些依赖的注入。如果需要同时指定多个类可以这样写:@ContextConfiguration(classes = {ConfigA.class, ConfigB.class, ConfigC.class})。

@SpringBootTest是一个复合注解,其中包含了@RunWith。

  • 27
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
要创建一个Spring Boot测试,你需要按照以下步骤进行操作: 1. 在测试上添加`@RunWith(SpringRunner.class)`和`@SpringBootTest(classes = 你的启动.class)`注解。这将告诉测试框架在运行测试时使用Spring容器,并指定你的启动。 2. 创建一个测试,可以命名为`SpringbootTest`或其他你喜欢的名称。 3. 导入所需的依赖库和。例如,可以导入`org.junit.Test`、`org.junit.runner.RunWith`、`org.springframework.beans.factory.annotation.Autowired`、`org.springframework.boot.test.context.SpringBootTest`、`org.springframework.cloud.client.discovery.DiscoveryClient`和`org.springframework.test.context.junit4.SpringRunner`等。 4. 使用`@RunWith(SpringRunner.class)`和`@SpringBootTest`注解标记测试。 5. 使用`@Autowired`注解自动注入需要测试的组件或服务。 6. 编测试方法。使用`@Test`注解标记方法,并在方法中编测试逻辑。 7. 运行测试。你可以使用IDE的测试运行功能或者在命令行中运行测试。 这样,你就可以使用Spring Boot测试框架进行测试了。Spring Boot Test提供了方便和高效的测试能力,并结合了Spring Test和JUnit等其他测试框架的特性,同时还增强了mock能力。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [Springboot创建测试](https://blog.csdn.net/m0_59259076/article/details/124036156)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [SpringBoot测试](https://blog.csdn.net/m0_67403076/article/details/126115129)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

364.99°

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值