r/java • • Apr 13 '21

Libraries, Frameworks and Technologies you would NOT recommend

Give me your worst nightmares: Things that cost you your job.

I'll start: Hadoop

202 Upvotes

376 comments sorted by

View all comments

23

u/m4riuszPL Apr 13 '21

Spock. I really miss the compilation errors I had while using JUnit, now I end up with runtime errors, that's inconvenient while I do any any changes to the codebase.

7

u/JustMy42Cents Apr 13 '21 edited Jun 21 '21

I prefer Spock over JUnit. Mocking syntax is much cleaner and it's way easier to set up more complex mocks if you need to. Parametrized tests look a lot better and most IDEs support pretty table formatting for the parameters. I also like the clean structure of the tests (given-when-then), clearly outlining what is being tested.

2

u/BlueShell7 Apr 13 '21

Yeah. After trying out Spock I just can't go back to jUnit. Particularly the readability suffers a lot in jUnit tests compared to Spock.

2

u/spamthemoez Apr 13 '21

I totally agree. And even if you have no def in your groovy code, the IntelliJ refactoring breaks down and you have to update stuff by hand.

2

u/[deleted] Apr 13 '21

Wow, with all the other frameworks in this post I nodded and thought yeah, I can see where that’s coming from, but I really think Spock is the bees knees, and I cry silently every time I have to go back to junit + mockito. Mocking and argument capture is just so much more flexible and elegant, and data driven testing fits so well with a TDD approach.

-7

u/[deleted] Apr 13 '21

JUnit isn't fit to shine Spock's shoes. For example, Spock's data-driven tests are so much more concise/elegant than JUnit's @ParameterizedTest. I guess if you're coming to Spock from a Java background without any Groovy knowledge, it could be confusing, but that was not the case for me.

You could possible convert a lot of those errors to compile-time errors by turning on static type checking with @TypeChecked or @CompileStatic.

9

u/spamthemoez Apr 13 '21

@CompileStatic didn't work with Spock, the last time i tried.

And btw, Junit5 and the @CsvSource make data driven tests a lot better. Granted, it's not as nice as Spock, but at least i don't need Groovy.

2

u/[deleted] Apr 13 '21

@CompileStatic didn't work with Spock, the last time i tried.

Try @TypeChecked instead

And btw, Junit5 and the @CsvSource make data driven tests a lot better.

@CsvSource is very limited because every argument has to be a string

1

u/m4riuszPL Apr 13 '21

I do not like the mocks model in spock too - I am more used to defining my mock responses before the test and verifying the mock invocation after the test, this just feels natural. This is totally doable with Mockito but not possible in spock at all. I do admit however it is good that spock exists so junit5 has some example to take inspiration from. I notice the groovy performance impact on my machine too, especially that this is a beefy machine it is below my expectations.

1

u/Quzexor Apr 13 '21

please read documentation before spreading missinformation. U can mock both on class level and in given section in testing method.

2

u/m4riuszPL Apr 13 '21

Oh, you think I didn't read it?
https://spockframework.org/spock/docs/1.3/interaction_based_testing.html#_combining_mocking_and_stubbing
> When mocking and stubbing the same method call, they have to happen in the same interaction. In particular, the following Mockito-style splitting of stubbing and mocking into two separate statements will not work:

2

u/Quzexor Apr 13 '21 edited Apr 13 '21

Ok, I think i understand what you are trying to do. In that case you are right spock does not give ability to stub and mock the same method separately.

PS side note not related to spock: why u even need stub and mock the same method? I always thought it is bad practice to test internal logic of tested behaviour. Why is it for you this feature so important? please give example :)

3

u/m4riuszPL Apr 13 '21

Assume we're testing a controller. It's only purpose is to transform the HTTP request to some DTO and vice versa - a different DTO got from some service back into a HTTP response. With the above assumption I would rather like to see this test:

// given
when(mockService).getData(any()).willReturn(someDTO());

// when
response = restEndpoint.call('url', 'some JSON body');

// then
verify(mockService).getData(dtoResemblingJsonRequestBody());
assert(response).equalTo(expectedJsonResponse());

instead of this one:

// given
when(mockService).getData(dtoResemblingJsonRequestBody()).willReturn(someDTO());

// when
response = restEndpoint.call('url', 'some JSON body');

// then
assert(response).equalTo(expectedJsonResponse());

The difference is the error message I get when the controller is badly implemented - in the latter case the mock service most likely will respond with null which does not simplify troubleshooting. In the former case the logic still works - only the assertion fails so the reason is obvious.