Code and related files for the dependency injection demos.
This commit is contained in:
+9
-5
@@ -8,20 +8,24 @@ import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.support.AnnotationConfigContextLoader;
|
||||
|
||||
import com.baeldung.configuration.ApplicationContextTestAutowiredName;
|
||||
import com.baeldung.dependency.ArbitraryDependency;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(locations={
|
||||
"/applicationContextTest-@Autowired-Name.xml"})
|
||||
public class FieldAutowiredNameDemo {
|
||||
@ContextConfiguration(
|
||||
loader=AnnotationConfigContextLoader.class,
|
||||
classes=ApplicationContextTestAutowiredName.class)
|
||||
public class FieldAutowiredNameTest {
|
||||
|
||||
@Autowired
|
||||
private ArbitraryDependency autowiredFieldDependency;
|
||||
|
||||
@Test
|
||||
public void autowiredFieldDependency_MUST_BE_AUTOWIRED_Correctly() {
|
||||
public void givenAutowiredAnnotation_WhenOnField_ThenDependencyValid(){
|
||||
assertNotNull(autowiredFieldDependency);
|
||||
assertEquals("Arbitrary Dependency", autowiredFieldDependency.toString());
|
||||
assertEquals("Arbitrary Dependency",
|
||||
autowiredFieldDependency.toString());
|
||||
}
|
||||
}
|
||||
+7
-4
@@ -8,19 +8,22 @@ import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.support.AnnotationConfigContextLoader;
|
||||
|
||||
import com.baeldung.configuration.ApplicationContextTestAutowiredType;
|
||||
import com.baeldung.dependency.ArbitraryDependency;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(locations={
|
||||
"/applicationContextTest-@Autowired-Type.xml"})
|
||||
public class FieldAutowiredDemo {
|
||||
@ContextConfiguration(
|
||||
loader=AnnotationConfigContextLoader.class,
|
||||
classes=ApplicationContextTestAutowiredType.class)
|
||||
public class FieldAutowiredTest {
|
||||
|
||||
@Autowired
|
||||
private ArbitraryDependency fieldDependency;
|
||||
|
||||
@Test
|
||||
public void fieldDependency_MUST_BE_AUTOWIRED_Correctly() {
|
||||
public void givenAutowired_WhenSetOnField_ThenDependencyResolved() {
|
||||
assertNotNull(fieldDependency);
|
||||
assertEquals("Arbitrary Dependency", fieldDependency.toString());
|
||||
}
|
||||
+10
-6
@@ -9,13 +9,16 @@ 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;
|
||||
import org.springframework.test.context.support.AnnotationConfigContextLoader;
|
||||
|
||||
import com.baeldung.configuration.ApplicationContextTestAutowiredQualifier;
|
||||
import com.baeldung.dependency.ArbitraryDependency;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(locations={
|
||||
"/applicationContextTest-@Autowired-Qualifier.xml"})
|
||||
public class FieldQualifierAutowiredDemo {
|
||||
@ContextConfiguration(
|
||||
loader=AnnotationConfigContextLoader.class,
|
||||
classes=ApplicationContextTestAutowiredQualifier.class)
|
||||
public class FieldQualifierAutowiredTest {
|
||||
|
||||
@Autowired
|
||||
@Qualifier("autowiredFieldDependency")
|
||||
@@ -26,14 +29,15 @@ public class FieldQualifierAutowiredDemo {
|
||||
private ArbitraryDependency fieldDependency2;
|
||||
|
||||
@Test
|
||||
public void fieldDependency1_MUST_BE_AUTOWIRED_Correctly() {
|
||||
public void givenAutowiredQualifier_WhenOnField_ThenDep1Valid(){
|
||||
assertNotNull(fieldDependency1);
|
||||
assertEquals("Arbitrary Dependency", fieldDependency1.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void fieldDependency2_MUST_BE_AUTOWIRED_Correctly() {
|
||||
public void givenAutowiredQualifier_WhenOnField_ThenDep2Valid(){
|
||||
assertNotNull(fieldDependency2);
|
||||
assertEquals("Another Arbitrary Dependency", fieldDependency2.toString());
|
||||
assertEquals("Another Arbitrary Dependency",
|
||||
fieldDependency2.toString());
|
||||
}
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
package com.baeldung.configuration;
|
||||
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
@ComponentScan(basePackages={"com.baeldung.dependency"})
|
||||
public class ApplicationContextTestAutowiredName {
|
||||
}
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
package com.baeldung.configuration;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import com.baeldung.dependency.AnotherArbitraryDependency;
|
||||
import com.baeldung.dependency.ArbitraryDependency;
|
||||
|
||||
@Configuration
|
||||
public class ApplicationContextTestAutowiredQualifier {
|
||||
|
||||
@Bean
|
||||
public ArbitraryDependency autowiredFieldDependency() {
|
||||
ArbitraryDependency autowiredFieldDependency = new ArbitraryDependency();
|
||||
|
||||
return autowiredFieldDependency;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public ArbitraryDependency anotherAutowiredFieldDependency() {
|
||||
ArbitraryDependency anotherAutowiredFieldDependency = new AnotherArbitraryDependency();
|
||||
|
||||
return anotherAutowiredFieldDependency;
|
||||
}
|
||||
}
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
package com.baeldung.configuration;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import com.baeldung.dependency.ArbitraryDependency;
|
||||
|
||||
@Configuration
|
||||
public class ApplicationContextTestAutowiredType {
|
||||
|
||||
@Bean
|
||||
public ArbitraryDependency autowiredFieldDependency() {
|
||||
ArbitraryDependency autowiredFieldDependency = new ArbitraryDependency();
|
||||
return autowiredFieldDependency;
|
||||
}
|
||||
}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
package com.baeldung.configuration;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import com.baeldung.dependency.ArbitraryDependency;
|
||||
import com.baeldung.dependency.YetAnotherArbitraryDependency;
|
||||
|
||||
@Configuration
|
||||
public class ApplicationContextTestInjectName {
|
||||
|
||||
@Bean
|
||||
public ArbitraryDependency yetAnotherFieldInjectDependency() {
|
||||
ArbitraryDependency yetAnotherFieldInjectDependency = new YetAnotherArbitraryDependency();
|
||||
return yetAnotherFieldInjectDependency;
|
||||
}
|
||||
}
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
package com.baeldung.configuration;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import com.baeldung.dependency.AnotherArbitraryDependency;
|
||||
import com.baeldung.dependency.ArbitraryDependency;
|
||||
|
||||
@Configuration
|
||||
public class ApplicationContextTestInjectQualifier {
|
||||
|
||||
@Bean
|
||||
public ArbitraryDependency defaultFile() {
|
||||
ArbitraryDependency defaultFile = new ArbitraryDependency();
|
||||
return defaultFile;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public ArbitraryDependency namedFile() {
|
||||
ArbitraryDependency namedFile = new AnotherArbitraryDependency();
|
||||
return namedFile;
|
||||
}
|
||||
}
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
package com.baeldung.configuration;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import com.baeldung.dependency.ArbitraryDependency;
|
||||
|
||||
@Configuration
|
||||
public class ApplicationContextTestInjectType {
|
||||
|
||||
@Bean
|
||||
public ArbitraryDependency injectDependency() {
|
||||
ArbitraryDependency injectDependency = new ArbitraryDependency();
|
||||
return injectDependency;
|
||||
}
|
||||
}
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
package com.baeldung.configuration;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
public class ApplicationContextTestResourceNameType {
|
||||
|
||||
@Bean(name="namedFile")
|
||||
public File namedFile() {
|
||||
File namedFile = new File("namedFile.txt");
|
||||
return namedFile;
|
||||
}
|
||||
}
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
package com.baeldung.configuration;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
public class ApplicationContextTestResourceQualifier {
|
||||
|
||||
@Bean(name="defaultFile")
|
||||
public File defaultFile() {
|
||||
File defaultFile = new File("defaultFile.txt");
|
||||
return defaultFile;
|
||||
}
|
||||
|
||||
@Bean(name="namedFile")
|
||||
public File namedFile() {
|
||||
File namedFile = new File("namedFile.txt");
|
||||
return namedFile;
|
||||
}
|
||||
}
|
||||
+3
@@ -1,5 +1,8 @@
|
||||
package com.baeldung.dependency;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class AnotherArbitraryDependency extends ArbitraryDependency {
|
||||
|
||||
private final String label = "Another Arbitrary Dependency";
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
package com.baeldung.dependency;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component(value="autowiredFieldDependency")
|
||||
public class ArbitraryDependency {
|
||||
|
||||
private final String label = "Arbitrary Dependency";
|
||||
|
||||
+3
@@ -1,5 +1,8 @@
|
||||
package com.baeldung.dependency;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class YetAnotherArbitraryDependency extends ArbitraryDependency {
|
||||
|
||||
private final String label = "Yet Another Arbitrary Dependency";
|
||||
|
||||
+9
-5
@@ -10,21 +10,25 @@ import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.support.AnnotationConfigContextLoader;
|
||||
|
||||
import com.baeldung.configuration.ApplicationContextTestInjectName;
|
||||
import com.baeldung.dependency.ArbitraryDependency;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(locations={
|
||||
"/applicationContextTest-@Inject-Name.xml"})
|
||||
public class FieldByNameInjectDemo {
|
||||
@ContextConfiguration(
|
||||
loader=AnnotationConfigContextLoader.class,
|
||||
classes=ApplicationContextTestInjectName.class)
|
||||
public class FieldByNameInjectTest {
|
||||
|
||||
@Inject
|
||||
@Named("yetAnotherFieldInjectDependency")
|
||||
private ArbitraryDependency yetAnotherFieldInjectDependency;
|
||||
|
||||
@Test
|
||||
public void yetAnotherFieldInjectDependency_MUST_BE_INJECTED_Correctly() {
|
||||
public void givenInjectQualifier_WhenSetOnField_ThenDependencyValid() {
|
||||
assertNotNull(yetAnotherFieldInjectDependency);
|
||||
assertEquals("Yet Another Arbitrary Dependency", yetAnotherFieldInjectDependency.toString());
|
||||
assertEquals("Yet Another Arbitrary Dependency",
|
||||
yetAnotherFieldInjectDependency.toString());
|
||||
}
|
||||
}
|
||||
@@ -1,27 +0,0 @@
|
||||
package com.baeldung.inject;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import com.baeldung.dependency.ArbitraryDependency;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(locations={
|
||||
"/applicationContextTest-@Inject-Type.xml"})
|
||||
public class FieldInjectDemo {
|
||||
|
||||
@Inject
|
||||
private ArbitraryDependency inject1Dependency;
|
||||
|
||||
@Test
|
||||
public void fieldDependency_MUST_BE_INJECTED_Successfully() {
|
||||
assertNotNull(inject1Dependency);
|
||||
assertEquals("Arbitrary Dependency", inject1Dependency.toString());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package com.baeldung.inject;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.support.AnnotationConfigContextLoader;
|
||||
|
||||
import com.baeldung.configuration.ApplicationContextTestInjectType;
|
||||
import com.baeldung.dependency.ArbitraryDependency;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(
|
||||
loader=AnnotationConfigContextLoader.class,
|
||||
classes=ApplicationContextTestInjectType.class)
|
||||
public class FieldInjectTest {
|
||||
|
||||
@Inject
|
||||
private ArbitraryDependency fieldInjectDependency;
|
||||
|
||||
@Test
|
||||
public void givenInjectAnnotation_WhenOnField_ThenValidDependency(){
|
||||
assertNotNull(fieldInjectDependency);
|
||||
assertEquals("Arbitrary Dependency",
|
||||
fieldInjectDependency.toString());
|
||||
}
|
||||
}
|
||||
+11
-7
@@ -10,13 +10,15 @@ import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.support.AnnotationConfigContextLoader;
|
||||
|
||||
import com.baeldung.configuration.ApplicationContextTestInjectQualifier;
|
||||
import com.baeldung.dependency.ArbitraryDependency;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(locations={
|
||||
"/applicationContextTest-@Inject-Qualifier.xml"})
|
||||
public class FieldQualifierInjectDemo {
|
||||
@ContextConfiguration(loader=AnnotationConfigContextLoader.class,
|
||||
classes=ApplicationContextTestInjectQualifier.class)
|
||||
public class FieldQualifierInjectTest {
|
||||
|
||||
@Inject
|
||||
@Qualifier("defaultFile")
|
||||
@@ -27,14 +29,16 @@ public class FieldQualifierInjectDemo {
|
||||
private ArbitraryDependency namedDependency;
|
||||
|
||||
@Test
|
||||
public void defaultDependency_MUST_BE_INJECTED_Successfully() {
|
||||
public void givenInjectQualifier_WhenOnField_ThenDefaultFileValid(){
|
||||
assertNotNull(defaultDependency);
|
||||
assertEquals("Arbitrary Dependency", defaultDependency.toString());
|
||||
assertEquals("Arbitrary Dependency",
|
||||
defaultDependency.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void namedDependency_MUST_BE_INJECTED_Correctly() {
|
||||
public void givenInjectQualifier_WhenOnField_ThenNamedFileValid(){
|
||||
assertNotNull(defaultDependency);
|
||||
assertEquals("Another Arbitrary Dependency", namedDependency.toString());
|
||||
assertEquals("Another Arbitrary Dependency",
|
||||
namedDependency.toString());
|
||||
}
|
||||
}
|
||||
-25
@@ -1,25 +0,0 @@
|
||||
package com.baeldung.resource;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(locations={
|
||||
"/applicationContextTest-@Resource-NameType.xml"})
|
||||
public class FieldResourceInjectionDemo {
|
||||
|
||||
@Resource(name="namedFile")
|
||||
private File defaultFile;
|
||||
|
||||
@Test
|
||||
public void plainResourceAnnotation_MUST_FIND_DefaultFile() {
|
||||
assertNotNull(defaultFile);
|
||||
}
|
||||
}
|
||||
+31
@@ -0,0 +1,31 @@
|
||||
package com.baeldung.resource;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.support.AnnotationConfigContextLoader;
|
||||
|
||||
import com.baeldung.configuration.ApplicationContextTestResourceNameType;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(
|
||||
loader=AnnotationConfigContextLoader.class,
|
||||
classes=ApplicationContextTestResourceNameType.class)
|
||||
public class FieldResourceInjectionTest {
|
||||
|
||||
@Resource(name="namedFile")
|
||||
private File defaultFile;
|
||||
|
||||
@Test
|
||||
public void givenResourceAnnotation_WhenOnField_ThenDependencyValid(){
|
||||
assertNotNull(defaultFile);
|
||||
assertEquals("namedFile.txt", defaultFile.getName());
|
||||
}
|
||||
}
|
||||
+8
-4
@@ -12,17 +12,21 @@ import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.support.AnnotationConfigContextLoader;
|
||||
|
||||
import com.baeldung.configuration.ApplicationContextTestResourceQualifier;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(locations={
|
||||
"/applicationContextTest-@Resource-Qualifier.xml"})
|
||||
public class MethodByQualifierResourceDemo {
|
||||
@ContextConfiguration(
|
||||
loader=AnnotationConfigContextLoader.class,
|
||||
classes=ApplicationContextTestResourceQualifier.class)
|
||||
public class MethodByQualifierResourceTest {
|
||||
|
||||
private File arbDependency;
|
||||
private File anotherArbDependency;
|
||||
|
||||
@Test
|
||||
public void dependencies_MUST_BE_INJECTED_Correctly() {
|
||||
public void givenResourceQualifier_WhenSetter_ThenValidDependencies(){
|
||||
assertNotNull(arbDependency);
|
||||
assertEquals("namedFile.txt", arbDependency.getName());
|
||||
assertNotNull(anotherArbDependency);
|
||||
+10
-4
@@ -1,5 +1,6 @@
|
||||
package com.baeldung.resource;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
import java.io.File;
|
||||
@@ -10,11 +11,15 @@ import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.support.AnnotationConfigContextLoader;
|
||||
|
||||
import com.baeldung.configuration.ApplicationContextTestResourceNameType;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(locations={
|
||||
"/applicationContextTest-@Resource-NameType.xml"})
|
||||
public class MethodByTypeResourceDemo {
|
||||
@ContextConfiguration(
|
||||
loader=AnnotationConfigContextLoader.class,
|
||||
classes=ApplicationContextTestResourceNameType.class)
|
||||
public class MethodByTypeResourceTest {
|
||||
|
||||
private File defaultFile;
|
||||
|
||||
@@ -24,7 +29,8 @@ public class MethodByTypeResourceDemo {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void defaultFile_MUST_BE_INJECTED_Correctly() {
|
||||
public void givenResourceAnnotation_WhenSetter_ThenValidDependency(){
|
||||
assertNotNull(defaultFile);
|
||||
assertEquals("namedFile.txt", defaultFile.getName());
|
||||
}
|
||||
}
|
||||
+11
-4
@@ -1,4 +1,6 @@
|
||||
package com.baeldung.resource;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
import java.io.File;
|
||||
@@ -9,11 +11,15 @@ import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.support.AnnotationConfigContextLoader;
|
||||
|
||||
import com.baeldung.configuration.ApplicationContextTestResourceNameType;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(locations={
|
||||
"/applicationContextTest-@Resource-NameType.xml"})
|
||||
public class MethodResourceInjectionDemo {
|
||||
@ContextConfiguration(
|
||||
loader=AnnotationConfigContextLoader.class,
|
||||
classes=ApplicationContextTestResourceNameType.class)
|
||||
public class MethodResourceInjectionTest {
|
||||
|
||||
private File defaultFile;
|
||||
|
||||
@@ -23,7 +29,8 @@ public class MethodResourceInjectionDemo {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void defaultFile_MUST_BE_INJECTED_Correctly() {
|
||||
public void givenResourceAnnotation_WhenSetter_ThenDependencyValid(){
|
||||
assertNotNull(defaultFile);
|
||||
assertEquals("namedFile.txt", defaultFile.getName());
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
package com.baeldung.resource;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
@@ -10,18 +10,21 @@ import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.support.AnnotationConfigContextLoader;
|
||||
|
||||
import com.baeldung.configuration.ApplicationContextTestResourceNameType;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(locations={
|
||||
"/applicationContextTest-@Resource-NameType.xml"})
|
||||
@ContextConfiguration(loader=AnnotationConfigContextLoader.class,
|
||||
classes=ApplicationContextTestResourceNameType.class)
|
||||
public class NamedResourceTest {
|
||||
|
||||
@Resource(name="namedFile")
|
||||
private File testFile;
|
||||
|
||||
@Test
|
||||
public void namedResource_MUST_FIND_SPECIFIED_File() {
|
||||
public void givenResourceAnnotation_WhenOnField_THEN_DEPENDENCY_Found() {
|
||||
assertNotNull(testFile);
|
||||
assertTrue(testFile.getName().equals("namedFile.txt"));
|
||||
assertEquals("namedFile.txt", testFile.getName());
|
||||
}
|
||||
}
|
||||
|
||||
-35
@@ -1,35 +0,0 @@
|
||||
package com.baeldung.resource;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(locations={
|
||||
"/applicationContextTest-@Resource-Qualifier.xml"})
|
||||
public class QualifierResourceInjectionDemo {
|
||||
|
||||
@Resource
|
||||
private File defaultFile;
|
||||
|
||||
@Resource
|
||||
@Qualifier("namedFile")
|
||||
private File namedFile;
|
||||
|
||||
@Test
|
||||
public void defaultFile_MUST_BE_Valid() {
|
||||
assertNotNull(defaultFile);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void namedFile_MUST_BE_Valid() {
|
||||
assertNotNull(namedFile);
|
||||
}
|
||||
}
|
||||
+43
@@ -0,0 +1,43 @@
|
||||
package com.baeldung.resource;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.support.AnnotationConfigContextLoader;
|
||||
|
||||
import com.baeldung.configuration.ApplicationContextTestResourceQualifier;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(
|
||||
loader=AnnotationConfigContextLoader.class,
|
||||
classes=ApplicationContextTestResourceQualifier.class)
|
||||
public class QualifierResourceInjectionTest {
|
||||
|
||||
@Resource
|
||||
@Qualifier("defaultFile")
|
||||
private File dependency1;
|
||||
|
||||
@Resource
|
||||
@Qualifier("namedFile")
|
||||
private File dependency2;
|
||||
|
||||
@Test
|
||||
public void givenResourceAnnotation_WhenField_ThenDependency1Valid(){
|
||||
assertNotNull(dependency1);
|
||||
assertEquals("defaultFile.txt", dependency1.getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenResourceQualifier_WhenField_ThenDependency2Valid(){
|
||||
assertNotNull(dependency2);
|
||||
assertEquals("namedFile.txt", dependency2.getName());
|
||||
}
|
||||
}
|
||||
+10
-4
@@ -1,4 +1,6 @@
|
||||
package com.baeldung.resource;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
import java.io.File;
|
||||
@@ -9,11 +11,14 @@ import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.support.AnnotationConfigContextLoader;
|
||||
|
||||
import com.baeldung.configuration.ApplicationContextTestResourceNameType;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(locations={
|
||||
"/applicationContextTest-@Resource-NameType.xml"})
|
||||
public class SetterResourceInjectionDemo {
|
||||
@ContextConfiguration(loader=AnnotationConfigContextLoader.class,
|
||||
classes=ApplicationContextTestResourceNameType.class)
|
||||
public class SetterResourceInjectionTest {
|
||||
|
||||
private File defaultFile;
|
||||
|
||||
@@ -23,7 +28,8 @@ public class SetterResourceInjectionDemo {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setter_MUST_INJECT_Resource() {
|
||||
public void givenResourceAnnotation_WhenOnSetter_THEN_MUST_INJECT_Dependency() {
|
||||
assertNotNull(defaultFile);
|
||||
assertEquals("namedFile.txt", defaultFile.getName());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user