From 368f190dc51f39c56eea3dcf298554b6dc90afa5 Mon Sep 17 00:00:00 2001 From: Thomas Darimont Date: Wed, 2 Oct 2013 13:15:33 +0200 Subject: [PATCH] DATACMNS-374 - Improved error handling in case of a missing repository. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit In Repositories.getCrudInvoker(…) we now throw an IllegalArgumentException if we cannot find an appropriate repository for the given domain class. Previously it just blew up with a NullPointerException without a clear error message. Original pull request: #46. --- .../data/repository/support/Repositories.java | 2 ++ .../support/RepositoriesUnitTests.java | 22 +++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/src/main/java/org/springframework/data/repository/support/Repositories.java b/src/main/java/org/springframework/data/repository/support/Repositories.java index d1d0437db..82529d796 100644 --- a/src/main/java/org/springframework/data/repository/support/Repositories.java +++ b/src/main/java/org/springframework/data/repository/support/Repositories.java @@ -163,6 +163,8 @@ public class Repositories implements Iterable> { RepositoryInformation information = getRepositoryInformationFor(domainClass); Object repository = getRepositoryFor(domainClass); + Assert.notNull(repository, String.format("No repository found for domain class: %s", domainClass)); + if (repository instanceof CrudRepository) { return new CrudRepositoryInvoker((CrudRepository) repository); } else { diff --git a/src/test/java/org/springframework/data/repository/support/RepositoriesUnitTests.java b/src/test/java/org/springframework/data/repository/support/RepositoriesUnitTests.java index fcb06bf77..ece9e8082 100644 --- a/src/test/java/org/springframework/data/repository/support/RepositoriesUnitTests.java +++ b/src/test/java/org/springframework/data/repository/support/RepositoriesUnitTests.java @@ -25,7 +25,9 @@ import java.util.Collections; import java.util.List; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; import org.junit.runner.RunWith; import org.mockito.runners.MockitoJUnitRunner; import org.springframework.beans.factory.support.AbstractBeanDefinition; @@ -51,12 +53,15 @@ import org.springframework.data.repository.query.QueryMethod; * Unit tests for {@link Repositories}. * * @author Oliver Gierke + * @author Thomas Darimont */ @RunWith(MockitoJUnitRunner.class) public class RepositoriesUnitTests { ApplicationContext context; + @Rule public ExpectedException exception = ExpectedException.none(); + @Before public void setUp() { @@ -107,6 +112,19 @@ public class RepositoriesUnitTests { assertThat(repositories.getPersistentEntity(Address.class), is(notNullValue())); } + /** + * @see DATACMNS-374 + */ + @Test + public void shouldThrowMeaningfulExceptionWhenTheRepositoryForAGivenDomainClassCannotBeFound() { + + exception.expect(IllegalArgumentException.class); + exception.expectMessage(containsString("No repository found for domain class: ")); + + Repositories repositories = new Repositories(context); + repositories.getCrudInvoker(EntityWithoutRepository.class); + } + class Person { } @@ -115,6 +133,10 @@ public class RepositoriesUnitTests { } + class EntityWithoutRepository { + + } + interface PersonRepository extends CrudRepository { }