BAEL-3290: Added unit test for autowiring by type
This commit is contained in:
parent
a96ad15223
commit
49b511d74a
14
spring-di/src/main/resources/ioc-context-by-type.xml
Normal file
14
spring-di/src/main/resources/ioc-context-by-type.xml
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://www.springframework.org/schema/beans
|
||||||
|
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
|
||||||
|
|
||||||
|
<!-- Autowired injection -->
|
||||||
|
|
||||||
|
<bean id="item" class="org.baeldung.store.ItemImpl1" />
|
||||||
|
|
||||||
|
<bean id="xml-store-by-autowire-type" class="org.baeldung.store.Store" autowire="byType">
|
||||||
|
</bean>
|
||||||
|
|
||||||
|
</beans>
|
@ -20,7 +20,8 @@
|
|||||||
<!-- Autowired injection -->
|
<!-- Autowired injection -->
|
||||||
|
|
||||||
<bean id="item" class="org.baeldung.store.ItemImpl1" />
|
<bean id="item" class="org.baeldung.store.ItemImpl1" />
|
||||||
<bean id="xml-store-by-autowire" class="org.baeldung.store.Store" autowire="byName">
|
|
||||||
|
<bean id="xml-store-by-autowire-name" class="org.baeldung.store.Store" autowire="byName">
|
||||||
</bean>
|
</bean>
|
||||||
|
|
||||||
<!-- Lazy instantiation -->
|
<!-- Lazy instantiation -->
|
||||||
|
@ -0,0 +1,32 @@
|
|||||||
|
package org.baeldung.store;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertNotNull;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.beans.factory.annotation.Qualifier;
|
||||||
|
import org.springframework.test.context.ContextConfiguration;
|
||||||
|
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Separate unit test class where only one Item object is available for
|
||||||
|
* autowiring. If the ioc-context.xml were used for autowiring by type, there
|
||||||
|
* would be multiple qualifying Item objects, causing a failure.
|
||||||
|
*
|
||||||
|
* @author Justin Albano <albano.justin@gmail.com>
|
||||||
|
*/
|
||||||
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
|
@ContextConfiguration("classpath:/ioc-context-by-type.xml")
|
||||||
|
public class XmlAppConfigByTypeUnitTest {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
@Qualifier("xml-store-by-autowire-type")
|
||||||
|
private Store storeByAutowireInjectionByType;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenValidXmlConfig_WhenInjectStoreByAutowireInjectionByType_ThenBeanIsNotNull() {
|
||||||
|
assertNotNull(storeByAutowireInjectionByType);
|
||||||
|
assertNotNull(storeByAutowireInjectionByType.getItem());
|
||||||
|
}
|
||||||
|
}
|
@ -22,6 +22,10 @@ public class XmlAppConfigUnitTest {
|
|||||||
@Qualifier("xml-store-by-setter")
|
@Qualifier("xml-store-by-setter")
|
||||||
private Store storeBySetterInjection;
|
private Store storeBySetterInjection;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
@Qualifier("xml-store-by-autowire-name")
|
||||||
|
private Store storeByAutowireInjectionByName;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
@Qualifier("xml-store-by-setter-lazy")
|
@Qualifier("xml-store-by-setter-lazy")
|
||||||
private Store storeBySetterInjectionLazy;
|
private Store storeBySetterInjectionLazy;
|
||||||
@ -38,6 +42,12 @@ public class XmlAppConfigUnitTest {
|
|||||||
assertNotNull(storeByConstructorInjection.getItem());
|
assertNotNull(storeByConstructorInjection.getItem());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenValidXmlConfig_WhenInjectStoreByAutowireInjectionByName_ThenBeanIsNotNull() {
|
||||||
|
assertNotNull(storeByAutowireInjectionByName);
|
||||||
|
assertNotNull(storeByAutowireInjectionByName.getItem());
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenValidXmlConfig_WhenInjectStoreBySetterInjectionLazy_ThenBeanIsNotNull() {
|
public void givenValidXmlConfig_WhenInjectStoreBySetterInjectionLazy_ThenBeanIsNotNull() {
|
||||||
assertNotNull(storeBySetterInjectionLazy);
|
assertNotNull(storeBySetterInjectionLazy);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user