首页 > 其他分享 >Spring 整合 Junit

Spring 整合 Junit

时间:2022-12-24 21:00:10浏览次数:30  
标签:RunWith 配置文件 Spring 指定 ContextConfiguration class 整合 注解 Junit

1、导入依赖

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>5.3.24</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>

2、使用@RunWith 注解替换原有运行器

@RunWith(SpringJUnit4ClassRunner.class)
public class AccountServiceTest {
}

3、用@ContextConfiguration 指定 spring 配置文件的位置

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations= {"classpath:bean.xml"})
public class AccountServiceTest {
}

@ContextConfiguration 注解:
    locations 属性:用于指定配置文件的位置。如果是类路径下,需要用 classpath:表明
    classes 属性:用于指定注解的类。当不使用 xml 配置时,需要用此属性指定注解类的位置

标签:RunWith,配置文件,Spring,指定,ContextConfiguration,class,整合,注解,Junit
From: https://www.cnblogs.com/yanshiheng/p/17003385.html

相关文章