Reputation: 33
JUnit 4.x does not provide for private methods to have test methods generated for them. I was looking in the junit source code (downloaded from github) for where this is enforced in the code but I was not able to locate it. I did find where it checks that test methods themselves are public, but I am looking for where it either explicitly checks a target class's methods' modifiers or otherwise omits or skips processing private methods of the target class. Does anyone know where this code might be located?
ADDENDUM:
OK so, we've been having a nice little chat on this and here's what's been learned. Suppose you have a class foo with two methods, bar and baz, which are both private. Ordinarily using a combination of some IDE and JUnit4.x you cannot make JUnit to generate test stubs for these methods on account of their being private. This rule "do not generate test stubs for private methods" is enforced at the level of the IDE and is not a part of JUnit per se.
So with a class like this:
public class Foo
{
private void bar(){}
private void baz(){}
}
you can't get this generated:
public class FooTest{
@Test
public void testBar(){}
@Test
public void testBaz(){}
}
But if you alter your IDE, leaving JUnit alone completely you can get those test methods generated. Note that JUnit WILL check in its Runner if the methods marked with @Test are public and refuse to run them if they're not, but that's a different concern.
Upvotes: 2
Views: 91
Reputation: 280112
The actual check is in FrameworkMethod#validatePublicVoid(boolean, List<Throwable>)
.
Runner
implementations that extend from ParentRunner
make a call to validate()
in the ParentRunner
constructor. That validate()
is private
but it delegates to a protected
method which implementations override called collectInitializationErrors(List<Throwable>)
. BlockJUnit4ClassRunner
's implementation calls validateInstanceMethods(errors);
(which is deprecated, so it's weird) that ends up calling the method above.
Confused edit:
I don't know what you're asking anymore. There's nothing special about JUnit. It's just a library. You write Java code that has a number of annotations that make sense to JUnit. JUnit cannot prevent you from writing code.
JUnit or you (or your IDE) provide a class that has a main
method.
It gets executed like so
java -cp <some class path> TheClassWithMain <more arguments>
just like any other java
program.
The JUnit classes, ie. ParentRunner
, its children, and others related, use reflection to scan your provided test classes. They do this to build up a test suite, ie. setup methods, test methods, tear down methods, thrown exception expectations, assertion expectations, etc.
The JUnit API has very specific documentation about what is allowed and what isn't. You can find the javadoc here. For @Test
for example, it says
The Test annotation tells JUnit that the
public void
method to which it is attached can be run as a test case.
This is true regardless of your IDE.
Remember, these are not compile time checks, they are run time checks that are the result of reflective analysis of the code.
Now, your IDE, whatever it is, might have a JUnit plugin that does these checks statically, ie. doing static analysis of your code, not run time analysis. It can do this because the source code is available.
If your question is about IDE behavior, you'll need to specify the IDE you're using. If your question is about how does JUnit prevent private
@Test
methods from being run, the answer is above.
Upvotes: 3
Reputation: 8938
Private methods in a class are not visible outside the class, so they are not visible to a JUnit test class. That is how the Java language works and JUnit does not have to have any code to check that.
If you want the private methods to be visible for testing, but not for other purposes, one common solution is instead to make them default scope - sometimes called "package private" - by omitting the scope from the declaration:
void function() {...
instead of
private void function() {...
If you do that, JUnit test classes in the same package will have access to them for testing.
Upvotes: 0