Reputation: 139921
I've been looking through a particular open source library in which all unit tests are defined as static nested class inside the class they test, for example:
public class Foo {
public int bar() { ... }
public static class UnitTest {
@Test
public void testBar() { ... }
}
}
I've never seen a project or Java codebase that does this before and I am really curious about the thinking behind it.
Is there any advantage over this pattern than having a separate FooTest
class in another source folder src/test/java
?
Is this a convention of projects that use Gradle as their build tool?
Upvotes: 1
Views: 1193
Reputation: 139921
As Jon Skeet suggested, I found the answer within the documentation of the project in question:
Hystrix has all of its unit tests as inner classes rather than in a separate /test/ folder.
Why?
- Low Friction
More information on the reasoning can be found in this blog post: JUnit Tests as Inner Classes
Upvotes: 6
Reputation: 166
There doesn't seem to be any advantage writing unit tests as a nested inner class besides access to private members. The disadvantages that I can think of are:
Also, according to the Gradle docs for the Java plugin, it recommends keeping tests in src/test/java so I don't think that this is a Gradle-specific convention at all.
Upvotes: 3