Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add documentation for the spring module #641

Merged
merged 3 commits into from Sep 1, 2016

Conversation

leonard84
Copy link
Member

No description provided.

@Bean
GreeterService serviceMock() {
return mockFactory.Mock(GreeterService)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it worth documenting that you can also do this?

@Bean public FactoryBean<GreeterService> serviceMock() {
  return new SpockMockFactoryBean(GreeterService.class);
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So this example will add a GreeterService Bean to the Spring context?

Since I did not knew this before, I'd love to see this solution mentioned in the docs as well.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes it would. I guess it's just a different pattern that doesn't require the DetachedMockFactory instance. It does exactly the same thing.

@robfletcher
Copy link
Contributor

Looks good. I think this is a really useful addition to 1.1 as this was super-painful before.

@snekse
Copy link

snekse commented Aug 31, 2016

When testing against a real app, I got caught in a turtles all the way down scenario.

I was testing a controller that depended on a service that depended on a repo. The test would fail because the repo wasn’t mocked. Turns out it was mostly caused by us using @autowired on the field instead of the constructor.

It’s worth noting in the docs. I’ve updated my example with some notes.

snekse/spock-webmvctest@2d86454

@leonard84
Copy link
Member Author

@snekse I don't know. It is not actually the fault of the DetachedMockFactory it is just the way spring behaves. I personally always use Interfaces for services and such, avoiding class-Mocking has its benefits.

@kiview
Copy link
Member

kiview commented Sep 1, 2016

@snekse As a rule of thumb, you should simply avoid field injection. And using constructor injection has become even easier in Spring-Boot 1.4, since @Autowired is automatically applied to a constructor without specifying it explicitly.

I don't think this behavior needs to be mentioned in the Spock docs.

@leonard84 leonard84 merged commit 6df79a6 into spockframework:master Sep 1, 2016
@leonard84 leonard84 deleted the spring-module-docu branch September 27, 2018 18:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants