News for hackers. Stuff that matters.
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.