News for hackers. Stuff that matters.
Re:Generating a Java program with 90% less code by oldmanhorton on Wednesday December 04, 2019 @11:18AM (#21703445)
Do you consider test mocks to be a second implementation that warrants the interface?
Re:Generating a Java program with 90% less code by bcrosby95 on Wednesday December 04, 2019 @11:57AM (#21703927)
No. This is what many considered test induced damage. If the only reason you create an interface is because a test requires it, you are damaging the integrity of your design.

Interfaces are one of the more powerful abstractions available and should be well thought out points of flexibility you specifically designed into your solution. If you use interfaces for other reasons your codebase is full of false flexibility and not-actually-abstracted abstractions which makes it difficult for other people to figure out what they can actually do with it.

Re:Generating a Java program with 90% less code by iamaelephant on Wednesday December 04, 2019 @12:04PM (#21704013)
> No. This is what many considered test induced damage. If the only reason why you create an interface is because a test requires it, you are damaging the integrity of your design.

Writing testable code is not "damage". An interface is a small concession in exchange for the well known benefits of automated testing.

I see where you're coming from and many programmers use too many interfaces as a reflex. But I absolutely think interfaces should be used to create seams for automated tests in front of components that do things like interact with databases, file systems, networks etc.

Re:Generating a Java program with 90% less code by klmr on Wednesday December 04, 2019 @01:07PM (#21704955)
> Writing testable code is not "damage"

Correct, but you can go a long way in writing testable code without requiring mock implementations. While I don’t deny the usefulness of mock testing, it comes at a steep cost (= vastly more complex code base), and I increasingly question whether that’s worth it. In practice I find it more useful to unit test what can be tested in isolation, and to perform integration testing for the test, and to limit mock testing (and thus unit tests which would require mocking) to a few well-defined interfaces. This cuts down drastically on boilerplate, with little (if any) loss of testability. And these few remaining mock object dependencies can be modelled via simple parameters and higher-order functions, further cutting down on extraneous interface classes.

Re:Generating a Java program with 90% less code by BeetleB on Wednesday December 04, 2019 @08:03PM (#21708987)
> Correct, but you can go a long way in writing testable code without requiring mock implementations.

For some languages (e.g. C++), if you are using classes, it can actually be fairly challenging to do unit tests without mocks. And almost every solution to this problem out there is to use interfaces/dependency injection/inversion.

Re:Generating a Java program with 90% less code by klmr on Thursday December 05, 2019 @05:11AM (#21711200)
> [in C++] it can actually be fairly challenging to do unit tests without mocks

Yes, but you can write integration tests instead. There’s no rule that says that unit testing must cover every aspect of the software. Unit test those units that can be used in isolation, integration test the rest. Case in point, our main software at work, written in C++, has close to 100% test coverage, and uses almost no mock tests. Instead, we have extensive integration tests where unit testing without mocking doesn’t work.

And, again: I’m not saying that there are no tradeoffs involved in that. But the benefit of having a drastically simpler code architecture outweigh the cost of having to move those tests into integration.

Incidentally this is vastly easier when the architecture cleanly separates business logic from general logic and moves as much of it as possible into side-effect free general-purpose functions.

Re:Generating a Java program with 90% less code by BeetleB on Thursday December 05, 2019 @02:33PM (#21715601)
> Yes, but you can write integration tests instead. There’s no rule that says that unit testing must cover every aspect of the software. Unit test those units that can be used in isolation, integration test the rest. Case in point, our main software at work, written in C++, has close to 100% test coverage, and uses almost no mock tests. Instead, we have extensive integration tests where unit testing without mocking doesn’t work.

In my last C++ job, perhaps less than 5% of our code was unit testable. We had very good test coverage using integrated tests, but those were very expensive tests. Running a full test suite took 10 hours. Our SW heavily relied on certain equipment. Fortunately, the vendor had provided a very good simulator for it - so almost all our integration tests needed that simulator. And the simulator was such that there was a fair amount of overhead in loading and setting it up and unloading it. We didn't do this for every test - that would have taken days/weeks to run - but for logically grouped tests. And unfortunately many of the integration tests we wrote were much more suited to unit tests, but because we didn't have interfaces, we were forced to test them via an integration test, which was very expensive.

Oh, and the simulator would not allow parallelism - you could have only one simulator running on a given machine at a time. So all our tests had to be run in sequence.

And of course, the simulator had bugs, so whenever we had a failure, we had to determine if the simulator was at fault. Especially for intermittent failures.

So yes, we had very thorough tests and were proud of it, but unit testing for perhaps 30% of our tests would have been better. But to unit test those we really needed interfaces. I know because I convinced management to give me some weeks to convert one of our simplest portions of code into something we could unit test. I thought it would be trivial, but I ended up needing interfaces.

There is a whole other approach which our sister team (very similar SW, but different HW) used. They did not have a reliable simulator. So they wrote a "fake" simulator. They simply reproduced the APIs and wrote code to return certain values under certain conditions - a poor man's mock. On the plus side their test suite ran very quickly. The down side was they needed to write many mocks for the same function (for when they needed to return different values). Writing tests was very expensive for them.

Why did they write fakes instead of using a mocking framework? Because every mocking framework they found in C++ required interfaces, and they didn't want to change their SW architecture.

In many cases integration tests are OK. But in our case, they were extremely expensive.

> Incidentally this is vastly easier when the architecture cleanly separates business logic from general logic and moves as much of it as possible into side-effect free general-purpose functions.

This is precisely where interfaces come from! It's not trivial to separate business logic from general logic without them. When I took the time to show how we could write unit tests for our code base, I ended up with interfaces. And no, I didn't read any books/sites telling me I should do it. I didn't even know they were called interfaces. I just took some of the simplest code in our codebase, and asked "How can I write unit tests for this?" and essentially reinvented interfaces. I separated the business logic from the logic requiring the simulator, and then wrote unit tests for the business logic, and had an interface class to interface with the simulator.

To give you an idea, the code I converted to unit tests was trivial: Given this input string with these flags set, return this converted string, etc. It should be one of the easiest things to unit test! However, the input string and flags were entities understood by the HW and so we had simulator dependencies. I wrote a class that dealt only with strings and booleans to implement the business logic, and then had the interface class translate back and forth between strings/bools to entities the simulator expected/understood.

I appreciate your comment, and trust me, I'm not trying to be difficult. But while I've heard many claims, every time I've gone into the details I end up with interface classes. I work at a large company and went around asking several teams in different C++ projects, and they all independently had come to the same conclusion: Either use interfaces or live without unit tests. I presented my work to convert portions of our code base to unit tests at internal SW conferences, and would say "This is a highly nontrivial and crappy way to do unit tests in C++. I'd really like to hear from people in the audience if they found a better way." And whoever approached me afterwards said they had gone down the same path as me and ended up with interfaces.

Testing in C++ sucks, to be frank.

As an aside, I'm not pro-mocks, BTW. I recently did a small personal Python project where I insisted doing TDD. I would not write code till I had a test. And this involved several mocks. And while all my tests involving mocks passed, in the majority of cases involving the mocked code, my program had bugs and would fail when I tested with real data.

(Maybe that's more a criticism of TDD than of mocks, though).

Re:Generating a Java program with 90% less code by klmr on Friday December 06, 2019 @09:14AM (#21721875)
I appreciate your comment and your experience. And I agree: integration tests tend to be time consuming to run. At work we actually had similar issues (and many of our integration tests need to exercise cloud APIs) before a rewrite that made the time manageable.

I just wanted to pick up one of your points:

> I've gone into the details I end up with interface classes

My reply to this is that functional programming languages tend to manage entirely without, just by doing “dependency injection” via higher-order functions (in fact, outside Java I’ve never [needed to] use a DI framework). This (often? always? I honestly don’t know!) makes mocking possible without impacting the overall architecture. And like you, I practice strict TDD for all my current own hobby projects, and I manage without interfaces. But I don’t want to make a claim of generality here: it’s entirely possible that this approach doesn’t work everywhere, or even in the majority of cases.

Re:Generating a Java program with 90% less code by t-writescode on Wednesday December 04, 2019 @12:38PM (#21704539)
A common pattern to get around this situation is to use mocking frameworks to fake those calls without using interfaces. It’s in the air whether this is better or worse in the general lifespan of a piece of code; but, there are alternatives.
Re:Generating a Java program with 90% less code by taco_emoji on Wednesday December 04, 2019 @01:01PM (#21704881)
How does that work in a static language? In most (all?) static languages, you'd need an interface or virtual method in order to on-the-fly generate a class that implements the mocked functionality.
Re:Generating a Java program with 90% less code by chinhodado on Wednesday December 04, 2019 @01:32PM (#21705308)
Not entirely sure what you meant, but in Java/C# you can easily mock out a concrete class with a mock library, like Mockito. No interface required.
Re:Generating a Java program with 90% less code by GordonS on Wednesday December 04, 2019 @01:46PM (#21705511)
There are plenty of mocking frameworks for C# (Moq, FakeItEasy, etc), but for all but the most trivial classes, you generally need to use an interface or mark methods as virtual.

It's one of the reasons I tend to minimise my use of mocks as much as possible.

Re:Generating a Java program with 90% less code by chinhodado on Wednesday December 04, 2019 @02:03PM (#21705731)
It's the same with Java where it's very hard/not possible to mock final methods. It's just that methods in Java are virtual by default so it's less "in the way" when you need to mock something.
Re:Generating a Java program with 90% less code by apta on Wednesday December 04, 2019 @05:38PM (#21707995)
What do you use instead of mocks when testing?
Re:Generating a Java program with 90% less code by GordonS on Wednesday December 04, 2019 @06:10PM (#21708186)
Real instances of the dependant class.

A "unit test purist" would likely shun this, but I take more of a realist approach; in many real-world cases, mock-heavy tests make for brittle tests that don't give confidence of correctness.

I tend to focus on integration tests, as I find these provide the most value while still executing very quickly (depending on the stack, obv).

That doesn't mean I don't use unit tests, or that I never use mocks - I just use mocks very sparingly.

Re:Generating a Java program with 90% less code by andrewprock on Wednesday December 04, 2019 @01:09PM (#21704993)
On the contrary, using interfaces to make testable code is precisely the damage.

You should be writing pure functions which are easily testable, and using integration code to wrap the pure and testable parts.

Interfaces are typically a hack to get around the challenge of writing well designed, pure, and testable code.

Re:Generating a Java program with 90% less code by BeetleB on Wednesday December 04, 2019 @08:04PM (#21708996)
> You should be writing pure functions which are easily testable, and using integration code to wrap the pure and testable parts.

This often ends up with interfaces...

Re:Generating a Java program with 90% less code by andrewprock on Thursday December 05, 2019 @02:12PM (#21715347)
If you write a pure function, an interface is meaningless.
Re:Generating a Java program with 90% less code by BeetleB on Thursday December 05, 2019 @02:49PM (#21715772)
The interface comes in this part of your comment:

> and using integration code to wrap the pure and testable parts.

Re:Generating a Java program with 90% less code by andrewprock on Thursday December 05, 2019 @06:16PM (#21717714)
I think you and I are talking about different things when we say "integration test".
Re:Generating a Java program with 90% less code by rb808 on Wednesday December 04, 2019 @12:41PM (#21704586)
I'm so glad to see someone write this. I keep seeing all these horrible code bases that are supposed to be easily testable but all I see is a horrible design that is impossible to understand - all because Mocking is supposed to be a good idea.
Re:Generating a Java program with 90% less code by marcus_holmes on Wednesday December 04, 2019 @11:53AM (#21703880)
I did this, and then switched back to just running the code on a test database. I found I was writing two sets of tests: one that tested the code to see if it injected the right parameters into the SQL, and another to see if the SQL worked. It just got easier to test them both together.
Re:Generating a Java program with 90% less code by breischl on Wednesday December 04, 2019 @03:34PM (#21706613)
This can become a challenge when you want to scale a dev team. You either have people conflicting in the DB, or each dev needs to set up a local DB and deal with DB migrations. Containerized DBs help with this to some extent, but it's still a maintenance headache.

There's also a tradeoff around testing error states, but that can be a wash. Some errors are easier to create with a mock, some are easier to create with the actual driver.

Re:Generating a Java program with 90% less code by dcolkitt on Wednesday December 04, 2019 @12:38PM (#21704537)
There are tradeoffs to this approach. When the tests break it becomes a lot harder to triangulate the origin of the problems.
Re:Generating a Java program with 90% less code by runeks on Friday December 06, 2019 @06:14AM (#21720763)
I don’t buy this argument.

Once you know there’s a bug, you will definitely want to write more tests, in order to find the cause. But always writing a lot of tests, which are only usable in case you find a bug, seems like wasted effort to me.

If you need to triangulate the origin, do that when it’s necessary (i.e. when your broad test fails) and not all the time.

Re:Generating a Java program with 90% less code by GordonS on Wednesday December 04, 2019 @01:48PM (#21705534)
I prefer to just runs tests using a real database too. They might not complete as fast as mocked out ones, but they give me much more confidence of correctness, and still complete in milliseconds.
Re:Generating a Java program with 90% less code by ses1984 on Wednesday December 04, 2019 @11:19AM (#21703465)
There are test frameworks that obviate the need to do this.
Re:Generating a Java program with 90% less code by gautamdivgi on Wednesday December 04, 2019 @11:22AM (#21703507)
Not if you use something like c/c++. There are test frameworks but there is no way from preventing testing code from leeching into functional code.

But yes - Java/python, etc. you shouldn't need to do this.

Re:Generating a Java program with 90% less code by CraigJPerry on Wednesday December 04, 2019 @11:39AM (#21703723)
What’s cheaper to own though - a whole test library / framework (assuming you don’t use any other feature), or a simple interface?

I’m generally hesitant to add test libraries if there’s an easy design choice alternative that keeps the code simpler.

Re:Generating a Java program with 90% less code by ses1984 on Wednesday December 04, 2019 @12:39PM (#21704560)
Sure pulling in a giant magical reflection heavy test framework has costs but I think it's well worth it.
Re:Generating a Java program with 90% less code by MockObject on Wednesday December 04, 2019 @12:23PM (#21704311)
Adding interfaces whose only purpose is unit tests is not keeping the code simpler. Using a test library that automatically generates mocks for unit testing is what keeps the code simpler.
Re:Generating a Java program with 90% less code by closeparen on Wednesday December 04, 2019 @12:55PM (#21704793)
Using a test framework that bends the rules of the language is far from simple. One of the things I appreciate about Go is that any mortal can understand how a unit test works: you "just" plug in something that satisfies the interface. However, we are spoiled to have implicitly satisfied interfaces, which can be declared on the consumer side without the implementation even knowing.
Re:Generating a Java program with 90% less code by BeetleB on Wednesday December 04, 2019 @08:08PM (#21709014)
> Adding interfaces whose only purpose is unit tests is not keeping the code simpler. Using a test library that automatically generates mocks for unit testing is what keeps the code simpler.

I've yet to find a library for C++ that generates mocks without requiring interfaces, but I'll admit I'm not an expert on it. Can you recommend any?

As an example, Google Mock/Test will require interfaces in a C++ code base.

Of course, we may be disagreeing on what is meant by the term "interface".

Re:Generating a Java program with 90% less code by CraigJPerry on Wednesday December 04, 2019 @12:43PM (#21704627)
>> automatically generates mocks for unit testing is what keeps the code simpler

You're possibly confusing easy with simple but adding a versioned library of some thousands of lines of code to a project is in no objective way simpler than adding a small test double which you wrote.

Re:Generating a Java program with 90% less code by MockObject on Wednesday December 04, 2019 @02:51PM (#21706193)
Yes, it is, in the only objective way that people actually care about: the size and simplicity of the source code.

(What people don't care about is the size of libraries that are scoped for the test context only, and don't even impact artifact size at all.)

Also, a Mockito mock is simpler in usage than a hand-written and normally instantiated test double.

Re:Generating a Java program with 90% less code by CraigJPerry on Wednesday December 04, 2019 @04:08PM (#21707005)
Ripping off the classic talk by Rich Hickey:

  Simple - composed of a single element; not compound
  Complex - consisting of many different and connected parts
  Easy - achieved without great effort; presenting few difficulties.
This is not as easy but is simple. It is explicit, fundamental Java.

  public void allocationOnlyPossibleWhenOpen() {
     LocalDateTime expected = LocalDateTime.now();
     TimeAllocator sut = new TimeAllocator(new Clock() {
    public LocalDateTime getTime() {
      return expected;
    }
     });
    
     sut.setOpen(false);
     assertThat(sut.allocate()).isNull();

     sut.setOpen(true);
     assertThat(sut.allocate()).isEqualTo(expected);
   }
This is easier but more complex. It depends on a multi-thousand line library with its own API.

  public void allocationOnlyPossibleWhenOpen() {
     LocalDateTime expected = LocalDateTime.now();
     Clock mockClock = mock(Clock.class);
     when(clock.getTime()).thenReturn(expected);
     TimeAllocator sut = new TimeAllocator(mockClock);

     sut.setOpen(false);
     assertThat(sut.allocate()).isNull();

     sut.setOpen(true);
     assertThat(sut.allocate()).isEqualTo(expected);
   }
The 2nd example has:

A versioned external dependency, externally versioned == opportunity for future conflict. Remember the java logging library wars? Today it's trivial to encounter conflicting spring dependencies on your classpath

A complex (but convenient) reflection based implementation, the object has non-obvious behaviour with a clever implementation

An entire api surface distinct from the components of your software system

Tens of thousands of lines of opportunity for bugs, security risks or just plain old $$ cost to hide in

Once you're able to see the world in complexity, it opens up a whole new dimension of cost & quality for your code.

All this said, in practice complexity is just a consideration. Often the complexity is worth paying. I don't want to risk the impression that i'm saying never use mockito. If my argument is anything then it's consider the impact of complexity.

Re:Generating a Java program with 90% less code by organsnyder on Wednesday December 04, 2019 @11:59AM (#21703949)
Mockito is a single pom.xml (or build.gradle) entry.

edit: Not "Moquitto".

Re:Generating a Java program with 90% less code by MockObject on Wednesday December 04, 2019 @12:21PM (#21704275)
Close! That's Mockito.

https://site.mockito.org/

Re:Generating a Java program with 90% less code by organsnyder on Wednesday December 04, 2019 @12:29PM (#21704401)
Oops. Haven't done Java programming full-time since last year, but I did set up a home MQTT broker recently...
Re:Generating a Java program with 90% less code by CraigJPerry on Wednesday December 04, 2019 @12:45PM (#21704657)
It is - but my point is about complexity not ease.

The fact that dependency is versioned separately from your code base is one source of complexity that it adds. The fact that it has many lines of code is another complexity.

It is however often easier, even although i'm advocating very small self-written test doubles, they are still less easy than using mockito.

Re:Generating a Java program with 90% less code by organsnyder on Wednesday December 04, 2019 @01:42PM (#21705457)
It's a dependency that's only needed during testing, though—it's not shipped with production artifacts.
Re:Generating a Java program with 90% less code by CraigJPerry on Wednesday December 04, 2019 @02:43PM (#21706115)
That argument doesn't address complexity. Tests are part of the system.
Re:Generating a Java program with 90% less code by taco_emoji on Wednesday December 04, 2019 @01:04PM (#21704906)
Not on all platforms. In .NET you need an interface or virtual method in order to allow the mocking framework to dynamically generate a concrete subclass that mocks out that method's functionality.
Re:Generating a Java program with 90% less code by chopin on Wednesday December 04, 2019 @11:21AM (#21703487)
I do that for this reason but only if I am able to inject the implementation into the classes under test. Very often I just resort to constrictor injection for internal code.
Re:Generating a Java program with 90% less code by devit on Wednesday December 04, 2019 @03:27PM (#21706542)
Testing with mocks is generally a bad idea; instead, one should just test using the same dependencies that are used in production (after testing those dependencies, if also part of the project).

This avoids having to introduce the extra interface, having to code the mock, sometimes having to change the mock or tests when changing the code and also lets you catch bugs in the dependencies that the tests on the dependency itself missed and that would otherwise show up in production.

One might have to do some work to partially replicate the production setup in testing and making it fast to start and run, but that's usually less work and gives a much better result.

Re:Generating a Java program with 90% less code by ludjer on Thursday December 05, 2019 @06:40AM (#21711589)
That is more integration tests. Unit tests should target logic more then dependencies/integration.
Re:Generating a Java program with 90% less code by ludjer on Thursday December 05, 2019 @06:36AM (#21711571)
You do know you can mock out concrete classes aswell. You do not need a interface.