Browse Source

DATACMNS-374 - Improved error handling in case of a missing repository.

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.
pull/73/merge
Thomas Darimont 13 years ago committed by Oliver Gierke
parent
commit
368f190dc5
  1. 2
      src/main/java/org/springframework/data/repository/support/Repositories.java
  2. 22
      src/test/java/org/springframework/data/repository/support/RepositoriesUnitTests.java

2
src/main/java/org/springframework/data/repository/support/Repositories.java

@ -163,6 +163,8 @@ public class Repositories implements Iterable<Class<?>> { @@ -163,6 +163,8 @@ public class Repositories implements Iterable<Class<?>> {
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<T>((CrudRepository<T, Serializable>) repository);
} else {

22
src/test/java/org/springframework/data/repository/support/RepositoriesUnitTests.java

@ -25,7 +25,9 @@ import java.util.Collections; @@ -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; @@ -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 { @@ -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 { @@ -115,6 +133,10 @@ public class RepositoriesUnitTests {
}
class EntityWithoutRepository {
}
interface PersonRepository extends CrudRepository<Person, Long> {
}

Loading…
Cancel
Save