springboot的测试类

springboot 的测试

需要的以来jar

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <!--test表明只能在test包下才可以用的jar,在非test包下不可用-->
            <scope>test</scope>
        </dependency>

优雅的方法
①先在pom.xml文件中引入上面的jar,然后在test(一定是与main同级的那个test下哦)的包才可以。
②然后新建一个TestBean
③用其他需要测试的类继承这个TestBean

package org.my.test;
// TestBean类
import org.junit.runner.RunWith;
import org.my.SpringBootApplication;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

 //作用:让当前类在容器环境下进行测试
@RunWith(SpringRunner.class)
//作用:声明当前类是springboot的测试类并且获取入口类上的相关信息 SpringBootApplication是入口类类名
@SpringBootTest(classes = SpringBootApplication.class)
public class TestBean {

}

注意:子类的类名一定是不能够和main包下的任意类同名,否则报错,这是一个坑的

package org.my.service;

import lombok.extern.log4j.Log4j;
import org.junit.Test;
import org.my.entity.Province;
import org.my.test.TestBean;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Log4j
public class TestProvinceService extends TestBean {
    @Autowired
    ProvinceService provinceService;

    @Test
    public void testSelectAllProvinces() {
        List<Province> provinceList = provinceService.selectAllProvinces();
        log.debug("testSelectAllProvinces");
        System.out.println("testSelectAllProvinces");
        for (Province province : provinceList) {
            System.out.println(province);

        }
    }
}

简单粗暴的方法:①导入依赖;
②直接在test包下新建测试类 加上下面的两个注解,然后就可以直接写测试方法了。当然了,你的每一个测试类都是需要添加上这两个注解哦

package org.my.test;
// TestBean类
import org.junit.runner.RunWith;
import org.my.SpringBootApplication;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;


@RunWith(SpringRunner.class) //作用:让当前类在容器环境下进行测试
@SpringBootTest(classes = SpringBootApplication.class)//作用:声明当前类是springboot的测试类并且获取入口类上的相关信息 SpringBootApplication是入口类类名
public class TestBean {

}
  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要创建一个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 ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值