Spring中单元测试
1 传统中的单元测试存在的问题
存在的问题:会导致进入死循环,当我们启动单元测试的时候会实例化这个类,然后回加载ioc容器,ioc容器发现这个组件会进行实例化,然后就进入到了死循环之中。
2 Spring中的单元测试
第一步:导入Spring的test包
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.2.8.RELEASE</version>
</dependency>
第二步:使用注解:
- @ContextConfiguration(locations=“classpath:applicationContext.xml”)
- @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations=""):使用他来只当Spring的配置文件的位置。
@RunWith:指定用哪种驱动进行的那原测试,默认使用的是junit。我们需要使用Spring的单元测试模块来执行@Test注解的测试方法。以前由Junit来执行。