mmnaseri / spring-data-mock

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

Mocking methods with @Query in Repository #149

Closed pvpkiran closed 6 years ago

pvpkiran commented 7 years ago

Hi, I want to know how I can mock the repository methods which has @Query defined.

Thanks

iceberk commented 6 years ago

As I understand, the only way to mock methods annotated with @Query is to provide custom implementation, like: PersonRepository personRepository = new RepositoryMockBuilder().usingImplementation(RepoMock.class).mock(PersonRepository.class); Where RepoMock is following:

public class RepoMock implements DataStoreAware {
    private DataStore dataStore;

    //this method is annotated with @Query in PersonRepository
    public List<Person> findSome() {
        return new ArrayList<>(dataStore.retrieveAll());
    }

    @Override
    public void setDataStore(DataStore dataStore) {
        this.dataStore = dataStore;
    }
}
mmnaseri commented 6 years ago

@iceberk thanks for the comment.

RE @pvpkiran: That is exactly what you would have to do. There is no way that this framework would be able to parse and process all vendor-specific SQL/NoSQL queries and provide the implementation for you. You'll have to replace the behavior of native queries with your own Java-based implementation.