public方法返回Java中的私有类实例? [英] public method returns private class instance in Java?

查看:189
本文介绍了public方法返回Java中的私有类实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个方法,返回一个私人类的实例,我需要访问其方法从不同的包进行单元测试。这些类存在于同一个文件中。
它就像这样:

I have a method that returns an instance of a private class and I need access to its methods from a different package for unit testing. These classes live in the same file. It goes like this:

file:A.java

public class A{
    B b;
    public B getB(){
        return b;
    }  
    public setB(B b){
        this->b = b;
   }
}

class B{
    C c;
    public C getC(){
        return c;
    }
    public setC(C c){
        this->c = c;
   }
}

class C{
    public int getInt(){
        return 1;
    }
}

所以...基本上问题是:的方法在B或C到达以某种方式?

So... Basically the question is: are any of the methods in B or C reachable somehow? Am I obligated to place B and C in different files and make them public to accomplish that?

推荐答案

你想使用Reflection来反映用单元测试测试私有方法。您可以在这里阅读更多内容(请参阅答案):

You want to use Reflection to test private methods with unit testing. You can read more here(see answer):

单元测试私有方法的最佳方法是什么?

这篇关于public方法返回Java中的私有类实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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