{"id":332,"date":"2026-05-08T09:00:00","date_gmt":"2026-05-08T03:30:00","guid":{"rendered":"https:\/\/smitabharat.com\/blog\/go-mocking-interfaces-testing\/"},"modified":"2026-10-10T21:21:15","modified_gmt":"2026-10-10T15:51:15","slug":"go-mocking-interfaces-testing","status":"publish","type":"post","link":"https:\/\/smitabharat.com\/blog\/go-mocking-interfaces-testing\/","title":{"rendered":"Go Mocking with Interfaces for Unit Tests"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Go mocking lets you test code that depends on slow services, such as a weather API, without calling them. In this post, you will hide a dependency behind an interface and swap in a fake during tests. Then you will test the real HTTP code with a local test server.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You need two earlier lessons here. First, review <a href=\"https:\/\/smitabharat.com\/blog\/go-unit-testing-guide\/\">Go unit testing for beginners<\/a> for table-driven tests. Second, revisit <a href=\"https:\/\/smitabharat.com\/blog\/go-interfaces-examples\/\">interfaces in Go with examples<\/a>, because interfaces make Go mocking possible.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What Is Go Mocking?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A mock, or fake, is a pretend version of a dependency that you control completely. For example, a fake weather service can say &#8220;40 degrees&#8221; on demand, even in winter. As a result, your tests run fast, never fail because of the network and can cover rare cases easily.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Think of a flight simulator. Pilots practise engine failures in the simulator, not in a real plane. Similarly, a fake lets you practise errors without breaking anything real.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 1: Depend on a Small Interface<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ TempSource is the small interface our code depends on.\ntype TempSource interface {\n\tTemp(city string) (float64, error)\n}\n\ntype Advisor struct {\n\tSource TempSource\n}\n\nfunc (a Advisor) Advice(city string) (string, error) {\n\tt, err := a.Source.Temp(city)\n\tif err != nil {\n\t\treturn \"\", fmt.Errorf(\"advice for %s: %w\", city, err)\n\t}\n\tswitch {\n\tcase t &gt;= 35:\n\t\treturn \"Stay indoors and drink water\", nil\n\tcase t &lt;= 10:\n\t\treturn \"Wear a warm jacket\", nil\n\tdefault:\n\t\treturn \"Enjoy the day\", nil\n\t}\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>Advisor<\/code> never mentions HTTP. Instead, it only knows that something can return a temperature. Because of that, both the real API client and a test fake can plug in. The diagram below shows the two options side by side.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"640\" src=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-mocking-interfaces-testing-diagram-1024x640.png\" alt=\"Go Mocking diagram showing an Advisor that depends on an interface implemented by a real client and a test fake\" class=\"wp-image-327\" srcset=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-mocking-interfaces-testing-diagram-1024x640.png 1024w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-mocking-interfaces-testing-diagram-300x188.png 300w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-mocking-interfaces-testing-diagram-768x480.png 768w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-mocking-interfaces-testing-diagram-1536x960.png 1536w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-mocking-interfaces-testing-diagram-2048x1280.png 2048w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><em>Advisor depends only on the TempSource interface, so production plugs in the HTTP client while tests plug in a fake.<\/em><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 2: Write a Fake for Go Mocking<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>type fakeSource struct {\n\ttemp  float64\n\terr   error\n\tcalls []string\n}\n\nfunc (f *fakeSource) Temp(city string) (float64, error) {\n\tf.calls = append(f.calls, city)\n\treturn f.temp, f.err\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The fake returns whatever the test asks for. Also, it records each call, so a test can check how the code used it. This hand-written style needs no library at all.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 3: Test Every Branch with Go Mocking<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>func TestAdvice(t *testing.T) {\n\ttests := []struct {\n\t\tname string\n\t\ttemp float64\n\t\twant string\n\t}{\n\t\t{\"hot\", 40, \"Stay indoors and drink water\"},\n\t\t{\"cold\", 5, \"Wear a warm jacket\"},\n\t\t{\"mild\", 24, \"Enjoy the day\"},\n\t}\n\tfor _, tt := range tests {\n\t\tt.Run(tt.name, func(t *testing.T) {\n\t\t\tfake := &amp;fakeSource{temp: tt.temp}\n\t\t\tgot, err := Advisor{Source: fake}.Advice(\"Pune\")\n\t\t\tif err != nil {\n\t\t\t\tt.Fatal(err)\n\t\t\t}\n\t\t\tif got != tt.want {\n\t\t\t\tt.Errorf(\"got %q, want %q\", got, tt.want)\n\t\t\t}\n\t\t\tif len(fake.calls) != 1 || fake.calls[0] != \"Pune\" {\n\t\t\t\tt.Errorf(\"calls = %v\", fake.calls)\n\t\t\t}\n\t\t})\n\t}\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Each case sets a different temperature, so every branch of the switch runs. In addition, we check that the code asked for the right city exactly once.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Testing the Error Path<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>func TestAdviceError(t *testing.T) {\n\tfake := &amp;fakeSource{err: ErrNotFound}\n\t_, err := Advisor{Source: fake}.Advice(\"Atlantis\")\n\tif !errors.Is(err, ErrNotFound) {\n\t\tt.Fatalf(\"got %v, want ErrNotFound\", err)\n\t}\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Errors are hard to trigger with a real service. With Go mocking, however, one field makes the fake fail on purpose.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 4: Test the Real Client with httptest<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>func TestHTTPSource(t *testing.T) {\n\tsrv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {\n\t\tif r.URL.Query().Get(\"city\") == \"Pune\" {\n\t\t\tfmt.Fprint(w, `{\"celsius\": 31.5}`)\n\t\t\treturn\n\t\t}\n\t\thttp.NotFound(w, r)\n\t}))\n\tdefer srv.Close()\n\n\tsrc := HTTPSource{BaseURL: srv.URL, Client: srv.Client()}\n\tgot, err := src.Temp(\"Pune\")\n\tif err != nil || got != 31.5 {\n\t\tt.Fatalf(\"got %v, %v\", got, err)\n\t}\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The real <code>HTTPSource<\/code> still needs a test. So <code>httptest.NewServer<\/code> starts a tiny local server with your own answers. Therefore, you exercise the real <a href=\"https:\/\/smitabharat.com\/blog\/go-http-client-guide\/\">Go HTTP client<\/a> code without the internet.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ go test -v -cover .\n--- PASS: TestAdvice (0.00s)\n    --- PASS: TestAdvice\/hot (0.00s)\n    --- PASS: TestAdvice\/cold (0.00s)\n    --- PASS: TestAdvice\/mild (0.00s)\n--- PASS: TestAdviceError (0.00s)\n--- PASS: TestHTTPSource (0.00s)\ncoverage: 88.2% of statements<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Common Mistakes with Go Mocking<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Error you see<\/th><th>Why it happens<\/th><th>How to fix it<\/th><\/tr><\/thead><tbody><tr><td>fakeSource does not implement TempSource (wrong type for method Temp)<\/td><td>Because the fake&#8217;s method signature differs.<\/td><td>So copy the interface method exactly.<\/td><\/tr><tr><td>fakeSource does not implement TempSource (method Temp has pointer receiver)<\/td><td>Because a value was used where a pointer was needed.<\/td><td>So pass <code>&amp;fakeSource{}<\/code>.<\/td><\/tr><tr><td>Tests break after every refactor<\/td><td>Because the interface is too big.<\/td><td>So keep interfaces small and owned by the caller.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">A good rule is &#8220;accept interfaces, return structs&#8221;. In other words, functions take small interfaces as inputs, which keeps them easy to test. Testing questions appear in most Go interviews, so practise with my <a href=\"https:\/\/smitabharat.com\/blog\/go-lang-interview-questions-and-answers\/\">Go interview questions and answers<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Official Resources<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">These pages come straight from the Go team, so they stay accurate and up to date.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/pkg.go.dev\/testing\">testing package documentation<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/pkg.go.dev\/net\/http\/httptest\">net\/http\/httptest package documentation<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/go.dev\/doc\/effective_go#interfaces\">Effective Go: Interfaces<\/a><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Quick Recap<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Go mocking swaps a real dependency for a fake you control.<\/li>\n\n\n\n<li>Depend on a small interface, not a concrete client.<\/li>\n\n\n\n<li>A hand-written fake can return values, errors and record calls.<\/li>\n\n\n\n<li>Table-driven tests with fakes cover every branch quickly.<\/li>\n\n\n\n<li>Use <code>httptest.NewServer<\/code> to test real HTTP code offline.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">What&#8217;s Next?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">You can now test code that talks to the outside world. Next, we will build fast service-to-service APIs with gRPC and Protocol Buffers.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Coming soon: Building a Go gRPC Service<\/p>\n\n","protected":false},"excerpt":{"rendered":"<p>Go mocking lets you test code that depends on slow services, such as a weather API, without calling them. In this post, you will&hellip;<\/p>\n","protected":false},"author":1,"featured_media":326,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_swapz_level":"","_swapz_series_part":0,"_swapz_hide_toc":false,"_swapz_hide_featured":false,"_swapz_description":"","footnotes":""},"categories":[15],"tags":[11,65,23,21],"swapz_series":[],"class_list":["post-332","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-go-lang","tag-go-lang","tag-go-mocking","tag-go-tutorial","tag-golang-basics"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.9 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Go Mocking with Interfaces for Better Unit Tests<\/title>\n<meta name=\"description\" content=\"Learn Go mocking with interfaces: replace real APIs with fakes, test error paths, record calls and test HTTP code with httptest servers.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/smitabharat.com\/blog\/go-mocking-interfaces-testing\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Go Mocking with Interfaces for Better Unit Tests\" \/>\n<meta property=\"og:description\" content=\"Learn Go mocking with interfaces: replace real APIs with fakes, test error paths, record calls and test HTTP code with httptest servers.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/smitabharat.com\/blog\/go-mocking-interfaces-testing\/\" \/>\n<meta property=\"og:site_name\" content=\"Smita Bharat - Blog\" \/>\n<meta property=\"article:published_time\" content=\"2026-05-08T03:30:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-10-10T15:51:15+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-mocking-interfaces-testing-featured.png\" \/>\n\t<meta property=\"og:image:width\" content=\"2400\" \/>\n\t<meta property=\"og:image:height\" content=\"1260\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Smita\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Smita\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-mocking-interfaces-testing\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-mocking-interfaces-testing\\\/\"},\"author\":{\"name\":\"Smita\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#\\\/schema\\\/person\\\/27a1b1067d396703215c54acedbfd339\"},\"headline\":\"Go Mocking with Interfaces for Unit Tests\",\"datePublished\":\"2026-05-08T03:30:00+00:00\",\"dateModified\":\"2026-10-10T15:51:15+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-mocking-interfaces-testing\\\/\"},\"wordCount\":590,\"publisher\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#\\\/schema\\\/person\\\/27a1b1067d396703215c54acedbfd339\"},\"image\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-mocking-interfaces-testing\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/10\\\/go-mocking-interfaces-testing-featured.png\",\"keywords\":[\"Go Lang\",\"Go Mocking\",\"Go Tutorial\",\"Golang Basics\"],\"articleSection\":[\"Go Lang\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-mocking-interfaces-testing\\\/\",\"url\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-mocking-interfaces-testing\\\/\",\"name\":\"Go Mocking with Interfaces for Better Unit Tests\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-mocking-interfaces-testing\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-mocking-interfaces-testing\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/10\\\/go-mocking-interfaces-testing-featured.png\",\"datePublished\":\"2026-05-08T03:30:00+00:00\",\"dateModified\":\"2026-10-10T15:51:15+00:00\",\"description\":\"Learn Go mocking with interfaces: replace real APIs with fakes, test error paths, record calls and test HTTP code with httptest servers.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-mocking-interfaces-testing\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-mocking-interfaces-testing\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-mocking-interfaces-testing\\\/#primaryimage\",\"url\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/10\\\/go-mocking-interfaces-testing-featured.png\",\"contentUrl\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/10\\\/go-mocking-interfaces-testing-featured.png\",\"width\":2400,\"height\":1260,\"caption\":\"Go Mocking featured image: cards for a small interface, a fake and an httptest server\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-mocking-interfaces-testing\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Go Mocking with Interfaces for Unit Tests\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/\",\"name\":\"Smita Bharat Blog\",\"description\":\"Golang &amp; Backend Development Tutorials and Interview Questions\",\"publisher\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#\\\/schema\\\/person\\\/27a1b1067d396703215c54acedbfd339\"},\"alternateName\":\"Smita Bharat\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#\\\/schema\\\/person\\\/27a1b1067d396703215c54acedbfd339\",\"name\":\"Smita\",\"logo\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#\\\/schema\\\/person\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/smitabharat.com\\\/blog\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Go Mocking with Interfaces for Better Unit Tests","description":"Learn Go mocking with interfaces: replace real APIs with fakes, test error paths, record calls and test HTTP code with httptest servers.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/smitabharat.com\/blog\/go-mocking-interfaces-testing\/","og_locale":"en_US","og_type":"article","og_title":"Go Mocking with Interfaces for Better Unit Tests","og_description":"Learn Go mocking with interfaces: replace real APIs with fakes, test error paths, record calls and test HTTP code with httptest servers.","og_url":"https:\/\/smitabharat.com\/blog\/go-mocking-interfaces-testing\/","og_site_name":"Smita Bharat - Blog","article_published_time":"2026-05-08T03:30:00+00:00","article_modified_time":"2026-10-10T15:51:15+00:00","og_image":[{"width":2400,"height":1260,"url":"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-mocking-interfaces-testing-featured.png","type":"image\/png"}],"author":"Smita","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Smita","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/smitabharat.com\/blog\/go-mocking-interfaces-testing\/#article","isPartOf":{"@id":"https:\/\/smitabharat.com\/blog\/go-mocking-interfaces-testing\/"},"author":{"name":"Smita","@id":"https:\/\/smitabharat.com\/blog\/#\/schema\/person\/27a1b1067d396703215c54acedbfd339"},"headline":"Go Mocking with Interfaces for Unit Tests","datePublished":"2026-05-08T03:30:00+00:00","dateModified":"2026-10-10T15:51:15+00:00","mainEntityOfPage":{"@id":"https:\/\/smitabharat.com\/blog\/go-mocking-interfaces-testing\/"},"wordCount":590,"publisher":{"@id":"https:\/\/smitabharat.com\/blog\/#\/schema\/person\/27a1b1067d396703215c54acedbfd339"},"image":{"@id":"https:\/\/smitabharat.com\/blog\/go-mocking-interfaces-testing\/#primaryimage"},"thumbnailUrl":"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-mocking-interfaces-testing-featured.png","keywords":["Go Lang","Go Mocking","Go Tutorial","Golang Basics"],"articleSection":["Go Lang"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/smitabharat.com\/blog\/go-mocking-interfaces-testing\/","url":"https:\/\/smitabharat.com\/blog\/go-mocking-interfaces-testing\/","name":"Go Mocking with Interfaces for Better Unit Tests","isPartOf":{"@id":"https:\/\/smitabharat.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/smitabharat.com\/blog\/go-mocking-interfaces-testing\/#primaryimage"},"image":{"@id":"https:\/\/smitabharat.com\/blog\/go-mocking-interfaces-testing\/#primaryimage"},"thumbnailUrl":"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-mocking-interfaces-testing-featured.png","datePublished":"2026-05-08T03:30:00+00:00","dateModified":"2026-10-10T15:51:15+00:00","description":"Learn Go mocking with interfaces: replace real APIs with fakes, test error paths, record calls and test HTTP code with httptest servers.","breadcrumb":{"@id":"https:\/\/smitabharat.com\/blog\/go-mocking-interfaces-testing\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/smitabharat.com\/blog\/go-mocking-interfaces-testing\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/smitabharat.com\/blog\/go-mocking-interfaces-testing\/#primaryimage","url":"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-mocking-interfaces-testing-featured.png","contentUrl":"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-mocking-interfaces-testing-featured.png","width":2400,"height":1260,"caption":"Go Mocking featured image: cards for a small interface, a fake and an httptest server"},{"@type":"BreadcrumbList","@id":"https:\/\/smitabharat.com\/blog\/go-mocking-interfaces-testing\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/smitabharat.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Go Mocking with Interfaces for Unit Tests"}]},{"@type":"WebSite","@id":"https:\/\/smitabharat.com\/blog\/#website","url":"https:\/\/smitabharat.com\/blog\/","name":"Smita Bharat Blog","description":"Golang &amp; Backend Development Tutorials and Interview Questions","publisher":{"@id":"https:\/\/smitabharat.com\/blog\/#\/schema\/person\/27a1b1067d396703215c54acedbfd339"},"alternateName":"Smita Bharat","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/smitabharat.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/smitabharat.com\/blog\/#\/schema\/person\/27a1b1067d396703215c54acedbfd339","name":"Smita","logo":{"@id":"https:\/\/smitabharat.com\/blog\/#\/schema\/person\/image\/"},"sameAs":["https:\/\/smitabharat.com\/blog"]}]}},"_links":{"self":[{"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/posts\/332","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/comments?post=332"}],"version-history":[{"count":1,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/posts\/332\/revisions"}],"predecessor-version":[{"id":337,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/posts\/332\/revisions\/337"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/media\/326"}],"wp:attachment":[{"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/media?parent=332"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/categories?post=332"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/tags?post=332"},{"taxonomy":"swapz_series","embeddable":true,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/swapz_series?post=332"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}