-
Notifications
You must be signed in to change notification settings - Fork 41.1k
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
allow to use different mocking librarires in spring-boot-test for @MockBean #9372
Comments
Thanks for the suggestion, but my feeling is that we don't want to introduce the extra complexity that supporting multiple mocking libraries would require. Let's see what the rest of the team think. |
I'm inclined to agree with Andy. Adding Mockito support was painful enough and I don't particularly want to make that code any more complex that it has to be. I'd like to see Spock support but I think it should be handled outside of Spring Boot core. If anyone is interested in starting a new Apache 2.0 project they are welcome to fork the existing Mockito code. We can also look into donating it under a different license if necessary. |
If anyone stumbles on this in the future I wanted to let you know that I've created springmock library which allows to inject mocks created by any mocking library (in theory, in practice I've got support for spock mocks and mockito and infrastructure to plugin more). Since I've got minimal set of features you can use it along with springboot @MockBean and @SpyBean. More on my blog and project github page |
@wilkinsona / @philwebb I've taken over @twicksell attempt to integrate this support into Spock itself and I've some questions:
|
Yes. The mock beans affect the contents of the context so they also affect the key that's used for the cache.
We haven't worried about parallel execution of tests in the same JVM/ClassLoader. Either forcing sequential execution or parallelising using multiple JVMs would be perfectly fine
Yes, that's fine. All the code is Apache V2 licensed and so is Spock so you're free to do whatever you'd like. |
@wilkinsona, @leonard84 Maybe you guys will be interested in springmock? @leonard84 maybe instead of coping spring behavior yet again we can figure something out together? Maybe you'll be interested in merging something from springmock into Spock or delegating some work to springmock? Spock is awesome so I'd be happy to help just let me know if you are interested :) @wilkinsona I know that springmock does not support @MockBean nor @SpyBean annotations but it is by design. I wanted to keep it separated to allow usage of springmock in existing projects and avoid conflicts. Maybe spring team will be interested in taking some code from springmock to allow other mocks providers work with spring? Please consider starting up yet another project in the spring-boot portfolio or merging springmock-infrastructure into spring-boot-test. Spring is also awesome project so I'll be happy to help just let me know if you are interested :) |
@pchudzik I'm aware of your project, I'm going to look at both sources. However, I also want to explore some different approaches, e.g. creating proxies in the spring context and using the full power of the existing @SpringMock
Service service = Mock() {
getSomething() >> "hello"
} My goal would be to integrate it into spock-spring, without the need to for an external module. I've send you an invite to the Spock slack channel where we can discuss it further. If you @wilkinsona or @philwebb want to join the Spock slack channel as well just say the word, we are always interested in getting key developers as advisors or contributors. |
Right now only way of mocking stuff in spring-boot-tests is using mockito which is awesome if you are writing your tests in java but if you are using spock then you might want to use spock mocks. With spock 1.1 one can create detachable mocks not bounded to Specification class instance. It would be great if it would be possible to allow plug in different mocking framework. Spring will ship with mockito as it is now, but spock will be able to provide some classes which will provide custom mock factory.
I've been trying to investigate this issue on my own (and do it in my project) and here is what I found out.
Class org.springframework.boot.test.mock.mockito.MockitoPostProcessor would be good entry point to start. There is pretty promising method org.springframework.boot.test.mock.mockito.MockitoPostProcessor#createMock (with spies it is more complicated and I stopped searching after I tripped over this one), but classes MockDefinition and SpyDefinition are package private (for starters) and method createMock is
protected final
so it is impossible to override.imho mock creation should be abstracted somehow and it should open MockitoPostProcessor (not only mockito any more) for different mocking libraries (like spocks detachable beans).
What do you think about it? Would you consider accepting PR which will mess around in mockito package
The text was updated successfully, but these errors were encountered: