Let me preface this by saying I'm pretty new to Go, so I am looking for mocking techniques when working with other libraries. I am well aware that interfaces and dependency injection are the best way to keep code testable and mockable.
While working with a 3rd party client library (Google Cloud Storage), I have run into a problem with attempting to mock the implementation of their client. The primary problem is that the types in the client library are not implemented with interfaces. I can generate interfaces to mimic the client implementation. However, the return values for some of the functions return pointers to underlying struct types which are tricky or impossible to mock due to private attributes. Here is a sample of the problem I am trying to solve:
package third_party
type UnderlyingType struct {
secret string
}
type ThirdPartyClient struct {}
func (f *ThirdPartyClient) SomeFunction() *UnderlyingType {
return &UnderlyingType{
secret: "I can't mock this, it's a secret to the package"
}
}
Here is an annotated sample with the problem I'm trying to solve.
package mock
// Create interface that matches third party client structure
type MyClientInterface interface {
SomeFunction() *third_party.UnderlyingType
}
type MockClient struct {
third_party.Client
}
// Forced to return the third party non-interface type 'UnderlyingType'
func (f *MockClient) SomeFunction() *UnderlyingType {
// No way to mock the value of the 'secret' property outside
// of the third-party package. Any underlying methods that
// depend on a non-nil reference to 'secret' will explode
// with the mock.
//
// TODO: Find a way to mock the 'secret' value
return &UnderlyingType{}
}
Is this even a mockable scenario? Are there special techniques to work around the fact that the library provides no interfaces as return types?