In the Golang project under test, there’s a that a file into a variable. Its code is like this:

// Load the JSON config file
func Load(configFile string, outputObj interface{}) *errors.ErrorSt {
    var err error
    // Read the config file
    jsonBytes, err := ioutil.ReadFile(configFile)
    if err != nil {
        return errors.File().AddDetails(err.Error())

    // Parse the config
    if err := json.Unmarshal(jsonBytes, outputObj); err != nil {
        return errors.JSON().AddDetails("Could not parse" + configFile + ": " + err.Error())
    return nil


I wish to test it but I don’t know if I should create fake JSON file for the test cases, or just mock the whole function. My Java background has me leaning towards the latter.

Exploring that, I found the testify framework I’m using has a package for mocking methods, but what I’m attempting to mock doesn’t belong to interfaces (the pitfalls of non-OOP languages!!)

Source link


Please enter your comment!
Please enter your name here