Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

testing an internal class

how to write unit tests to internal classes ???

like image 997
Chen Kinnrot Avatar asked Mar 03 '09 12:03

Chen Kinnrot


People also ask

Should I test internal classes?

You should unit test internal classes that provide shared functionality - some classes in your library provide a place for shared code. Although they will be tested indirectly through classes using them, an additional direct testing lets you distinguish between problems with the class itself vs.

What is an internal class in C#?

Internal, in C#, is a keyword used to declare the accessibility of a type or type member such that the access is limited to the assembly in which it is declared. An internal modifier is used to prevent the use of a public modifier, which allows access to other assemblies wherever necessary.

Should kotlin test classes be internal?

Setting a declaration as internal means that it'll be available in the same module only. By module in Kotlin, we mean a group of files that are compiled together. These won't be visible outside the current module. Internal Modifiers is useful when you need to hide specific library implementations from the users.


5 Answers

You write tests which specify the behaviour of the top-level class' external interface. Whether that class uses internal classes to implement that behaviour or not, is an implementation detail of the class, and the tests don't need to know anything about it.

If the internal class cannot be adequately tested through the top-level class' interface, then it's usually best to move the internal class out and test it directly as a new top-level class. Wanting to test internal classes is a code smell that the internal class might be significant enough to be a top-level class.

like image 132
Esko Luontola Avatar answered Oct 06 '22 01:10

Esko Luontola


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

like image 21
Kent Boogaart Avatar answered Oct 06 '22 00:10

Kent Boogaart


When using MS Visual Studio for Unit Tests you have to simply create a private Accessor. Internally it works with reflections i think. Just take a look at the generated code.

like image 29
Alexander Avatar answered Oct 06 '22 01:10

Alexander


You don't test it directly. It will be tested through the class where it is defined.

And, if you apply TDD, as this question tags currently implies, what is the test you just write that call for an inner class ? I mean can't it be a standard class, privately owned by the class you're working on ?

like image 25
philant Avatar answered Oct 06 '22 01:10

philant


We have used a helper class which uses reflection to load and call methods on internal classes. It is also possible to change the accessibility at compile time using the DEBUG symbol eg

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

However Esko Luontola's answer is more correct as it is the functionality or business requirements which are most important. It is easy to get too focused on code coverage rather than testing the important risk areas.

like image 22
bstoney Avatar answered Oct 06 '22 01:10

bstoney