BAEL-3290: Added unit test for autowiring by type

This commit is contained in:
Justin Albano 2019-12-10 08:35:09 -05:00
parent a96ad15223
commit 49b511d74a
4 changed files with 59 additions and 2 deletions

View 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>

View File

@ -18,9 +18,10 @@
</bean>
<!-- Autowired injection -->
<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>
<!-- Lazy instantiation -->

View File

@ -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());
}
}

View File

@ -21,6 +21,10 @@ public class XmlAppConfigUnitTest {
@Autowired
@Qualifier("xml-store-by-setter")
private Store storeBySetterInjection;
@Autowired
@Qualifier("xml-store-by-autowire-name")
private Store storeByAutowireInjectionByName;
@Autowired
@Qualifier("xml-store-by-setter-lazy")
@ -38,6 +42,12 @@ public class XmlAppConfigUnitTest {
assertNotNull(storeByConstructorInjection.getItem());
}
@Test
public void givenValidXmlConfig_WhenInjectStoreByAutowireInjectionByName_ThenBeanIsNotNull() {
assertNotNull(storeByAutowireInjectionByName);
assertNotNull(storeByAutowireInjectionByName.getItem());
}
@Test
public void givenValidXmlConfig_WhenInjectStoreBySetterInjectionLazy_ThenBeanIsNotNull() {
assertNotNull(storeBySetterInjectionLazy);