---
title: "Discussion on \"Mocking Imported Libraries in Golang Unit Tests\" | Hashnode"
description: "Discussion on \"Mocking Imported Libraries in Golang Unit Tests\". In my last post, I covered mocking using interfaces. In this one, I'll take that a step further and show how we can ensure that we're able to mock every imported lib in our tested functions by using private variables.\ntl;dr: you can check out a repo ..."
url: "https://hashnode.com/posts/mocking-imported-libraries-in-golang-unit-tests/65783d3cbe2a26b51d52cc63"
---

[Feed](https://hashnode.com/feed) /

Discussion

# [Mocking Imported Libraries in Golang Unit Tests](https://colinj.hashnode.dev/mocking-imported-libraries-in-golang-unit-tests?utm_source=hashnode&utm_medium=feed)

In my last post, I covered mocking using interfaces. In this one, I'll take that a step further and show how we can ensure that we're able to mock every imported lib in our tested functions by using private variables. tl;dr: you can check out a repo ...

Dec 12, 2023 · 5 min read · colinj.hashnode.dev  
Read the article

-   [#unit-testing](https://hashnode.com/tag/unit-testing)
-   [#golang](https://hashnode.com/tag/golang)
-   [#mocking](https://hashnode.com/tag/mocking)
-   [#code-coverage](https://hashnode.com/tag/code-coverage)

## Written by

[Colin J Lacy](https://hashnode.com/@colinjlacy)

@colinjlacy

  
[View profile](https://hashnode.com/@colinjlacy)

A software engineer and recovering architect writing about things I find interesting code and cloud.

## Responses 1

Markdown supported · ⌘↵ to post

[ALFRED OKORONKWO](https://hashnode.com/@afredO)

[Feb 8, 2025](https://hashnode.com/posts/mocking-imported-libraries-in-golang-unit-tests/65783d3cbe2a26b51d52cc63/comment/67a7a3434025d9313c4afe06)

This is quite good. Clearly defines what it means to write maintainable code. I'd love to see how it would have handled FileInfo.IsDir() although it could be taken care of with fstest.

[Colin J Lacy](https://hashnode.com/@colinjlacy)

[Feb 9, 2025](https://hashnode.com/posts/mocking-imported-libraries-in-golang-unit-tests/65783d3cbe2a26b51d52cc63/comment/67a7a3434025d9313c4afe06#reply-67a886c484f916f9d10ec074)

Thanks! I'm glad you got something useful out of it. I haven't tried it, but in theory it should work with FileInfo.isDir() the same way, as long as the mock matches the function signature. But, to your point, I've been using fstest to create a mock file system whenever I have file calls that I have to test.

[ALFRED OKORONKWO](https://hashnode.com/@afredO)

[Feb 9, 2025](https://hashnode.com/posts/mocking-imported-libraries-in-golang-unit-tests/65783d3cbe2a26b51d52cc63/comment/67a7a3434025d9313c4afe06#reply-67a887ffcffe09ad02f35afc)

Yeah, should work if FileInfo is mocked and returned from osStats call

[Colin J Lacy](https://hashnode.com/@colinjlacy)

[Feb 9, 2025](https://hashnode.com/posts/mocking-imported-libraries-in-golang-unit-tests/65783d3cbe2a26b51d52cc63/comment/67a7a3434025d9313c4afe06#reply-67a8884ac920da73d7cf3f31)

[ALFRED OKORONKWO](https://hashnode.com/@afredO) Thanks for reading and commenting!
