当前位置:   article > 正文

系列六、Spring整合单元测试_spring 聚合工程 做单元测试

spring 聚合工程 做单元测试

一、概述

        Spring中获取bean最常见的方式是通过ClassPathXmlApplicationContext 或者 AnnotationConfigApplicationContext的getBean()方式获取bean,那么在Spring中如何像在SpringBoot中直接一个类上添加个@SpringBootTest注解,即可在类中注入自己想要测试的bean呢?解决方案是有的,spring-test即提供了这个功能。Spring整合单元测试步骤如下:

二、Spring整合Junit单元测试

2.1、案例

2.2、pom

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  3. <modelVersion>4.0.0</modelVersion>
  4. <groupId>org.star</groupId>
  5. <artifactId>spring5x06-mybatis</artifactId>
  6. <version>1.0-SNAPSHOT</version>
  7. <packaging>jar</packaging>
  8. <name>spring5x06-mybatis</name>
  9. <url>http://maven.apache.org</url>
  10. <properties>
  11. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  12. </properties>
  13. <dependencies>
  14. <!--spring基本依赖-->
  15. <dependency>
  16. <groupId>org.springframework</groupId>
  17. <artifactId>spring-aop</artifactId>
  18. <version>5.2.5.RELEASE</version>
  19. </dependency>
  20. <dependency>
  21. <groupId>org.springframework</groupId>
  22. <artifactId>spring-beans</artifactId>
  23. <version>5.2.5.RELEASE</version>
  24. </dependency>
  25. <dependency>
  26. <groupId>org.springframework</groupId>
  27. <artifactId>spring-context</artifactId>
  28. <version>5.2.5.RELEASE</version>
  29. </dependency>
  30. <dependency>
  31. <groupId>org.springframework</groupId>
  32. <artifactId>spring-core</artifactId>
  33. <version>5.2.5.RELEASE</version>
  34. </dependency>
  35. <dependency>
  36. <groupId>org.springframework</groupId>
  37. <artifactId>spring-expression</artifactId>
  38. <version>5.2.5.RELEASE</version>
  39. </dependency>
  40. <dependency>
  41. <groupId>org.springframework</groupId>
  42. <artifactId>spring-test</artifactId>
  43. <version>5.2.5.RELEASE</version>
  44. </dependency>
  45. <!-- 数据源 -->
  46. <dependency>
  47. <groupId>mysql</groupId>
  48. <artifactId>mysql-connector-java</artifactId>
  49. <version>8.0.26</version>
  50. </dependency>
  51. <dependency>
  52. <groupId>com.alibaba</groupId>
  53. <artifactId>druid</artifactId>
  54. <version>1.2.16</version>
  55. </dependency>
  56. <dependency>
  57. <groupId>org.springframework</groupId>
  58. <artifactId>spring-jdbc</artifactId>
  59. <version>5.3.27</version>
  60. </dependency>
  61. <dependency>
  62. <groupId>org.mybatis</groupId>
  63. <artifactId>mybatis</artifactId>
  64. <version>3.5.11</version>
  65. </dependency>
  66. <dependency>
  67. <groupId>org.mybatis</groupId>
  68. <artifactId>mybatis-spring</artifactId>
  69. <version>2.1.0</version>
  70. </dependency>
  71. <!-- 普通maven项目中使用Sl4j注解 -->
  72. <dependency>
  73. <groupId>org.projectlombok</groupId>
  74. <artifactId>lombok</artifactId>
  75. <version>1.18.22</version>
  76. </dependency>
  77. <dependency>
  78. <groupId>org.slf4j</groupId>
  79. <artifactId>slf4j-api</artifactId>
  80. <version>1.7.32</version>
  81. </dependency>
  82. <dependency>
  83. <groupId>ch.qos.logback</groupId>
  84. <artifactId>logback-classic</artifactId>
  85. <version>1.2.10</version>
  86. </dependency>
  87. <!-- aop -->
  88. <dependency>
  89. <groupId>cglib</groupId>
  90. <artifactId>cglib</artifactId>
  91. <version>3.1</version>
  92. </dependency>
  93. <dependency>
  94. <groupId>aopalliance</groupId>
  95. <artifactId>aopalliance</artifactId>
  96. <version>1.0</version>
  97. </dependency>
  98. <dependency>
  99. <groupId>org.aspectj</groupId>
  100. <artifactId>aspectjweaver</artifactId>
  101. <version>1.9.19</version>
  102. </dependency>
  103. <!-- 工具 -->
  104. <dependency>
  105. <groupId>junit</groupId>
  106. <artifactId>junit</artifactId>
  107. <version>4.13.2</version>
  108. <scope>test</scope>
  109. </dependency>
  110. <dependency>
  111. <groupId>com.alibaba</groupId>
  112. <artifactId>fastjson</artifactId>
  113. <version>1.2.76</version>
  114. </dependency>
  115. <dependency>
  116. <groupId>org.apache.commons</groupId>
  117. <artifactId>commons-collections4</artifactId>
  118. <version>4.3</version>
  119. </dependency>
  120. <dependency>
  121. <groupId>org.apache.commons</groupId>
  122. <artifactId>commons-lang3</artifactId>
  123. <version>3.11</version>
  124. </dependency>
  125. <dependency>
  126. <groupId>cn.hutool</groupId>
  127. <artifactId>hutool-all</artifactId>
  128. <version>5.7.22</version>
  129. </dependency>
  130. <dependency>
  131. <groupId>com.fasterxml.jackson.core</groupId>
  132. <artifactId>jackson-databind</artifactId>
  133. <version>2.12.1</version>
  134. </dependency>
  135. <dependency>
  136. <groupId>commons-logging</groupId>
  137. <artifactId>commons-logging</artifactId>
  138. <version>1.1.1</version>
  139. </dependency>
  140. </dependencies>
  141. <build>
  142. <resources>
  143. <resource>
  144. <directory>src/main/java</directory>
  145. <includes>
  146. <include>**/*.xml</include>
  147. </includes>
  148. </resource>
  149. <resource>
  150. <directory>src/main/resources</directory>
  151. </resource>
  152. </resources>
  153. </build>
  154. </project>

2.3、使用

  1. /**
  2. * @Author : 一叶浮萍归大海
  3. * @Date: 2023/11/23 19:12
  4. * @Description: Spring整合单元测试
  5. */
  6. @RunWith(SpringJUnit4ClassRunner.class)
  7. @ContextConfiguration(locations = "classpath:applicationContext.xml")
  8. public class SpringJunitTest {
  9. @Resource
  10. private UserMapper userMapper;
  11. @Resource
  12. private UserService userService;
  13. @Test
  14. public void userMapperTest() {
  15. List<UserDO> userDOS = userMapper.listAllUser();
  16. System.out.println("userMapper = " + userMapper);
  17. System.out.println("userDOS = " + userDOS);
  18. }
  19. @Test
  20. public void userServiceTest() {
  21. List<UserDO> userDOS = userService.listAllUser();
  22. System.out.println("userService = " + userService);
  23. System.out.println("userDOS = " + userDOS);
  24. }
  25. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/375336
推荐阅读
相关标签
  

闽ICP备14008679号