当前位置: 首页 > news >正文

JAVA中在junit测试类中加载Spring托管,通过注解使用Bean

一、前言

在java开发环境下,有时候需要使用junit来帮助我们做测试工作。正常使用时,只需在方法名上方加上@Test即可。但如果涉及到Spring的Bean托管,就需要多加2个注解了。

二、@Runner和@SpringBootTest

这两个注解需要加在测试类的类型上方,如下:

@RunWith(SpringRunner.class)
@SpringBootTest(classes = Springboot启动类.class)
public class Test {@Service你的Service类型 service;@Testpublic void testMethod1() {// 测试实现service.你的方法();}
}

写法简单,但是需要引入jar包依赖。在pom.xml中添加如下:

<dependencies><!-- 其它依赖开始 --><!-- ...... --><!-- 其它依赖结束 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-test</artifactId><version>3.2.5</version> </dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>6.1.6</version></dependency>
</dependencies>


http://www.mrgr.cn/news/23858.html

相关文章:

  • shell的使用sed处理数据
  • 轻量级模型解读——GhostNet系列
  • Git版本控制工具
  • day-52 三数之和
  • Qt放Element网页滑动菜单栏
  • udp的广播,多播,单播 demo
  • 图书馆上新了!新华书店×度小满推出“开学季悦读计划”公益活动
  • OpenCV findTours函数及其用法
  • Hive SQL子查询应用
  • springboot基础-Druid数据库连接池使用
  • [产品管理-4]:NPDP新产品开发 - 2 - 制定企业经营战略目标的结构化方法与工具
  • [Python]一文掌握Nuitka 参数语法详细介绍与示例
  • 7.科学计算模块Numpy(4)ndarray数组的常用操作(二)
  • WindTerm 2.6.1:全新版本,更强大,更便捷!
  • Web开发:ABP框架2——层次结构、ORM的使用、Webapi控制器的编写
  • 计算机网络:概述 - 计算机网络概述
  • c语言--力扣简单题目(删除排序链表中的重复元素)讲解
  • windows软件应该安装在哪里
  • 园区竞争十分激烈,各类园区网站美出天际啦。
  • 经验笔记:Web 浏览器存储手段