diff --git a/spring-data-commons-core/src/main/java/org/springframework/data/repository/core/support/RepositoryFactorySupport.java b/spring-data-commons-core/src/main/java/org/springframework/data/repository/core/support/RepositoryFactorySupport.java index 8e8ef5c39..5244fa08e 100644 --- a/spring-data-commons-core/src/main/java/org/springframework/data/repository/core/support/RepositoryFactorySupport.java +++ b/spring-data-commons-core/src/main/java/org/springframework/data/repository/core/support/RepositoryFactorySupport.java @@ -252,10 +252,11 @@ public abstract class RepositoryFactorySupport { this.target = target; QueryLookupStrategy lookupStrategy = getQueryLookupStrategy(queryLookupStrategyKey); + Iterable queryMethods = repositoryInformation.getQueryMethods(); if (lookupStrategy == null) { - if (repositoryInformation.hasCustomMethod()) { + if (queryMethods.iterator().hasNext()) { 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!"); @@ -264,7 +265,7 @@ public abstract class RepositoryFactorySupport { return; } - for (Method method : repositoryInformation.getQueryMethods()) { + for (Method method : queryMethods) { RepositoryQuery query = lookupStrategy.resolveQuery(method, repositoryInformation, namedQueries); invokeListeners(query); queries.put(method, query); diff --git a/spring-data-commons-core/src/test/java/org/springframework/data/repository/core/support/QueryExecutorMethodInterceptorUnitTests.java b/spring-data-commons-core/src/test/java/org/springframework/data/repository/core/support/QueryExecutorMethodInterceptorUnitTests.java index a2e386dcf..2693f90b9 100644 --- a/spring-data-commons-core/src/test/java/org/springframework/data/repository/core/support/QueryExecutorMethodInterceptorUnitTests.java +++ b/spring-data-commons-core/src/test/java/org/springframework/data/repository/core/support/QueryExecutorMethodInterceptorUnitTests.java @@ -18,16 +18,23 @@ package org.springframework.data.repository.core.support; import static org.mockito.Matchers.*; import static org.mockito.Mockito.*; +import java.lang.reflect.Method; +import java.util.Arrays; +import java.util.Collections; + import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; import org.mockito.runners.MockitoJUnitRunner; +import org.springframework.data.repository.core.NamedQueries; import org.springframework.data.repository.core.RepositoryInformation; +import org.springframework.data.repository.core.RepositoryMetadata; +import org.springframework.data.repository.query.QueryLookupStrategy; import org.springframework.data.repository.query.QueryLookupStrategy.Key; /** * Unit test for {@link QueryExecuterMethodInterceptor}. - * + * * @author Oliver Gierke */ @RunWith(MockitoJUnitRunner.class) @@ -37,11 +44,13 @@ public class QueryExecutorMethodInterceptorUnitTests { RepositoryFactorySupport factory; @Mock RepositoryInformation information; + @Mock + QueryLookupStrategy strategy; @Test(expected = IllegalStateException.class) - public void rejectsRepositoryInterfaceWithQueryMethodsIfNoQueryLookupStrategyIsDefined() { + public void rejectsRepositoryInterfaceWithQueryMethodsIfNoQueryLookupStrategyIsDefined() throws Exception { - when(information.hasCustomMethod()).thenReturn(true); + when(information.getQueryMethods()).thenReturn(Arrays.asList(Object.class.getMethod("toString"))); when(factory.getQueryLookupStrategy(any(Key.class))).thenReturn(null); factory.new QueryExecutorMethodInterceptor(information, null, new Object()); @@ -50,10 +59,10 @@ public class QueryExecutorMethodInterceptorUnitTests { @Test public void skipsQueryLookupsIfQueryLookupStrategyIsNull() { - when(information.hasCustomMethod()).thenReturn(false); - when(factory.getQueryLookupStrategy(any(Key.class))).thenReturn(null); + when(information.getQueryMethods()).thenReturn(Collections. emptySet()); + when(factory.getQueryLookupStrategy(any(Key.class))).thenReturn(strategy); factory.new QueryExecutorMethodInterceptor(information, null, new Object()); - verify(information, times(0)).getQueryMethods(); + verify(strategy, times(0)).resolveQuery(any(Method.class), any(RepositoryMetadata.class), any(NamedQueries.class)); } }