java单元测试技巧

本文介绍了如何在JUnit4中使用Test装饰器指定断言类型,以及如何通过反射访问和操作被测代码的protected字段和方法,包括设置字段可访问性和构造方法实例化。
摘要由CSDN通过智能技术生成

一、Test装饰器指定断言类型

在JUnit 4中,你可以使用expected属性在@Test注解中声明期望的异常类型。如:

@Test(expected = Exception.class)
public void testSqlSessionFactoryBeanWithNullDataSource() throws Exception {

二、利用反射来访问被测代码的protected字段

被测代码的字段定义下所示

public class MetaVoExample {
    protected String orderByClause;

    protected boolean distinct;

    protected List<Criteria> oredCriteria;

现在在单元测试代码中想对oredCriteria进行实例化
如果oredCriteria字段没有protected 声明,那么常规的实例方法如下

@Before
    public void setUp() throws Exception {
        MockitoAnnotations.initMocks(this);
        metaVoExample.oredCriteria = new ArrayList<>();

但是有protected 声明会导致无法直接调用,提示’oredCriteria’ has protected access in ‘com.my.blog.website.modal.Vo.MetaVoExample’
尝试直接访问MetaVoExample类中的oredCriteria字段,但该字段被声明为受保护的(protected访问级别),因此不能被外部类直接访问。如果你的测试类不在相同的包下或者不是MetaVoExample的子类,你将无法直接访问这个字段。

为了绕过这个问题,可以使用Java反射API来访问和修改oredCriteria字段
修改后的代码如下所示

    @Before
    public void setUp() throws Exception {
        MockitoAnnotations.initMocks(this);
        // 获取MetaVoExample类中的oredCriteria字段
        Field oredCriteriaField = MetaVoExample.class.getDeclaredField("oredCriteria");

        // 使该字段变得可访问
        oredCriteriaField.setAccessible(true);

        // 创建一个新的ArrayList实例并设置到oredCriteria字段
        oredCriteriaField.set(metaVoExample, new ArrayList<>());

        // 获取修改后的oredCriteria字段值
        List<Criteria> oredCriteria = (List<Criteria>)oredCriteriaField.get(metaVoExample);

ps:如果你在使用getDeclaredField时提示Unhandled exception: java.lang.NoSuchFieldException异常,那么大概率是因为测试方法中缺少异常处理导致的。在测试方法中使用反射时,你也需要处理或者抛出可能的异常,即便我们知道自己写的getDeclaredField传参没有问题不会出错,如下所示:

    @Test
    public void xxx() throws NoSuchFieldException {

三、利用反射来访问被测代码的protected方法

被测代码的格式大体如下所示:

public class MetaVoExample {
    protected String orderByClause;

    protected boolean distinct;

    protected List<Criteria> oredCriteria;
    
    ...此处省略部分内容...

    protected abstract static class GeneratedCriteria {
        protected List<Criterion> criteria;

        protected GeneratedCriteria() {
            super();
            criteria = new ArrayList<Criterion>();
        }

    public static class Criteria extends GeneratedCriteria {

        protected Criteria() {
            super();
        }
    }

需要在单元测试中访问的是公共类Criteria下的protected无参构造方法Criteria()

利用反射机制,访问的代码如下:

        // 创建一个 MetaVoExample 实例
        MetaVoExample metaVoExample = new MetaVoExample();
        // 通过反射获取 MetaVoExample.Criteria 的构造方法
        Constructor<MetaVoExample.Criteria> constructor = MetaVoExample.Criteria.class.getDeclaredConstructor();

        // 设置构造方法是可访问的,即使它是 protected
        constructor.setAccessible(true);
        
        MetaVoExample.Criteria expectedCriteria = constructor.newInstance();
  • 9
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值