diff --git a/spring-data-commons-core/src/main/java/org/springframework/data/repository/support/RepositoryFactorySupport.java b/spring-data-commons-core/src/main/java/org/springframework/data/repository/support/RepositoryFactorySupport.java index c6b8477be..5aefd18ae 100644 --- a/spring-data-commons-core/src/main/java/org/springframework/data/repository/support/RepositoryFactorySupport.java +++ b/spring-data-commons-core/src/main/java/org/springframework/data/repository/support/RepositoryFactorySupport.java @@ -190,14 +190,15 @@ public abstract class RepositoryFactorySupport { protected abstract Class getRepositoryBaseClass( Class repositoryInterface); - - /** - * Returns the {@link QueryLookupStrategy} for the given {@link Key}. - * - * @param key can be {@literal null} - * @return - */ - protected abstract QueryLookupStrategy getQueryLookupStrategy(Key key); + /** + * Returns the {@link QueryLookupStrategy} for the given {@link Key}. + * + * @param key can be {@literal null} + * @return the {@link QueryLookupStrategy} to use or {@literal null} if no queries should be looked up. + */ + protected QueryLookupStrategy getQueryLookupStrategy(Key key) { + return null; + } /** @@ -254,6 +255,18 @@ public abstract class RepositoryFactorySupport { QueryLookupStrategy lookupStrategy = getQueryLookupStrategy(queryLookupStrategyKey); + + if (lookupStrategy == null) { + + if (repositoryMetadata.hasCustomMethod()) { + throw new IllegalStateException( + "You have defined query method in the repository but " + + "you don't have no query lookup strategy defined. The " + + "infrastructure apparently does not support query methods!"); + } + + return; + } for (Method method : metadata.getQueryMethods()) { RepositoryQuery query = diff --git a/spring-data-commons-core/src/test/java/org/springframework/data/repository/support/QueryExecuterMethodInterceptorUnitTests.java b/spring-data-commons-core/src/test/java/org/springframework/data/repository/support/QueryExecuterMethodInterceptorUnitTests.java new file mode 100644 index 000000000..a9e41da5d --- /dev/null +++ b/spring-data-commons-core/src/test/java/org/springframework/data/repository/support/QueryExecuterMethodInterceptorUnitTests.java @@ -0,0 +1,59 @@ +/* + * Copyright 2011 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.repository.support; + +import static org.mockito.Matchers.*; +import static org.mockito.Mockito.*; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.runners.MockitoJUnitRunner; +import org.springframework.data.repository.query.QueryLookupStrategy.Key; +import org.springframework.data.repository.support.RepositoryFactorySupport.QueryExecuterMethodInterceptor; + +/** + * Unit test for {@link QueryExecuterMethodInterceptor}. + * + * @author Oliver Gierke + */ +@RunWith(MockitoJUnitRunner.class) +public class QueryExecuterMethodInterceptorUnitTests { + + @Mock + RepositoryFactorySupport factory; + @Mock + RepositoryMetadata metadata; + + @Test(expected=IllegalStateException.class) + public void rejectsRepositoryInterfaceWithQueryMethodsIfNoQueryLookupStrategyIsDefined() { + + when(metadata.hasCustomMethod()).thenReturn(true); + when(factory.getQueryLookupStrategy(any(Key.class))).thenReturn(null); + + factory.new QueryExecuterMethodInterceptor(metadata, null, new Object()); + } + + @Test + public void skipsQueryLookupsIfQueryLookupStrategyIsNull() { + + when(metadata.hasCustomMethod()).thenReturn(false); + when(factory.getQueryLookupStrategy(any(Key.class))).thenReturn(null); + + factory.new QueryExecuterMethodInterceptor(metadata, null, new Object()); + verify(metadata, times(0)).getQueryMethods(); + } +}