Added test case to check whether declaring a CRUD method in a repository interface extending Repository is delegated to the backing bean.

This commit is contained in:
Oliver Gierke
2011-05-13 10:03:48 +02:00
parent 138191fd19
commit 22f3e5cd1c
2 changed files with 18 additions and 4 deletions

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright 2008-2010 the original author or authors. * Copyright 2008-2011 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.

View File

@@ -25,6 +25,7 @@ import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner; import org.mockito.runners.MockitoJUnitRunner;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.Repository; import org.springframework.data.repository.Repository;
import org.springframework.data.repository.query.QueryLookupStrategy; import org.springframework.data.repository.query.QueryLookupStrategy;
import org.springframework.data.repository.query.QueryLookupStrategy.Key; import org.springframework.data.repository.query.QueryLookupStrategy.Key;
@@ -41,6 +42,9 @@ public class RepositoryFactorySupportUnitTests {
RepositoryFactorySupport factory = new DummyRepositoryFactory(); RepositoryFactorySupport factory = new DummyRepositoryFactory();
@Mock
CrudRepository<Object, Serializable> backingRepo;
@Mock @Mock
MyQueryCreationListener listener; MyQueryCreationListener listener;
@Mock @Mock
@@ -58,7 +62,15 @@ public class RepositoryFactorySupportUnitTests {
verify(listener, times(1)).onCreation(any(MyRepositoryQuery.class)); verify(listener, times(1)).onCreation(any(MyRepositoryQuery.class));
verify(otherListener, times(2)).onCreation(any(RepositoryQuery.class)); verify(otherListener, times(2)).onCreation(any(RepositoryQuery.class));
}
@Test
public void routesCallToRedeclaredMethodIntoTarget() {
ObjectRepository repository = factory.getRepository(ObjectRepository.class);
repository.save(repository);
verify(backingRepo, times(1)).save(any(Object.class));
} }
class DummyRepositoryFactory extends RepositoryFactorySupport { class DummyRepositoryFactory extends RepositoryFactorySupport {
@@ -77,14 +89,14 @@ public class RepositoryFactorySupportUnitTests {
@Override @Override
protected Object getTargetRepository(RepositoryMetadata metadata) { protected Object getTargetRepository(RepositoryMetadata metadata) {
return new Object(); return backingRepo;
} }
@Override @Override
protected Class<?> getRepositoryBaseClass(RepositoryMetadata metadata) { protected Class<?> getRepositoryBaseClass(RepositoryMetadata metadata) {
return Object.class; return CrudRepository.class;
} }
@@ -108,6 +120,8 @@ public class RepositoryFactorySupportUnitTests {
Object findByFoo(); Object findByFoo();
Object save(Object entity);
} }
interface PlainQueryCreationListener extends interface PlainQueryCreationListener extends