Make use of the interface feature. It may require some change to the dependencies (in this example, the package2
).
Change your code to something like this (the point is, to define several interfaces for better testability):
type Fetcher interface {
FetchAllData() ([]Data, error)
}
type Saver interface {
SaveData([]Data) (int, error)
}
func CreateData(input package1.InputRequest) (output package1.OututResponse){
..some code
..some code
// when you are writing tests, replace it with `s := NewMocking()`
s := package2.NewStorage()
DBdata, err := s.FetchAllData() //function to fetch data from database
..some code
..some code
id, insertErr := s.SaveData(someData) //function to insert data
..some code
..some code
}
A little change to the package2
is needed:
type MyStorage struct {}
func (s *MyStorage) FetchAllData ([]Data, error) {
// ... some fetching code
}
func (s *MyStorage) SaveData(someData []Data) (int, error) {
// ... some saving code
}
func NewStorage() *MyStorage {
// ... some initialization
}
When you want to do some tests without a requirement of package2
, define your own mocking type:
type Mocking struct {}
func (s *Mocking) FetchAllData ([]Data, error) {
// ... some mocking logic
}
func (s *Mocking) SaveData(someData []Data) (int, error) {
// ... some mocking logic
}
func NewMocking() *Mocking {
// ... initialize ...
}