mmnaseri / spring-data-mock

Mock facility for Spring Data repositories
MIT License
137 stars 44 forks source link

About Spring Dependency Injection Repository Object #152

Closed GuoxinL closed 6 years ago

GuoxinL commented 7 years ago

Hello, I have already successful use of Spring data mock (Referred to as Spring data mock to SDM), I initially thought about at the time of testing we should control their own data,rather than using the existing in the database, the people in collaborative development will affect the test results, until I found you on Google development testing framework, thank you very much, can let I stand on the shoulders of giants.

But I found the SDM doesn't use Spring dependency injection (DI), and I need to add a constructor on each service, through the way of the constructor xxxRepository into the into the service,I feel a lot of work here and have invasive on the source code,I don't know, whether the test using dependency injection way will mock repository into service

My English is not so good.

mmnaseri commented 6 years ago

Hi,

First of all, thank you for the kind words, and sorry for the late reply. I see no problems with using dependency injection with this framework. For instance, if you wanted to make the mock instance available in your test config, you could do something along the lines of:

@Configuration
public class MyTestConfig {

    @Bean
    public PersonRepository personRepository() {
        return RepositoryFactoryBuilder.builder().mock(PersonRepository.class);
    }

}

This should make the mock instance readily available in your tests.