I'm working as a Java developer, I'm fairly new in the Go world and having
a hard time to understand how to write tests properly.
In Java we usually mock every external dependency and it's pretty easy when
working with classes, autowiring dependencies etc.
I watched some talk/read articles about testing, they mostly suggest to
write stubs manually, without a framework, using interfaces in the code, or
define functions as variables and overwrite them in tests(but usually it's
not a good idea).
When we have an interface, then really not too hard to write a stub, but
when we using a function from an external package, then it's not that
straightforward.
in the latter case, a solution to create an interface, a type which
implement it and call the external function in production code and some
stub in test.
But this is really a good solution? Polluting our packages with this kind
of stuff makes the code noisy.
With mocking frameworks maybe harder to steup the test, but the production
code stays clean.
I checked some tests in the stdlib, e.g. for the JSON-RPC and ioutil
package and for some type they creating stubs, but mostly they are using
the real packages(json.NewDecoder etc).
As I learned, because of the lack of isolation these are not unit tests,
more like kind of integration tests.
Is this an approved approach for testing in Go?
So for example if I using json.Unmarshal function in a package, should I
hide it behind an internal interface/type and stub it or just use the real
method in tests too?
I know there's some similar topics, articles, but I can't find one which
compare the testing methods with other OO languages, like Java/C# etc.
I know that hard to test code usually means bad design, but as a newbie, a
little bit hard to catch these things.
As I mentioned, I'm trying to read the tests in the stdlib, but without the
whole process, seeing only the results, sometimes they are complicated.
Thanks,
Norbert
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.