BAEL-3290: Added test cases to exercise DI configuration
This commit is contained in:
parent
3539c9900a
commit
a96ad15223
@ -1,5 +0,0 @@
|
|||||||
package com.baeldung.dependency.ioc;
|
|
||||||
|
|
||||||
public interface Item {
|
|
||||||
|
|
||||||
}
|
|
@ -1,4 +1,4 @@
|
|||||||
package com.baeldung.dependency.ioc;
|
package org.baeldung.store;
|
||||||
|
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
|
|
5
spring-di/src/main/java/org/baeldung/store/Item.java
Normal file
5
spring-di/src/main/java/org/baeldung/store/Item.java
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
package org.baeldung.store;
|
||||||
|
|
||||||
|
public interface Item {
|
||||||
|
|
||||||
|
}
|
@ -1,4 +1,4 @@
|
|||||||
package com.baeldung.dependency.ioc;
|
package org.baeldung.store;
|
||||||
|
|
||||||
public class ItemImpl1 implements Item {
|
public class ItemImpl1 implements Item {
|
||||||
|
|
@ -1,4 +1,4 @@
|
|||||||
package com.baeldung.dependency.ioc;
|
package org.baeldung.store;
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
|
@ -8,7 +8,7 @@
|
|||||||
|
|
||||||
<bean id="item1" class="org.baeldung.store.ItemImpl1" />
|
<bean id="item1" class="org.baeldung.store.ItemImpl1" />
|
||||||
<bean id="xml-store-by-constructor" class="org.baeldung.store.Store">
|
<bean id="xml-store-by-constructor" class="org.baeldung.store.Store">
|
||||||
<constructor-arg type="ItemImpl1" index="0" name="item" ref="item1" />
|
<constructor-arg type="Item" index="0" name="item" ref="item1" />
|
||||||
</bean>
|
</bean>
|
||||||
|
|
||||||
<!-- Setter injection -->
|
<!-- Setter injection -->
|
||||||
|
@ -1,24 +0,0 @@
|
|||||||
package com.baeldung.dependency.ioc;
|
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
|
||||||
|
|
||||||
import org.junit.jupiter.api.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;
|
|
||||||
|
|
||||||
@RunWith(SpringJUnit4ClassRunner.class)
|
|
||||||
@ContextConfiguration("classpath:/ioc-context.xml")
|
|
||||||
public class XmlAppConfigTest {
|
|
||||||
|
|
||||||
@Autowired
|
|
||||||
@Qualifier("xml-store-by-constructor")
|
|
||||||
private Store storeByConstructorInjection;
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void givenValidXmlConfig_WhenInjectStoreByConstructorInject_ThenBeanIsNotNull() {
|
|
||||||
assertNotNull(storeByConstructorInjection);
|
|
||||||
}
|
|
||||||
}
|
|
@ -0,0 +1,36 @@
|
|||||||
|
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;
|
||||||
|
|
||||||
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
|
@ContextConfiguration(classes = AppConfig.class)
|
||||||
|
public class AppConfigUnitTest {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
@Qualifier("storeThroughConstructorInjection")
|
||||||
|
private Store storeByConstructorInjection;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
@Qualifier("storeThroughSetterInjection")
|
||||||
|
private Store storeBySetterInjection;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenValidXmlConfig_WhenInjectStoreByConstructorInjection_ThenBeanIsNotNull() {
|
||||||
|
assertNotNull(storeByConstructorInjection);
|
||||||
|
assertNotNull(storeByConstructorInjection.getItem());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenValidXmlConfig_WhenInjectStoreBySetterInjection_ThenBeanIsNotNull() {
|
||||||
|
assertNotNull(storeBySetterInjection);
|
||||||
|
assertNotNull(storeByConstructorInjection.getItem());
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,46 @@
|
|||||||
|
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;
|
||||||
|
|
||||||
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
|
@ContextConfiguration("classpath:/ioc-context.xml")
|
||||||
|
public class XmlAppConfigUnitTest {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
@Qualifier("xml-store-by-constructor")
|
||||||
|
private Store storeByConstructorInjection;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
@Qualifier("xml-store-by-setter")
|
||||||
|
private Store storeBySetterInjection;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
@Qualifier("xml-store-by-setter-lazy")
|
||||||
|
private Store storeBySetterInjectionLazy;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenValidXmlConfig_WhenInjectStoreByConstructorInjection_ThenBeanIsNotNull() {
|
||||||
|
assertNotNull(storeByConstructorInjection);
|
||||||
|
assertNotNull(storeByConstructorInjection.getItem());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenValidXmlConfig_WhenInjectStoreBySetterInjection_ThenBeanIsNotNull() {
|
||||||
|
assertNotNull(storeBySetterInjection);
|
||||||
|
assertNotNull(storeByConstructorInjection.getItem());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenValidXmlConfig_WhenInjectStoreBySetterInjectionLazy_ThenBeanIsNotNull() {
|
||||||
|
assertNotNull(storeBySetterInjectionLazy);
|
||||||
|
assertNotNull(storeByConstructorInjection.getItem());
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user