首页 > 其他分享 >springboot整合JUnit

springboot整合JUnit

时间:2023-04-03 22:44:23浏览次数:29  
标签:springboot bookDao class SpringBootTest classes 整合 测试 JUnit

步骤:

  1. 导入测试对应的starter(springboot帮我们自动导入,纯手工创建时一定记得自己导入)
  2. 测试类使用@SpringBootTest修饰
  3. 使用自动装配的形式添加要测试的对象



  • 名称:@SpringBootTest
  • 类型:测试类注解
  • 位置:测试类定义上方
  • 作用:设置JUnit加载的SpringBoot启动类
  • 范例:

@SpringBootTest
class Springboot02JunitApplicationTests {

    //1.注入你要测试的对象
    @Autowired
    private BookDao bookDao;

    @Test
    void contextLoads() {
        bookDao.save();
    }

}

@SpringBootTestd的classes属性

  • 当测试类在启动类的包及其子包下,则能够正常运行。否则不能正常运行
/*

@RunWith(设置运行器)
@TestConfiguration// 指定配置文件或者配置类是哪一个
测试的整个工作取得的被测试的对象是在spring容器中的,必须拿到spring的容器,如果没有按照要求放置类的层次结构,则拿不到容器
必须指定配置文件或者配置类是哪一个

*/

  • 解决方法:
    将引导类添加到测试类上面
    @SpringBootTest(classes = Springboot02JunitApplication.class)

@SpringBootTest(classes = Springboot02JunitApplication.class)
class Springboot02JunitApplicationTests {

    //1.注入你要测试的对象
    @Autowired
    private BookDao bookDao;

    @Test
    void contextLoads() {
        bookDao.save();
    }

}

classes属性作用:设置JUnit加载的springboot启动类

标签:springboot,bookDao,class,SpringBootTest,classes,整合,测试,JUnit
From: https://www.cnblogs.com/CenCen/p/17284775.html

相关文章

  • springboot请求响应
    springboot请求响应1.什么是请求?响应?请求:获取请求数据响应:设置响应数据2.原始方法获取请求数据Controller方法形参中声明HttpServletRequest对象调用对象的getParameter(参数名)这种方式复杂繁琐//@RequestMapping("/simpleParam")//原始方式//创建请求对......
  • Springboot JSON整合—官方原版
    SpringBoot提供与三个JSON映射库的集成:GsonJacksonJSON-BJackson是首选和默认库。一、Jackson提供了Jackson的自动配置,Jackson是springbootstarterjson的一部分。当Jackson在类路径上时,会自动配置一个ObjectMapperbean。提供了几个配置财产,用于自定义ObjectMapper的配置。1.......
  • 怎么在springboot中配置https证书的详细教程
    前言由于小程序需要https,然后之前申请的域名过期了,用了两年由于忘记续费要将域名赎回居然要1200....想了一下之前还有另一个域名,干脆就用这个域名弄个二级域名出来,所以二级域名建立出来后需要在springboot项目上开启https访问废话不多说,开整在阿里云新建二级域名这个......
  • SpringBoot启动异常的错误①
    java:无法访问org.springframework.boot.SpringApplication错误的类文件:/D:/maven/repository/org/springframework/boot/spring-boot/3.0.5/spring-boot-3.0.5.jar!/org/springframework/boot/SpringApplication.class类文件具有错误的版本61.0,应为52.0 2023-04......
  • springboot 日志
    <loggername="com.sinoservices.chainwork.bms"level="INFO"/><loggername="org.hibernate.orm.deprecation"level="error"/><loggername="druid"additivity="true"><levelval......
  • 26-springboot-thymeleaf字符串拼接-常量-符号
    Thymeleaf字符串拼接一种是字符串拼接:<spanth:text="'当前是第'+${sex}+'页,共'+${sex}+'页'"></span>另一种更简洁的方式,使用“|”减少了字符串的拼接:<spanth:text="|当前是第${sex}页,共${sex}页|"></span>Thymeleaf可直接使用的常量和符号1、所有......
  • 27-springboot-thymeleaf内置对象
    1、内置web对象thymaleaf内置的web对象,可以直接在模板中使用,这些对象由#号开头:#request:相当于HttpServletRequest对象,这是Thymeleaf3.x版本,若是Thymeleaf2.x版本使用#httpServletRequest;${#request.getContextPath()}${#request.getAttribute("phone")}#session:相当于H......
  • 24-springboot-thymeleaf的表达式
    1.添加热部署,为了测试不用频繁重启<!--热部署插件--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId><optional>true</optional><!--防止将该依赖传递到其他模块中--></depen......
  • 25-springboot-thymeleaf的常见属性
    th:action<formid="login"th:action="@{/login}">......</form>th:method<formid="login"th:action="@{/login}"th:method="post">......</form>th:href<a class="login"......
  • 22-springboot应用监控-actuator
    可以做成页面监控(springboot-admin),而不是json的格式,看起来会更方便。在生产环境中,有时可能需要监控服务的可用性,spring-boot的actuator就是提供了对应用的配置查看、健康检查、相关功能统计等,可以通过HTTP,JMX来访问这些监控功能;(端点)如何使用该功能呢?1、在项目的Maven中添......