spring test配合junit4 实现单元测试

引入依赖

<!--下面两个是测试相关的jar包-->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-test</artifactId>
    <version>5.1.5.RELEASE</version>
</dependency>
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.13</version>
    <scope>test</scope>
</dependency>

测试类写法一:

import com.imooc.spring.jdbc.entity.Employee;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext.xml")
public class EmployeeDaoTest {

    @Autowired
    private EmployeeDao employeeDao;

    @Test
    public void findById() {
        Employee employee = employeeDao.findById(3308);
        System.out.println(employee);
    }
}

测试类写法二:

import com.imooc.spring.jdbc.entity.Employee;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class EmployeeDaoTest {
    @Test
    public void findById() {
        ApplicationContext context =
                new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
        EmployeeDao employeeDao = context.getBean("employeeDao", EmployeeDao.class);
        Employee employee = employeeDao.findById(3308);
        System.out.println(employee);
    }
}

相关推荐

  1. spring test配合junit4 实现单元测试

    2024-04-10 15:38:02       15 阅读
  2. spring 单元测试 Junit

    2024-04-10 15:38:02       38 阅读
  3. 单元测试框架jUnit

    2024-04-10 15:38:02       42 阅读
  4. 单元测试框架 Junit

    2024-04-10 15:38:02       25 阅读
  5. 浅谈单元测试JUnit4使用

    2024-04-10 15:38:02       21 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-04-10 15:38:02       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-04-10 15:38:02       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-04-10 15:38:02       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-04-10 15:38:02       20 阅读

热门阅读

  1. 【springboot】项目启动时打印全部接口方法

    2024-04-10 15:38:02       15 阅读
  2. SAP-QM-UD自动完成

    2024-04-10 15:38:02       13 阅读
  3. docker基于alpine制作arm架构下的jdk8的镜像

    2024-04-10 15:38:02       13 阅读
  4. mysql 8.0 常用函数大全总结,并列出实例

    2024-04-10 15:38:02       13 阅读
  5. Vue数据修改异步渲染原理分析

    2024-04-10 15:38:02       11 阅读
  6. 正则表达式

    2024-04-10 15:38:02       14 阅读
  7. redis的过期策略和内存淘汰机制(redis篇)

    2024-04-10 15:38:02       15 阅读
  8. LeetCode-45. 跳跃游戏 II【贪心 数组 动态规划】

    2024-04-10 15:38:02       14 阅读
  9. 题目 2305: 等差数列

    2024-04-10 15:38:02       14 阅读
  10. 蓝桥杯之初等数论(一)

    2024-04-10 15:38:02       14 阅读
  11. PTA 三足鼎立

    2024-04-10 15:38:02       14 阅读
  12. 1354: 【C4】【搜索】【回溯】字母全排列

    2024-04-10 15:38:02       11 阅读
  13. 初识Linux:探索开源世界的大门

    2024-04-10 15:38:02       15 阅读
  14. 算法刷题day42

    2024-04-10 15:38:02       11 阅读