Skip to content

Add ability to override/mock the Client #1810

@erawhctim

Description

@erawhctim

Description

It's difficult to write unit tests for Bugsnag functions due to how the SDK is built and how the Client is initialized/managed.

#103 describes a solution - building a wrapper class around the Bugsnag SDK and mocking that in tests - which is a good strategy that works well for most testing use cases. However, when we want to actually unit test our Bugsnag SDK wrapper class, we need a way to mock Bugsnag's internals, which is difficult to do today (especially in a plain JVM unit test without access to Android APIs/Context/etc.

Describe the solution you'd like

The ability to set an arbitrary Client implementation within Bugsnag, so that Bugsnag.addOnError and/or Bugsnag.notify can be called without the SDK complaining about start not being called (or requiring an API key).
If Client was an interface that we could override, I think that would be a great start.

Additionally, it would be even better if there was an optional bugsnag-test artifact that provided a MockClient implementation to use and assert upon in tests.

Describe alternatives you've considered

  • Static mocks on Context and Bugsnag, essentially mimicking the behavior of the SDK and setting no-op's or reasonable defaults. This works, but it's tedious.
  • The recommendation in this comment (always set up the SDK with the API key, and just use release stages to gate the error upload functionality)
    • This is closest to the approach we landed on

Additional context

N/A

Metadata

Metadata

Assignees

No one assigned

    Labels

    backlogWe hope to fix this feature/bug in the futurefeature requestRequest for a new feature

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions