iplmania
iplmania

Reputation: 13

How to write junits for anonymous inner class?

I have searched for writing junits for anonymous inner class, but cannot find any solution. Is there a simple example for writing junits for anonymous inner class using Mockito?

Upvotes: 0

Views: 477

Answers (1)

henry
henry

Reputation: 6106

An anonymous inner class is an implementation detail, don't try and "write a test for it".

Instead write a test that describes the behaviour of the class that uses the inner class. It should be possible to fully exercise the code of the inner class from the parent's public interface, otherwise why is the code there?

Should ever you wish to change the implementation of this functionality the test will now support you in making that change. If you somehow "tested the inner class" you would need to also rewrite the test and it would make your job harder rather than easier.

Upvotes: 1

Related Questions