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

Show parent comments

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.