Java中的公共方法返回私有类实例?
我有一个返回私有类实例的方法,并且我需要从不同的包访问其方法以进行单元测试。这些类位于同一个文件中。 它是这样的:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您希望使用反射通过单元测试来测试私有方法。您可以在此处阅读更多信息(参见答案):
如何测试具有私有方法、字段或内部类的类?
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?
最佳实践是始终将每个顶级类的源代码放在单独的文件中。如果不这样做,您可能会遇到编译等方面的困难。
还有一个单独的问题,即类
B
和C
是否> 需要是public
,以便您可以创建单元测试。我想说这可能是个好主意,但是有几种方法可以解决这个问题:如果将单元测试放在与类
A
、相同的 Java 包中B
和C
,那么这些类(及其方法)将对单元测试可见。出于测试目的,您可以通过创建
B
和C
的public
子类来公开这些方法。这些必须在与A
、B
和C
类相同的 Java 包中声明。您可以创建
B
和C
的实例,并使用反射调用它们的方法。 (但这对于单元测试来说是一项繁重的工作,我不推荐这样做!)请注意,如果您的产品代码和测试代码有单独的源目录树,您可以将测试类放入与 < code>A、
B
和C
而不混淆代码库。当您为生产/发布而构建时,只需将测试树中的类保留在构建之外即可。顺便说一句,在您的示例中,类
B
和C
是“包私有”而不是“私有”。对真正的“私有”类进行单元测试是一个完全不同的问题。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
andC
need to bepublic
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
andC
, then the classes (and their methods) will be visible to the unit tests.You could expose the methods by creating
public
subclasses ofB
andC
for test purposes. These have to be declare in the same Java package as the classesA
,B
andC
.You could create instances of
B
andC
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
andC
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
andC
are "package private" rather than "private". Unit testing a real "private" class is a whole different problem.是的,您可以使用反射在任何地方调用B或C任何实例中的所有方法,但是从我的角度来看,这是一种黑客行为而不是一种技术。
否则,类 B 和 C 不会暴露在 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.
从技术上讲,这些类是受到保护的;因此,只要调用 getB 和 getC 的类位于同一个包中,就不会有任何问题。如果您尝试从另一个包运行 GetB GetC 您将看到一些问题,因为您的其他包将无法访问实际的类。
这里 TestProt 无法导入,因为它在 tester2 包中受到保护。
文件 1:
文件 2:
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:
FILE2: