SSM之Spring test模块

Spring和junit的整合

  • 在spring框架下,如何给类做单元测试?
    Junit+手动创建IOC容器
    Junit+spring test+自动创建IOC容器+自动注入(不用调用getBean)
  • 什么是spring-test模块
    spring提供的基于junit的测试模块,可以简化IOC创建,且可以使用注入
  • 如何使用
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class XxxTest

pom.xml

<dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>5.2.9.RELEASE</version>
        </dependency>

TestPersonServiceSpring

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class TestPersonServiceSpring {
    @Autowired
    IPersonService personService;

    @Autowired
    @Qualifier("personDaoImpl")
    IPersonDao personDao;
    @Test
    public void test01(){
        System.out.println(personService);
        //调了一个login
        Person person = new Person();
        boolean flag = personService.login(person);
        System.out.println(flag);
    }

    @Test
    public void test02(){
        Person person = new Person();
        boolean f = personDao.findByUserNameAndPassword(person);
        System.out.println(f);
    }
}
  • 注意junit的版本要求最低为4.12
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值