Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何编写返回布尔值的私有函数的JUnit测试?_Java_Private_Junit3 - Fatal编程技术网

Java 如何编写返回布尔值的私有函数的JUnit测试?

Java 如何编写返回布尔值的私有函数的JUnit测试?,java,private,junit3,Java,Private,Junit3,我已经为返回字符串的私有函数编写了JUnit测试。它工作得很好 public void test2() throws Exception { MyHandler handler = new MyHandler(); Method privateStringMethod = MyHandler.class.getDeclaredMethod("getName", String.class); privateStringMethod.setAccessible(true);

我已经为返回字符串的私有函数编写了JUnit测试。它工作得很好

public void test2() throws Exception
{
    MyHandler handler = new MyHandler();
    Method privateStringMethod = MyHandler.class.getDeclaredMethod("getName", String.class);
    privateStringMethod.setAccessible(true);
    String s = (String) privateStringMethod.invoke(handler, 852l);
    assertNotNull(s);
}
我还有一个返回布尔值的函数,但它不起作用。 但在这一点上,我得到了一个编译时错误,即
不能从对象强制转换为布尔值。

public void test1() throws Exception
{
    MyHandler handler = new MyHandler();
    Method privateStringMethod = MyHandler.class.getDeclaredMethod("isvalid", Long.class);
    privateStringMethod.setAccessible(true);
    boolean s = (boolean) privateStringMethod.invoke(handler, 852l);
    assertNotNull(s);
}

如何运行?

我完全反对单独测试私有方法。单元测试应该针对类的公共接口进行(因此会无意中测试私有方法),因为这是在生产环境中处理它的方式


我想在一些小的情况下,您需要测试私有方法,使用这种方法可能是正确的,但是每当我遇到一个我想要测试的私有方法时,我肯定不会放下所有多余的代码。

我完全反对孤立地测试私有方法。单元测试应该针对类的公共接口进行(因此会无意中测试私有方法),因为这是在生产环境中处理它的方式


我想在一些小的情况下,您需要测试私有方法,使用这种方法可能是正确的,但我肯定不会在遇到要测试的私有方法时放下所有冗余代码。

返回值将“自动装箱”到布尔对象。因为原语不能为null,所以不能针对null进行测试。因为自动装箱,所以不能调用Even.booleanValue()

但是关于测试私有方法,我和@alex.p的观点相同

public class Snippet {

    @Test
    public void test1() throws Exception {
        final MyHandler handler = new MyHandler();
        final Method privateStringMethod = MyHandler.class.getDeclaredMethod("isvalid");
        privateStringMethod.setAccessible(true);
        final Boolean s = (Boolean) privateStringMethod.invoke(handler);
        Assert.assertTrue(s.booleanValue());
    }

    class MyHandler {
        private boolean isvalid() {
            return false;
        }
    }
}

returnvalue将“自动装箱”到布尔对象。因为原语不能为null,所以不能针对null进行测试。因为自动装箱,所以不能调用Even.booleanValue()

但是关于测试私有方法,我和@alex.p的观点相同

public class Snippet {

    @Test
    public void test1() throws Exception {
        final MyHandler handler = new MyHandler();
        final Method privateStringMethod = MyHandler.class.getDeclaredMethod("isvalid");
        privateStringMethod.setAccessible(true);
        final Boolean s = (Boolean) privateStringMethod.invoke(handler);
        Assert.assertTrue(s.booleanValue());
    }

    class MyHandler {
        private boolean isvalid() {
            return false;
        }
    }
}

isvalid()返回布尔值还是布尔值?@Jim它返回布尔值。isvalid()返回布尔值还是布尔值?@Jim它返回布尔值。这如何回答问题?这如何回答问题?