0

I am trying to write tests for a piece of code that uses the inner class of the following object as an input (I've generalized the names).

public class MockOuterClass implements OuterClass, Mock {

  static class MockInnerClass implements InnerClass {
  //fields and methods of the nested class
  }

//methods of the outer class
}

Now since the inner class does not have a visibility tag, it defaults to "protected." Here lies my issue: since my tests and source code are in separate packages, how can I create an instance of this inner object? I attempted this:

MockOuterClass.MockInnerClass test = new MockOuterClass.MockInnerClass();

When I do this, Eclipse says that this line is unacceptable (which I assumed was the case, but it was wishful thinking) since MockInnerClass defaults to protected and thus cannot be used outside its package. Is there a way I can use this class somehow? I did not write the source code, so I am unsure if the lack of a "public" tag on the inner class was intentional or the programmer's mistake.

Thank you in advance.

1
  • 6
    No modifier means package visibility, not protected. But this does not solve your problem. You should better put your test into the same package than the class under test. Commented Aug 6, 2014 at 19:39

2 Answers 2

2

A bit of an aside, not an actual answer, but I have always put my tests into the same package as the classes being tested. At least, as much as possible, there are a few very rare cases where this is impossible.

Why are you using a different package? Is there a good reason to do so?

Now, back to an answer, if you insist on testing from a separate package, unless MockOuterClass exposes some method to construct (or expose) the MockInnerClass, you are stuck. (I guess you could try using Reflection but that is getting desperate.) However, as several have commented, the author of the class "intended" for the inner class to be hidden, a. la. Kent Beck.

In other words, if you follow Kent Beck, you should be writing tests that target MockOuterClass, NOT MockInnerClass. The inner class is a "non public detail".

Sign up to request clarification or add additional context in comments.

1 Comment

The reason I have the tests in a separate package is because this is a project I am doing at work, hence why I generalized the names. Thank you for your response- after looking at the code for a bit, I might just create an object using the interface the inner class implements instead.
0

If this code wasn't written by you and declares a non-public static class (side-note: it defaults to package-protected, which is very different to protected), chances are the writer intends to hide the class from your use. In short, there is no way to access the class from outside the package, and you probably shouldn't be trying to.

3 Comments

Since this is for testing, there is might be a valid need to access it
If there is ever a need to access a non-public class, it means that the writer of the non-public class should have declared it public.
This is an area of controversy, I think. Some authors (including Kent Beck) suggest that only the publicly visible behavior of a class should be tested, but others disagree. I tend to prefer the former, since adding unit tests that test private behavior put limits on the ability to improve the implementation of a class or rework it to add a new feature. Just my opinion...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.