Java中的公共方法返回私有类实例?

发布于 2024-10-26 04:05:26 字数 515 浏览 1 评论 0原文

我有一个返回私有类实例的方法,并且我需要从不同的包访问其方法以进行单元测试。这些类位于同一个文件中。 它是这样的:

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 中的任何方法是否可以以某种方式访问​​?我是否有义务将 B 和 C 放在不同的文件中并将其公开才能实现这一目标?

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;
    }
}

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

一指流沙 2024-11-02 04:05:26

您希望使用反射通过单元测试来测试私有方法。您可以在此处阅读更多信息(参见答案):

如何测试具有私有方法、字段或内部类的类?

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

How do I test a class that has private methods, fields or inner classes?

甜点 2024-11-02 04:05:26

最佳实践是始终将每个顶级类的源代码放在单独的文件中。如果不这样做,您可能会遇到编译等方面的困难。

还有一个单独的问题,即类 BC 是否> 需要是public,以便您可以创建单元测试。我想说这可能是个好主意,但是有几种方法可以解决这个问题:

  • 如果将单元测试放在与类 A相同的 Java 包中BC,那么这些类(及其方法)将对单元测试可见。

  • 出于测试目的,您可以通过创建 BCpublic 子类来公开这些方法。这些必须在与 ABC 类相同的 Java 包中声明。

  • 您可以创建 BC 的实例,并使用反射调用它们的方法。 (但这对于单元测试来说是一项繁重的工作,我不推荐这样做!)

请注意,如果您的产品代码和测试代码有单独的源目录树,您可以将测试类放入与 < code>A、BC 而不混淆代码库。当您为生产/发布而构建时,只需将测试树中的类保留在构建之外即可。


顺便说一句,在您的示例中,类 BC 是“包私有”而不是“私有”。对真正的“私有”类进行单元测试是一个完全不同的问题。

It is best practice to ALWAYS put the source code for each top level class in a separate file. If you don't do this, you may run into difficulties with compiling, etc.

There is a separate issue of whether the classes B and C need to be public so that you can create unit tests. I'd say it is probably a good idea, but there are a couple of ways around this:

  • If you put the unit tests in the same Java package as the classes A, B and C, then the classes (and their methods) will be visible to the unit tests.

  • You could expose the methods by creating public subclasses of B and C for test purposes. These have to be declare in the same Java package as the classes A, B and C.

  • You could create instances of B and C and call methods on them using reflection. (But this is a lot of work for a unit test, and I would NOT recommend it!)

Note that if you have separate source directory trees for your product code and your test code, you can put test classes into the same package as A, B and C without mixing up the codebases. When you build for production / release, you just leave the classes in the test tree out of the build.


By the way, in your example the classes B and C are "package private" rather than "private". Unit testing a real "private" class is a whole different problem.

若水般的淡然安静女子 2024-11-02 04:05:26

是的,您可以使用反射在任何地方调用BC任何实例中的所有方法,但是从我的角度来看,这是一种黑客行为而不是一种技术。

否则,类 BC 不会暴露在 A.class 之外。

Yes, you can use reflection to invoke all methods in any instance of B or C anywhere, but this, from my POV, is a hack rather than a technique.

Otherwise, class B and C are not exposed outside your A.class.

肩上的翅膀 2024-11-02 04:05:26

从技术上讲,这些类是受到保护的;因此,只要调用 getB 和 getC 的类位于同一个包中,就不会有任何问题。如果您尝试从另一个包运行 GetB GetC 您将看到一些问题,因为您的其他包将无法访问实际的类。

这里 TestProt 无法导入,因为它在 tester2 包中受到保护。
文件 1:

package tester; 
import tester2.Test; 
public class Testing {   
  public Testing() {
    Test t = new Test();
    TestProt p = t.prot();   
  } 
}

文件 2:

package tester2;
public class Test {
  public TestProt prot() {
    return new TestProt();
  } 
}

class TestProt{
  public TestProt() {
  } 
}

Technically the classes are protected; so as long as classes that call getB and getC are within the same package you won't have any issues. If from another package you tried to run GetB GetC you are going to see some issues because your other package will not be able to access the actual class.

Here TestProt CANNOT be imported since it is protected in the tester2 package.
FILE1:

package tester; 
import tester2.Test; 
public class Testing {   
  public Testing() {
    Test t = new Test();
    TestProt p = t.prot();   
  } 
}

FILE2:

package tester2;
public class Test {
  public TestProt prot() {
    return new TestProt();
  } 
}

class TestProt{
  public TestProt() {
  } 
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文