美文网首页
使用 Spring整合 Junit4进行测试

使用 Spring整合 Junit4进行测试

作者: 走在冷风中吧 | 来源:发表于2020-02-03 16:36 被阅读0次
  1. pom.xml
    <!--  Spring test -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>5.2.3.RELEASE</version>
            <scope>test</scope>
        </dependency>
<!--  junit的版本号需要大于等于 4.12 -->
 <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
  1. test 类

@RunWith(SpringJUnit4ClassRunner.class) //使用 junit4 测试
@ContextConfiguration(classes = {Config.class})  //这里可以使用配置文件 xml, 也可以支持配置类
public class MyTest02 {

    @Autowired  //直接通过spring注解可以直接获得需要的类对象
    private AccountService accountService;

 /**
*test01 是使用传统方法实现, 作对比用的
*/
    @Test 
    public void test01(){
        ConfigurableApplicationContext context = new AnnotationConfigApplicationContext(Config.class);
        AccountService bean = context.getBean(AccountService.class);
        List<AccountBean> all = bean.findAll();
        all.forEach(accountBean -> System.out.println(accountBean));
    }

/**
*    直接使用spring 工厂类对象
*/
    @Test
    public void test02(){
        List<AccountBean> all = accountService.findAll();
        all.forEach(accountBean -> System.out.println(accountBean));
    }

}

相关文章

网友评论

      本文标题:使用 Spring整合 Junit4进行测试

      本文链接:https://www.haomeiwen.com/subject/wznhxhtx.html