Spring整合Junit的配置
步骤:
1. 导入spring整合junit的jar(坐标)和junit的jar包
2. 使用Junit提供的一个注解把原有的main方法替换了。替换成spring提供的
* @@RunWith(SpringJUnit4ClassRunner.class)
3. 告诉soring运行期,spring和ioc创建是基于xml还是注解的,并且说明位置
* @ContextConfiguration()
* locations: 指定xml文件的位置,加上classpath关键字,表示在类路径下
* eg:@ContextConfiguration(locations = "classpath:bean.xml")
* 注意:"classpath:此处不能有空格bean.xml"
* classes:指定注解类所在地位置.class
* eg: @ContextConfiguration(classes= SpringConfig.class)
* 注意:使用spring5.x版本,整合junit时,要求junit的jar包必须是4.12或者以上版本,否则会报错。
1. 在pox.xml中导入spring整合junit的jar(坐标)和junit的jar包
<!--spring依赖的jar包-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.12.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.1.12.RELEASE</version>
</dependency>
<!--junit依赖的jar包-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
2. 使用Junit提供的一个注解把原有的main方法替换了,替换成spring提供的
3. 告诉soring运行期,spring和ioc创建是基于xml还是注解的,并且说明位置
如果是基于xml配置:
如果是基于注解配置: