Inner class testing

how to write unit tests for inner classes ???

+5
unit-testing tdd rhino
Mar 03 '09 at 12:03
source share
6 answers

You write tests that determine the behavior of the front end of a top-level class. Whether this class is inner classes to implement this behavior or not is a detail of the implementation of this class, and tests should not know anything about it.

If the inner class cannot be adequately tested through the interface of the top-level class, then it is usually best to move the inner class and test it as a new top-level class. The desire to test inner classes is the smell of code that an inner class can be significant enough to be a top-level class.

+15
03 Mar. '09 at 12:10
source share

Not that I recommend it, but you can also use InternalsVisibleToAttribute .

+5
Mar 03 '09 at 12:19
source share

You do not check it directly. It will be tested through the class where it is defined.

And, if you are using TDD, as tags currently imply, which test are you writing, call the inner class? I mean, could it not be a standard class, privately owned by the class you are working on?

+2
03 Mar. '09 at 12:15
source share

When using MS Visual Studio for unit tests, you just need to create a private Accessor . Inside, he works with the thoughts that I think. Just take a look at the generated code.

+1
03 Mar. '09 at 12:10
source share

We used a helper class that uses reflection to load and call methods on inner classes. It is also possible to change accessibility at compile time using the DEBUG symbol, for example

#if DEBUG public #else internal #endif class MyInternalClass { ... } 

However, Esko Luontola's answer is more correct, since it is more important than functionality or business requirements. It's easy to focus too much on code coverage rather than testing important areas of risk.

+1
Mar 03 '09 at 12:15
source share
0
Jun 30 2018-11-11T00:
source share



All Articles