Hi,

I have a problem, I have written a class, inside another class. No problem with that, but I have now come to develop a unit test for the outer class. The JUnit must be able to test the methods of the inner class as well as all other class methods. However, I can not seem to access the methods, or even reference the class.

Please can someone point me in the correct direction.

Thanks

Dani AI

Generated

A few practical notes for anyone who hits the same snag when writing JUnit tests for a nested class. Nested types in Java behave differently depending on whether they are static or non-static and on their access modifier. When the nested type is an implementation detail of a GUI (like a helper inside a JFrame), prefer testing observable behavior through the outer class; only test the nested class directly if it contains significant, independent logic.

Common ways to access a nested class from a test (replace names with actual package/class names):

// non-static inner instance (requires an outer instance)
Outer outer = new Outer();
Outer.Inner inner = outer.new Inner();

// static nested class
Outer.Inner staticInner = new Outer.Inner();

// reflective access for private or otherwise inaccessible nested types
Class<?> innerClass = Class.forName("com.pkg.Outer$Inner");
Constructor<?> ctor = innerClass.getDeclaredConstructor(Outer.class /*, other param types */);
ctor.setAccessible(true);
Object innerInstance = ctor.newInstance(outer /*, other args */);
Method m = innerClass.getDeclaredMethod("somePrivateMethod", paramTypes);
m.setAccessible(true);
m.invoke(innerInstance, args);

Design and maintenance guidance: prefer black‑box tests that verify behaviour via public APIs. If the nested class needs standalone tests, consider extracting it as a package‑private top‑level class so tests in the same package can instantiate it naturally. Use reflection only as a last resort — it’s brittle and restricted by the Java module system (Java 9+) unless you open the module for testing. For annotations like Guava’s @VisibleForTesting, the pattern documents intent but does not change access rules.

Thanks to for the direction about instance-vs-static, and to for confirming the issue was resolvable. For authoritative details on nested classes and their rules see the Oracle tutorial on nested classes: Oracle Java tutorial: Nested classes.

Recommended Answers

All 2 Replies

Why do you need to write tests that don't adhere to the access scope of the inner class? If it's not a static inner class, you should have an instance of the enclosing class to work from. If that's not the case, then perhaps your inner class should actually be a public top-level class?

Hi there, the class functions perfectly, and the app as well. the class was made for the sol purpose of that JFrame, as such it was easier to make it a nested class.

However, I have now solved the problem. Thank you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.