如何使用JUnit和JMock测试抽象类的受保护方法 [英] How to test protected methods of abstract class using JUnit and JMock

查看:316
本文介绍了如何使用JUnit和JMock测试抽象类的受保护方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这种情况-我有接口(例如MyInterface)和简单的部分实现(AbstractMyInterface).后者添加了一些我想测试的受保护方法.

当前,我只是简单地手工编写一个模拟对象,该对象扩展了AbstractMyInterface并将受保护的方法导出为公共方法.有没有更简单的方法可以做到这一点-例如使用JMock + scripting?

解决方案

使用JUnit测试受保护的方法时,我看不到任何问题.只要测试的程序包结构反映了源树的结构,对于测试而言,除了private之外的其他方法都是可见的.

当然,如果要测试的实现是抽象的,则必须自己创建要测试的类的普通子类(或者,如果更适合您的目的,则通过一些模拟库来实现).同样在这种情况下,无需创建仅用于调用受保护可见性方法的公共方法层.仅对于私有方法,此策略不起作用.但是无论如何,经常需要测试私有方法是设计问题的征兆.

例如: 要测试的类位于src/mypackage/AbstractClass.java 打包mypackage;

/** This could as well implement some interface, 
    but that does not change a thing */
public abstract class AbstractClass {
    protected int returnsOne() {
        return 1;
    }
}

然后位于test/mypackage/AbstractClassTest.java的测试

package mypackage;

import org.junit.Test;

import static junit.framework.Assert.assertEquals;

public class AbstractClassTest {
    @Test
    public void returnsOneReturnsOne() {
        AbstractClass instanceToTest = new AbstractClassTestable();
        assertEquals(1, instanceToTest.returnsOne());
    }
}

/** This is needed, because we cannot construct abstract class directly */
class AbstractClassTestable extends AbstractClass {
}

I have such situation - I have interface (say MyInterface) and simple partial implementation (AbstractMyInterface). The latter adds a few protected methods which I would like to test.

Currently I simply write by hand a mock object which extends AbstractMyInterface and export protected methods as public. Is there a simpler way of doing this - for example using JMock+scripting?

解决方案

I cannot see any problem with testing protected methods with JUnit. As long as package structure for tests mirrors source tree structure, methods other than private are visible for tests.

Of course if implementation to test is abstract, you have to create normal subclass of class under test by yourself (or do it via some mocking library if fits better for your purposes). Also in such a case, no need to create layer of public methods for just for calling protected visibility methods. Only for private methods this strategy does not work. But often need to test private methods is sign of design problem anyway.

For example: Class to test located to src/mypackage/AbstractClass.java package mypackage;

/** This could as well implement some interface, 
    but that does not change a thing */
public abstract class AbstractClass {
    protected int returnsOne() {
        return 1;
    }
}

And test which is located to tests/mypackage/AbstractClassTest.java

package mypackage;

import org.junit.Test;

import static junit.framework.Assert.assertEquals;

public class AbstractClassTest {
    @Test
    public void returnsOneReturnsOne() {
        AbstractClass instanceToTest = new AbstractClassTestable();
        assertEquals(1, instanceToTest.returnsOne());
    }
}

/** This is needed, because we cannot construct abstract class directly */
class AbstractClassTestable extends AbstractClass {
}

这篇关于如何使用JUnit和JMock测试抽象类的受保护方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆