The only thing that came to my mind is to not write it myself. Here is an implementaion. If you are not as lazy as I am, you can mock the class using mockito.
Is there a cleaner way to fake the Analytics API for unit tests
-
06-10-2022 - |
Question
I'm attempting to create a fake to use in my test cases that replaces the google analytics class com.google.api.services.analytics.Analytics
. I will use it to make assertions about the queries that run, and return custom responses to ensure that the resulting data is properly handled.
I cannot extend the Analytics class because its only public constructor seems to do real work. Therefore I created an interface that mirrors the Analytics functionality. I must create the real analytics class with a Builder, so I created a wrapper class that implements my Analytics interface and sends the calls into the real deal.
At this point, it seems that the inner class Analytics.Data is not static, and therefore I cannot extend it due to "No enclosing instance of type Analytics is available due to some intermediate constructor invocation".
Therefore the obvious solution is to create a custom interface that recreates all of the Analytics.Data methods, and create another wrapper from my interface to the Analytics.Data class.
This is becoming a lot of useless classes in the name of testability, but the only other means of mocking I can think of is to mock HttpTransport, which itself is a gigantic interface and will require a very detailed analysis of things I do not care about to make assertions. Additionally, return values require valid JSON (actually not a big deal) and header values. And after all of that work, all of my tests will be dependent on the implementation of this api client lib version.
Am I missing anything obvious about how to cleanly intercept the layer between my class and the google library?
La solution