BAEL-3744: Added unit tests for Spring Framework design patterns.
This commit is contained in:
parent
31abf680ee
commit
76df3d0117
spring-core-3/src/test/java/com/baeldung/spring/patterns/factory
36
spring-core-3/src/test/java/com/baeldung/spring/patterns/factory/AnnotationConfigApplicationContextTest.java
Normal file
36
spring-core-3/src/test/java/com/baeldung/spring/patterns/factory/AnnotationConfigApplicationContextTest.java
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
package com.baeldung.spring.patterns.factory;
|
||||||
|
|
||||||
|
import static org.hamcrest.CoreMatchers.is;
|
||||||
|
import static org.hamcrest.MatcherAssert.assertThat;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.springframework.context.ApplicationContext;
|
||||||
|
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||||
|
|
||||||
|
public class AnnotationConfigApplicationContextTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@SuppressWarnings("resource")
|
||||||
|
public void whenGetSimpleBean_ThenReturnConstructedBean() {
|
||||||
|
|
||||||
|
ApplicationContext context = new AnnotationConfigApplicationContext(ApplicationConfig.class);
|
||||||
|
|
||||||
|
Foo foo = context.getBean(Foo.class);
|
||||||
|
|
||||||
|
assertNotNull(foo);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@SuppressWarnings("resource")
|
||||||
|
public void whenGetPrototypeBean_ThenReturnConstructedBean() {
|
||||||
|
|
||||||
|
String expectedName = "Some name";
|
||||||
|
ApplicationContext context = new AnnotationConfigApplicationContext(ApplicationConfig.class);
|
||||||
|
|
||||||
|
Bar bar = context.getBean(Bar.class, expectedName);
|
||||||
|
|
||||||
|
assertNotNull(bar);
|
||||||
|
assertThat(bar.getName(), is(expectedName));
|
||||||
|
}
|
||||||
|
}
|
9
spring-core-3/src/test/java/com/baeldung/spring/patterns/factory/ApplicationConfig.java
Normal file
9
spring-core-3/src/test/java/com/baeldung/spring/patterns/factory/ApplicationConfig.java
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
package com.baeldung.spring.patterns.factory;
|
||||||
|
|
||||||
|
import org.springframework.context.annotation.ComponentScan;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
@ComponentScan(basePackageClasses = ApplicationConfig.class)
|
||||||
|
public class ApplicationConfig {
|
||||||
|
}
|
@ -0,0 +1,20 @@
|
|||||||
|
package com.baeldung.spring.patterns.factory;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
|
||||||
|
import org.springframework.context.annotation.Scope;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
|
||||||
|
public class Bar {
|
||||||
|
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
public Bar(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,7 @@
|
|||||||
|
package com.baeldung.spring.patterns.factory;
|
||||||
|
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
public class Foo {
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user