Initial commit for "Spring Custom Annotation with Dynamic Qualifier"

article.
This commit is contained in:
xsalefter
2015-10-21 00:50:44 +07:00
parent 6aec841b73
commit 32ea1e0a1e
10 changed files with 394 additions and 0 deletions

View File

@@ -0,0 +1,53 @@
package org.baeldung.customannotation;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.junit.Assert.assertThat;
import java.lang.reflect.Type;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { CustomAnnotationConfiguration.class })
public class DataAccessFieldCallbackTest {
@Autowired
private ConfigurableListableBeanFactory configurableListableBeanFactory;
@Autowired
private BeanWithGenericDAO beanWithGenericDAO;
@Rule
public ExpectedException ex = ExpectedException.none();
@Test
public void whenObjectCreated_thenObjectCreationIsSuccessful() {
final DataAccessFieldCallback dataAccessFieldCallback = new DataAccessFieldCallback(configurableListableBeanFactory, beanWithGenericDAO);
assertThat(dataAccessFieldCallback, is(notNullValue()));
}
@Test
public void whenMethodGenericTypeIsValidCalled_thenReturnCorrectValue()
throws NoSuchFieldException, SecurityException {
final DataAccessFieldCallback callback = new DataAccessFieldCallback(configurableListableBeanFactory, beanWithGenericDAO);
final Type fieldType = BeanWithGenericDAO.class.getDeclaredField("personGenericDAO").getGenericType();
final boolean result = callback.genericTypeIsValid(Person.class, fieldType);
assertThat(result, is(true));
}
@Test
public void whenMethodGetBeanInstanceCalled_thenReturnCorrectInstance() {
final DataAccessFieldCallback callback = new DataAccessFieldCallback(configurableListableBeanFactory, beanWithGenericDAO);
final Object result = callback.getBeanInstance("personGenericDAO", GenericDAO.class, Person.class);
assertThat((result instanceof GenericDAO), is(true));
}
}