How to create mock objects for Google Analytics v4 SDK in unit tests

I’m working on unit tests for my Android app and I need to mock the Google Analytics v4 SDK components. My main challenge is with creating a mock for the com.google.android.gms.analytics.Tracker class using Mockito framework.

When I try to create a mock like this:

AnalyticsTracker mockTracker = Mockito.mock(AnalyticsTracker.class);

I get an error that prevents the mock from being created properly. Has anyone dealt with this issue before? I’m wondering if there are any specific strategies for mocking Google Analytics components or if I should consider creating a custom wrapper interface around the analytics functionality to make testing easier.

Been there. The wrapper approach works, but you don’t need to create all those interfaces manually.

I automated this when we hit the same testing nightmare across projects. Instead of hand-writing wrapper interfaces, I built something that auto-generates mock responses and handles analytics calls without touching the actual GA SDK during tests.

It intercepts analytics events, validates data structure, and returns predictable responses. No final class headaches, no PowerMock mess, and consistent test behavior everywhere.

Bonus: it logs all analytics calls so you can verify what events fired. Way cleaner than mocking individual tracker methods.

Set it up once and the whole team benefits. No more fighting Google’s SDK quirks.

Had this exact problem when we migrated our analytics testing suite last year. The Tracker class being final is definitely the root cause.

We fixed it using Mockito with the inline mock maker. Add this to your test resources in src/test/resources/mockito-extensions/org.mockito.plugins.MockMaker:

mock-maker-inline

This lets Mockito handle final classes without PowerMock overhead. Then your mock creation should work:

Tracker mockTracker = Mockito.mock(Tracker.class);

If you’re still having issues, the wrapper interface approach everyone mentioned works great. I usually name mine AnalyticsService and inject it via constructor. Makes testing way simpler and you can swap analytics providers later without touching your business logic.

Just remember to test your wrapper implementation separately with integration tests to make sure it actually calls GA correctly.

This tutorial covers PowerMock with Mockito for final classes if you want another approach, though I’d stick with the inline mock maker for cleaner tests.

same issues with GA v4, final classes like Tracker are hard to mock. I did the same - made a wrapper interface, AnalyticsHelper. Works way better for testing. give it a try!

You’re hitting this because Google Analytics classes are usually final or have messy dependencies that break Mockito. Instead of fighting the SDK directly, consider using PowerMock with Mockito. You’ll need to add PowerMock to your test dependencies, then use the @RunWith(PowerMockRunner.class) and @PrepareForTest(Tracker.class) annotations. This will allow you to mock final classes without issues. However, Laura219’s wrapper approach is a cleaner long-term solution. Create an interface that wraps your analytics calls, inject it into your classes, and mock the interface instead of Google’s concrete classes to avoid being tied to their specific implementation.