Conext
I found this question here but my problem is different.
So we are using Katharsis Controller
and Spring Data Rest
.
We only have 1 controller for entire application and then the request will be sent to Spring Data Rest
repositories classes.
We want to use Spring Restdoc
to generate documentation which requires us to write unit tests with MockMvc
.
But when using Mockmvc
, it starts up the container and will require datasources to be set up.
If we use standaloneSetup()
and pass the mocked repository class, then MockMvc
won't load Katharsis Controller
and therefore the request won't reach that repository.
I understand that we can create an in-memory database but our project is big and the database needs a huge number of tables to be created we want to avoid that since these tests are for documentation purposes.
Question
Is there any way to achieve this and only mock the target repository class?
Note
By repository I mean CrudRepository
interface in Spring DataRest
.
As Andy Wilkinson suggested, you may consider creating unit test where you wire beans together by yourself and use MokMvc standalone setup.
If you want to create integration test and create Spring Context anyway, there is way to fake Spring bean by using @Primary
, @ActiveProfiles
and @Profile
annotations. I wrote a blog post with GitHub example how to do it . You just need to combine this approach with WebApplicationContext
based MockMvc
setup. It works without problems, I wrote such tests in the past.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.