When we look at your class MyClass
and how it would later be used, this would have the following structure:
+---------------+ +--------------+ +----------------+
|Production Code| +-------> |MyClass.init()| +-------> |getApplication()|
+---------------+ +--------------+ +----------------+
Now, to test the MyClass.init()
method, a useful test setup might look as follows:
+---------+ +--------------+ +-----------------------+
|Test Code| +-------------> |MyClass.init()| +-------> |mocked getApplication()|
+---------+ +--------------+ +-----------------------+
The test code would call the MyClass.init()
method, because that is the method you want to test. You don't want to test the other classes / methods that are called by MyClass.init()
, ahd thus it might be a good option to mock these. (Note that this is not mandatory, sometimes you just can also reach all your unit-testing goals with the other components just used as they are without mocking.)
However, what you have done is the following:
+---------+ +---------------------+
|Test Code| +-------------> |mocked MyClass.init()|
+---------+ +---------------------+
And now you can clearly see, why you also do not see any covered lines in the original MyClass.init()
, because that just does not get called.