JAVA全系列 教程
3762个小节阅读:7089k
目录
C语言快速入门
JAVA全系列 教程
面向对象的程序设计语言
Python全系列 教程
Python3.x版本,未来主流的版本
人工智能 教程
顺势而为,AI创新未来
大厂算法 教程
算法,程序员自我提升必经之路
C++ 教程
一门通用计算机编程语言
微服务 教程
目前业界流行的框架组合
web前端全系列 教程
通向WEB技术世界的钥匙
大数据全系列 教程
站在云端操控万千数据
AIGC全能工具班
A A
White Night
之前进行单元测试时都需要手动创建Spring容器,能否在测试时让Spring自动创建容器呢?
引入Junit和Spring整合依赖
xxxxxxxxxx
<!-- spring整合测试模块 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>6.0.11</version>
</dependency>
编写测试类
xxxxxxxxxx
// JUnit使用Spring方式运行代码,即自动创建spring容器。
@RunWith(SpringJUnit4ClassRunner.class)
// 告知创建spring容器时读取哪个配置类或配置文件
// 配置类写法:@ContextConfiguration(classes=配置类.class)
@ContextConfiguration(locations="classpath:applicationContext.xml")
public class StudentServiceTest {
@Autowired
private StudentService studentService;
@Test
public void testFindAll(){
List<Student> allStudent = studentService.findAllStudent();
allStudent.forEach(System.out::println);
}
}
注:使用SqlSessionTemplate创建代理对象还是需要注册接口或者映射文件的。
在MyBatis配置文件注册接口
xxxxxxxxxx
<configuration>
<mappers>
<mapper class="com.itbaizhan.dao.StudentDao"></mapper>
</mappers>
</configuration>
创建sqlSessionFactory时指定MyBatis配置文件
xxxxxxxxxx
<!-- 创建Spring封装过的SqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation" value="classpath:SqlMapConfig.xml"></property>
</bean>
1.在Spring中整合JUnit中,测试类添加注解表示使用Spring方式运行代码
A @RunWith(SpringJUnit4ClassRunner.class)
B @Test
C @ContextConfiguration
D @JUnit
答案
1=>A