6 ms·
Funnily enough, a lot of Java code does what the Go code did: it mocks by defining interfaces and then attaching either production or mock code as required. See
by jacques_chester 12y ago
Funnily enough, a lot of Java code does what the Go code did: it mocks by defining interfaces and then attaching either production or mock code as required. See SpringMVC for an example.
Why? Because Java mocking libraries aren't perfect either. If you're using an injection framework, it tends to be trickier to arrange the injection of the right object in the right place and when you don't, the breakages tend to be mysterious.
Then you get into the exciting world of bytecode manipulation, which is necessary for some mocking libraries.
And, most annoyingly, it's basically impossible to mock static methods.
I've worked in projects that used extensive Java mocking and on a project that used Go with hand-rolled mocks. I experienced far less pain with Go, because everything was plain and simple. All we needed was to verify behaviour and we did that with relative ease.
I'd note that the only 3rd-party libraries we used were Ginkgo/Gomega, to support a more spec-style of testing.
- lmm 12y ago> Funnily enough, a lot of Java code does what the Go code did: it mocks by defining interfaces and then attaching either production or mock code as required. See SpringMVC for an example. I actually support this; I just think it's worth being explicit about the relationship between interface and implementation. The difference between Camera.shoot() and Gun.shoot() can be important, and when you want to upgrade a library you need to know what you're implementing. In Java you have extends and @Override to give you a straightforward compile error when a parent interface changes, which makes library upgrades relatively painless. I fear the Go approach gives you the worst of both worlds - the extra typing of a Java-style explicit interface declaration, but no more safety than a Rails upgrade. > Why? Because Java mocking libraries aren't perfect either. If you're using an injection framework, it tends to be trickier to arrange the injection of the right object in the right place and when you don't, the breakages tend to be mysterious. I don't think you're comparing like with like. If you're using an injection framework in Go, you'll have the same difficulty injecting and mysterious failures when you get it wrong. There's no need to throw the Java baby out with the Spring bathwater. > Then you get into the exciting world of bytecode manipulation, which is necessary for some mocking libraries. > And, most annoyingly, it's basically impossible to mock static methods. On the contrary, the whole point of bytecode manipulation is that it lets you mock static methods (or undeclared interfaces) - you don't need it otherwise. Is mocking statics any easier in Go? If Go simply doesn't let you have statics, that's not really a Go advantage - you can (and I would argue should) program without statics in Java 99% of the time, but they're there for when you really need them.
- jerf 12y ago"The difference between Camera.shoot() and Gun.shoot() can be important, and when you want to upgrade a library you need to know what you're implementing." If I went looking, and if the archives are still there, I could probably find posts from myself on comp.lang.python complaining about "duck typing" for just that reason. I was wrong. In the 10-12 years since I posted that, I've been programming in nothing but "duck-typed" languages, and I have never once encountered that problem. Not once. Not even a little. Not even close. When theory conflicts with reality, I unapologetically side with reality. HN and the internet being what it is, I'm sure someone can pop up here with some horror story about when it happened to them, but I would contend that it's still something so unusual as to not be worth planning for. Maybe just a twinge in the gut if you really did declare an interface with just a Shoot of no parameters and no return value, but... if nothing else, in the Go world, the odds of you having a Camera.Shoot() and a Gun.Shoot() method are low anyhow, and the odds of them conflicting even lower. First you have to declare an interface that both of them fully implement. If you have a .Reload method on your gun, but not your camera, you can't accidentally cross them. If you have a .Reload method on both, but the Gun takes an Ammunition argument and the Camera takes a Film argument, they can't cross. And from a pure software-engineering perspective, you have to a have a program in which you are using the same exact word for two completely different concepts, which can happen, but is bad practice and still takes some work. Honestly, worry about it when it happens and not before. "If you're using an injection framework in Go," I see no utility in using an injection framework in Go. You just inject things. People keep creating injection frameworks with ideas ported from Java and posting them on /r/golang, but they don't go anywhere because they have nowhere near enough utility to become self-sustaining. There's a pattern more useful than an injection framework that I often use: http://www.jerf.org/iri/post/2929 http://www.jerf.org/iri/post/2929 (For the purposes of this conversation, I highlight the part of that post that comes after "But Go does have two attributes that make it easier than many other imperative languages.") Go has a much lower paperwork load than Java. Superficially, on paper, they seem like very similar languages. In practice, I find using Go to be only slightly more painful than Perl or Python, with the pain generally paying me back on the order of single-digit days anyhow (when I start doing aggressive unit-testing-backed refactorings that are unsafe in scripting languages). It isn't anywhere near as paperwork-ridden as Java. (That's probably why I've come to grips with the lack of generics. Yes, it causes me pain, but it comes up less than you think, and in general the rest of the language is still pretty slick. It's not like it's as complicated and paperwork-ridden as Java, and then they took away generics.)