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

猜你喜欢

转载自blog.csdn.net/xinxin_____/article/details/109043937