diff --git a/spring-core-4/README.md b/spring-core-4/README.md index 9da90ac77a..706c330f39 100644 --- a/spring-core-4/README.md +++ b/spring-core-4/README.md @@ -8,4 +8,7 @@ This module contains articles about core Spring functionality - [How to dynamically Autowire a Bean in Spring](https://www.baeldung.com/spring-dynamic-autowire) - [Spring @Import Annotation](https://www.baeldung.com/spring-import-annotation) - [Spring BeanPostProcessor](https://www.baeldung.com/spring-beanpostprocessor) +- [Using @Autowired in Abstract Classes](https://www.baeldung.com/spring-autowired-abstract-class) +- [Running Setup Data on Startup in Spring](https://www.baeldung.com/running-setup-logic-on-startup-in-spring) +- [Constructor Injection in Spring with Lombok](https://www.baeldung.com/spring-injection-lombok) - More articles: [[<-- prev]](/spring-core-3) diff --git a/spring-core-4/pom.xml b/spring-core-4/pom.xml index 299debbc3c..e5aee1f81d 100644 --- a/spring-core-4/pom.xml +++ b/spring-core-4/pom.xml @@ -29,6 +29,11 @@ spring-expression ${spring.version} + + org.projectlombok + lombok + ${lombok.version} + com.google.guava guava diff --git a/spring-core-4/src/main/java/com/baeldung/lombok/ApologizeService.java b/spring-core-4/src/main/java/com/baeldung/lombok/ApologizeService.java new file mode 100644 index 0000000000..76c3df8217 --- /dev/null +++ b/spring-core-4/src/main/java/com/baeldung/lombok/ApologizeService.java @@ -0,0 +1,22 @@ +package com.baeldung.lombok; + +import lombok.RequiredArgsConstructor; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +@Component +@RequiredArgsConstructor +public class ApologizeService { + + private final Translator translator; + private final String message; + + @Autowired + public ApologizeService(Translator translator) { + this(translator, "sorry"); + } + + public String apologize() { + return translator.translate(message); + } +} diff --git a/spring-core-4/src/main/java/com/baeldung/lombok/FarewellService.java b/spring-core-4/src/main/java/com/baeldung/lombok/FarewellService.java new file mode 100644 index 0000000000..4e8c4993cb --- /dev/null +++ b/spring-core-4/src/main/java/com/baeldung/lombok/FarewellService.java @@ -0,0 +1,18 @@ +package com.baeldung.lombok; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +@Component +public class FarewellService { + + private final Translator translator; + + public FarewellService(Translator translator) { + this.translator = translator; + } + + public String farewell() { + return translator.translate("bye"); + } +} diff --git a/spring-core-4/src/main/java/com/baeldung/lombok/GreetingService.java b/spring-core-4/src/main/java/com/baeldung/lombok/GreetingService.java new file mode 100644 index 0000000000..0e03e177e1 --- /dev/null +++ b/spring-core-4/src/main/java/com/baeldung/lombok/GreetingService.java @@ -0,0 +1,15 @@ +package com.baeldung.lombok; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +@Component +public class GreetingService { + + @Autowired + private Translator translator; + + public String greet() { + return translator.translate("hello"); + } +} diff --git a/spring-core-4/src/main/java/com/baeldung/lombok/ThankingService.java b/spring-core-4/src/main/java/com/baeldung/lombok/ThankingService.java new file mode 100644 index 0000000000..2e0c398d2d --- /dev/null +++ b/spring-core-4/src/main/java/com/baeldung/lombok/ThankingService.java @@ -0,0 +1,15 @@ +package com.baeldung.lombok; + +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Component; + +@Component +@RequiredArgsConstructor +public class ThankingService { + + private final Translator translator; + + public String thank() { + return translator.translate("thank you"); + } +} diff --git a/spring-core-4/src/main/java/com/baeldung/lombok/Translator.java b/spring-core-4/src/main/java/com/baeldung/lombok/Translator.java new file mode 100644 index 0000000000..2dea20b726 --- /dev/null +++ b/spring-core-4/src/main/java/com/baeldung/lombok/Translator.java @@ -0,0 +1,5 @@ +package com.baeldung.lombok; + +public interface Translator { + String translate(String input); +} diff --git a/spring-core-4/src/main/java/com/baeldung/sampleabstract/BallService.java b/spring-core-4/src/main/java/com/baeldung/sampleabstract/BallService.java new file mode 100644 index 0000000000..0d951aac8b --- /dev/null +++ b/spring-core-4/src/main/java/com/baeldung/sampleabstract/BallService.java @@ -0,0 +1,28 @@ +package com.baeldung.sampleabstract; + +import org.springframework.beans.factory.annotation.Autowired; + +import javax.annotation.PostConstruct; + +public abstract class BallService { + + private RuleRepository ruleRepository; + + private LogRepository logRepository; + + public BallService(RuleRepository ruleRepository) { + this.ruleRepository = ruleRepository; + } + + @Autowired + public final void setLogRepository(LogRepository logRepository) { + this.logRepository = logRepository; + } + + @PostConstruct + public void afterInitialize() { + + System.out.println(ruleRepository.toString()); + System.out.println(logRepository.toString()); + } +} diff --git a/spring-core-4/src/main/java/com/baeldung/sampleabstract/BasketballService.java b/spring-core-4/src/main/java/com/baeldung/sampleabstract/BasketballService.java new file mode 100644 index 0000000000..4d6345b069 --- /dev/null +++ b/spring-core-4/src/main/java/com/baeldung/sampleabstract/BasketballService.java @@ -0,0 +1,13 @@ +package com.baeldung.sampleabstract; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +@Component +public class BasketballService extends BallService { + + @Autowired + public BasketballService(RuleRepository ruleRepository) { + super(ruleRepository); + } +} diff --git a/spring-core-4/src/main/java/com/baeldung/sampleabstract/DemoApp.java b/spring-core-4/src/main/java/com/baeldung/sampleabstract/DemoApp.java new file mode 100644 index 0000000000..5a308b2671 --- /dev/null +++ b/spring-core-4/src/main/java/com/baeldung/sampleabstract/DemoApp.java @@ -0,0 +1,18 @@ +package com.baeldung.sampleabstract; + +import org.springframework.context.ApplicationContext; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; + +@Configuration +@ComponentScan(basePackages = "com.baeldung.sampleabstract") +public class DemoApp { + + + public static void main(String[] args) { + + ApplicationContext applicationContext = new AnnotationConfigApplicationContext(DemoApp.class); + } + +} diff --git a/spring-core-4/src/main/java/com/baeldung/sampleabstract/LogRepository.java b/spring-core-4/src/main/java/com/baeldung/sampleabstract/LogRepository.java new file mode 100644 index 0000000000..84979768b5 --- /dev/null +++ b/spring-core-4/src/main/java/com/baeldung/sampleabstract/LogRepository.java @@ -0,0 +1,12 @@ +package com.baeldung.sampleabstract; + +import org.springframework.stereotype.Component; + +@Component +public class LogRepository { + + @Override + public String toString() { + return "logRepository"; + } +} diff --git a/spring-core-4/src/main/java/com/baeldung/sampleabstract/RuleRepository.java b/spring-core-4/src/main/java/com/baeldung/sampleabstract/RuleRepository.java new file mode 100644 index 0000000000..a1c5b5067f --- /dev/null +++ b/spring-core-4/src/main/java/com/baeldung/sampleabstract/RuleRepository.java @@ -0,0 +1,12 @@ +package com.baeldung.sampleabstract; + +import org.springframework.stereotype.Component; + +@Component +public class RuleRepository { + + @Override + public String toString() { + return "ruleRepository"; + } +} diff --git a/spring-core-4/src/main/java/com/baeldung/startup/AllStrategiesExampleBean.java b/spring-core-4/src/main/java/com/baeldung/startup/AllStrategiesExampleBean.java new file mode 100644 index 0000000000..e08309d474 --- /dev/null +++ b/spring-core-4/src/main/java/com/baeldung/startup/AllStrategiesExampleBean.java @@ -0,0 +1,35 @@ +package com.baeldung.startup; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import org.springframework.beans.factory.InitializingBean; +import org.springframework.context.annotation.Scope; +import org.springframework.stereotype.Component; + +import javax.annotation.PostConstruct; + +@Component +@Scope(value = "prototype") +public class AllStrategiesExampleBean implements InitializingBean { + + private static final Logger LOG = LoggerFactory.getLogger(AllStrategiesExampleBean.class); + + public AllStrategiesExampleBean() { + LOG.info("Constructor"); + } + + @Override + public void afterPropertiesSet() throws Exception { + LOG.info("InitializingBean"); + } + + @PostConstruct + public void postConstruct() { + LOG.info("PostConstruct"); + } + + public void init() { + LOG.info("init-method"); + } +} diff --git a/spring-core-4/src/main/java/com/baeldung/startup/EventListenerExampleBean.java b/spring-core-4/src/main/java/com/baeldung/startup/EventListenerExampleBean.java new file mode 100644 index 0000000000..a76fc6a2b2 --- /dev/null +++ b/spring-core-4/src/main/java/com/baeldung/startup/EventListenerExampleBean.java @@ -0,0 +1,21 @@ +package com.baeldung.startup; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import org.springframework.context.event.ContextRefreshedEvent; +import org.springframework.context.event.EventListener; +import org.springframework.stereotype.Component; + +@Component +public class EventListenerExampleBean { + private static final Logger LOG = LoggerFactory.getLogger(EventListenerExampleBean.class); + + public static int counter; + + @EventListener + public void onApplicationEvent(ContextRefreshedEvent event) { + LOG.info("Increment counter"); + counter++; + } +} diff --git a/spring-core-4/src/main/java/com/baeldung/startup/InitMethodExampleBean.java b/spring-core-4/src/main/java/com/baeldung/startup/InitMethodExampleBean.java new file mode 100644 index 0000000000..a3b12028d1 --- /dev/null +++ b/spring-core-4/src/main/java/com/baeldung/startup/InitMethodExampleBean.java @@ -0,0 +1,24 @@ +package com.baeldung.startup; + +import java.util.Arrays; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Scope; +import org.springframework.core.env.Environment; +import org.springframework.stereotype.Component; + +@Component +@Scope(value = "prototype") +public class InitMethodExampleBean { + + private static final Logger LOG = LoggerFactory.getLogger(InitMethodExampleBean.class); + + @Autowired + private Environment environment; + + public void init() { + LOG.info("Env Default Profiles", Arrays.asList(environment.getDefaultProfiles())); + } +} diff --git a/spring-core-4/src/main/java/com/baeldung/startup/InitializingBeanExampleBean.java b/spring-core-4/src/main/java/com/baeldung/startup/InitializingBeanExampleBean.java new file mode 100644 index 0000000000..c625a172fd --- /dev/null +++ b/spring-core-4/src/main/java/com/baeldung/startup/InitializingBeanExampleBean.java @@ -0,0 +1,26 @@ +package com.baeldung.startup; + +import java.util.Arrays; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.InitializingBean; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Scope; +import org.springframework.core.env.Environment; +import org.springframework.stereotype.Component; + +@Component +@Scope(value = "prototype") +public class InitializingBeanExampleBean implements InitializingBean { + + private static final Logger LOG = LoggerFactory.getLogger(InitializingBeanExampleBean.class); + + @Autowired + private Environment environment; + + @Override + public void afterPropertiesSet() throws Exception { + LOG.info("Env Default Profiles", Arrays.asList(environment.getDefaultProfiles())); + } +} diff --git a/spring-core-4/src/main/java/com/baeldung/startup/InvalidInitExampleBean.java b/spring-core-4/src/main/java/com/baeldung/startup/InvalidInitExampleBean.java new file mode 100644 index 0000000000..d31aee8acd --- /dev/null +++ b/spring-core-4/src/main/java/com/baeldung/startup/InvalidInitExampleBean.java @@ -0,0 +1,18 @@ +package com.baeldung.startup; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Scope; +import org.springframework.core.env.Environment; +import org.springframework.stereotype.Component; + +@Component +@Scope("prototype") +public class InvalidInitExampleBean { + + @Autowired + private Environment environment; + + public InvalidInitExampleBean() { + environment.getActiveProfiles(); + } +} diff --git a/spring-core-4/src/main/java/com/baeldung/startup/LogicInConstructorExampleBean.java b/spring-core-4/src/main/java/com/baeldung/startup/LogicInConstructorExampleBean.java new file mode 100644 index 0000000000..ade7573bbe --- /dev/null +++ b/spring-core-4/src/main/java/com/baeldung/startup/LogicInConstructorExampleBean.java @@ -0,0 +1,22 @@ +package com.baeldung.startup; + +import java.util.Arrays; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Scope; +import org.springframework.core.env.Environment; +import org.springframework.stereotype.Component; + +@Component +@Scope(value = "prototype") +public class LogicInConstructorExampleBean { + + private static final Logger LOG = LoggerFactory.getLogger(LogicInConstructorExampleBean.class); + + @Autowired + public LogicInConstructorExampleBean(Environment environment) { + LOG.info("Env Default Profiles", Arrays.asList(environment.getDefaultProfiles())); + } +} diff --git a/spring-core-4/src/main/java/com/baeldung/startup/PostConstructExampleBean.java b/spring-core-4/src/main/java/com/baeldung/startup/PostConstructExampleBean.java new file mode 100644 index 0000000000..1001043d86 --- /dev/null +++ b/spring-core-4/src/main/java/com/baeldung/startup/PostConstructExampleBean.java @@ -0,0 +1,27 @@ +package com.baeldung.startup; + +import java.util.Arrays; + +import javax.annotation.PostConstruct; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Scope; +import org.springframework.core.env.Environment; +import org.springframework.stereotype.Component; + +@Component +@Scope(value = "prototype") +public class PostConstructExampleBean { + + private static final Logger LOG = LoggerFactory.getLogger(PostConstructExampleBean.class); + + @Autowired + private Environment environment; + + @PostConstruct + public void init() { + LOG.info("Env Default Profiles", Arrays.asList(environment.getDefaultProfiles())); + } +} diff --git a/spring-core-4/src/main/java/com/baeldung/startup/SpringStartupConfig.java b/spring-core-4/src/main/java/com/baeldung/startup/SpringStartupConfig.java new file mode 100644 index 0000000000..ad6492dadc --- /dev/null +++ b/spring-core-4/src/main/java/com/baeldung/startup/SpringStartupConfig.java @@ -0,0 +1,9 @@ +package com.baeldung.startup; + +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; + +@Configuration +@ComponentScan("com.baeldung.startup") +public class SpringStartupConfig { +} \ No newline at end of file diff --git a/spring-core-4/src/main/java/com/baeldung/startup/StartupApplicationListenerExample.java b/spring-core-4/src/main/java/com/baeldung/startup/StartupApplicationListenerExample.java new file mode 100644 index 0000000000..2cc5e6abcb --- /dev/null +++ b/spring-core-4/src/main/java/com/baeldung/startup/StartupApplicationListenerExample.java @@ -0,0 +1,22 @@ +package com.baeldung.startup; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import org.springframework.context.ApplicationListener; +import org.springframework.context.event.ContextRefreshedEvent; +import org.springframework.stereotype.Component; + +@Component +public class StartupApplicationListenerExample implements ApplicationListener { + + private static final Logger LOG = LoggerFactory.getLogger(StartupApplicationListenerExample.class); + + public static int counter; + + @Override + public void onApplicationEvent(ContextRefreshedEvent event) { + LOG.info("Increment counter"); + counter++; + } +} diff --git a/spring-core-4/src/main/resources/startupConfig.xml b/spring-core-4/src/main/resources/startupConfig.xml new file mode 100644 index 0000000000..d42e0f6c2b --- /dev/null +++ b/spring-core-4/src/main/resources/startupConfig.xml @@ -0,0 +1,16 @@ + + + + + + + + \ No newline at end of file diff --git a/spring-core-4/src/test/java/com/baeldung/lombok/ApologizeServiceAutowiringIntegrationTest.java b/spring-core-4/src/test/java/com/baeldung/lombok/ApologizeServiceAutowiringIntegrationTest.java new file mode 100644 index 0000000000..a49dd84f11 --- /dev/null +++ b/spring-core-4/src/test/java/com/baeldung/lombok/ApologizeServiceAutowiringIntegrationTest.java @@ -0,0 +1,33 @@ +package com.baeldung.lombok; + +import org.junit.Test; +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 static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.when; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration( + loader = AnnotationConfigContextLoader.class, + classes = TestConfig.class) +public class ApologizeServiceAutowiringIntegrationTest { + + private final static String TRANSLATED = "TRANSLATED"; + + @Autowired + private ApologizeService apologizeService; + + @Autowired + private Translator translator; + + @Test + public void apologizeWithTranslatedMessage() { + when(translator.translate("sorry")).thenReturn(TRANSLATED); + assertEquals(TRANSLATED, apologizeService.apologize()); + } + +} \ No newline at end of file diff --git a/spring-core-4/src/test/java/com/baeldung/lombok/ApologizeServiceIntegrationTest.java b/spring-core-4/src/test/java/com/baeldung/lombok/ApologizeServiceIntegrationTest.java new file mode 100644 index 0000000000..77f0c94299 --- /dev/null +++ b/spring-core-4/src/test/java/com/baeldung/lombok/ApologizeServiceIntegrationTest.java @@ -0,0 +1,21 @@ +package com.baeldung.lombok; + +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public class ApologizeServiceIntegrationTest { + + private final static String MESSAGE = "MESSAGE"; + private final static String TRANSLATED = "TRANSLATED"; + + @Test + public void apologizeWithCustomTranslatedMessage() { + Translator translator = mock(Translator.class); + ApologizeService apologizeService = new ApologizeService(translator, MESSAGE); + when(translator.translate(MESSAGE)).thenReturn(TRANSLATED); + assertEquals(TRANSLATED, apologizeService.apologize()); + } +} diff --git a/spring-core-4/src/test/java/com/baeldung/lombok/FarewellAutowiringIntegrationTest.java b/spring-core-4/src/test/java/com/baeldung/lombok/FarewellAutowiringIntegrationTest.java new file mode 100644 index 0000000000..ec0793bd2e --- /dev/null +++ b/spring-core-4/src/test/java/com/baeldung/lombok/FarewellAutowiringIntegrationTest.java @@ -0,0 +1,31 @@ +package com.baeldung.lombok; + +import org.junit.Test; +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 static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.when; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration( + loader = AnnotationConfigContextLoader.class, + classes = TestConfig.class) +public class FarewellAutowiringIntegrationTest { + + @Autowired + private FarewellService farewellService; + + @Autowired + private Translator translator; + + @Test + public void sayByeWithTranslatedMessage() { + String translated = "translated"; + when(translator.translate("bye")).thenReturn(translated); + assertEquals(translated, farewellService.farewell()); + } +} diff --git a/spring-core-4/src/test/java/com/baeldung/lombok/FarewellServiceIntegrationTest.java b/spring-core-4/src/test/java/com/baeldung/lombok/FarewellServiceIntegrationTest.java new file mode 100644 index 0000000000..38959a511f --- /dev/null +++ b/spring-core-4/src/test/java/com/baeldung/lombok/FarewellServiceIntegrationTest.java @@ -0,0 +1,20 @@ +package com.baeldung.lombok; + +import org.junit.Test; + +import static org.junit.Assert.*; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public class FarewellServiceIntegrationTest { + + private final static String TRANSLATED = "TRANSLATED"; + + @Test + public void sayByeWithTranslatedMessage() { + Translator translator = mock(Translator.class); + when(translator.translate("bye")).thenReturn(TRANSLATED); + FarewellService farewellService = new FarewellService(translator); + assertEquals(TRANSLATED, farewellService.farewell()); + } +} \ No newline at end of file diff --git a/spring-core-4/src/test/java/com/baeldung/lombok/GreetingServiceIntegrationTest.java b/spring-core-4/src/test/java/com/baeldung/lombok/GreetingServiceIntegrationTest.java new file mode 100644 index 0000000000..0516b5eb56 --- /dev/null +++ b/spring-core-4/src/test/java/com/baeldung/lombok/GreetingServiceIntegrationTest.java @@ -0,0 +1,37 @@ +package com.baeldung.lombok; + +import org.junit.Test; +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 static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.when; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration( + loader = AnnotationConfigContextLoader.class, + classes = TestConfig.class) +public class GreetingServiceIntegrationTest { + + @Autowired + private GreetingService greetingService; + + @Autowired + private Translator translator; + + @Test + public void greetWithTranslatedMessage() { + String translated = "translated"; + when(translator.translate("hello")).thenReturn(translated); + assertEquals(translated, greetingService.greet()); + } + + @Test(expected = NullPointerException.class) + public void throwWhenInstantiated() { + GreetingService greetingService = new GreetingService(); + greetingService.greet(); + } +} \ No newline at end of file diff --git a/spring-core-4/src/test/java/com/baeldung/lombok/TestConfig.java b/spring-core-4/src/test/java/com/baeldung/lombok/TestConfig.java new file mode 100644 index 0000000000..3278a8188f --- /dev/null +++ b/spring-core-4/src/test/java/com/baeldung/lombok/TestConfig.java @@ -0,0 +1,17 @@ +package com.baeldung.lombok; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; + +import static org.mockito.Mockito.mock; + +@Configuration +@ComponentScan("com.baeldung.lombok") +class TestConfig { + + @Bean + public Translator mockTranslator() { + return mock(Translator.class); + } +} diff --git a/spring-core-4/src/test/java/com/baeldung/lombok/ThankingServiceAutowiringIntegrationTest.java b/spring-core-4/src/test/java/com/baeldung/lombok/ThankingServiceAutowiringIntegrationTest.java new file mode 100644 index 0000000000..fb9abbad46 --- /dev/null +++ b/spring-core-4/src/test/java/com/baeldung/lombok/ThankingServiceAutowiringIntegrationTest.java @@ -0,0 +1,31 @@ +package com.baeldung.lombok; + +import org.junit.Test; +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 static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.when; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration( + loader = AnnotationConfigContextLoader.class, + classes = TestConfig.class) +public class ThankingServiceAutowiringIntegrationTest { + + @Autowired + private ThankingService thankingService; + + @Autowired + private Translator translator; + + @Test + public void thankWithTranslatedMessage() { + String translated = "translated"; + when(translator.translate("thank you")).thenReturn(translated); + assertEquals(translated, thankingService.thank()); + } +} \ No newline at end of file diff --git a/spring-core-4/src/test/java/com/baeldung/lombok/ThankingServiceIntegrationTest.java b/spring-core-4/src/test/java/com/baeldung/lombok/ThankingServiceIntegrationTest.java new file mode 100644 index 0000000000..680f926717 --- /dev/null +++ b/spring-core-4/src/test/java/com/baeldung/lombok/ThankingServiceIntegrationTest.java @@ -0,0 +1,20 @@ +package com.baeldung.lombok; + +import org.junit.Test; + +import static org.junit.Assert.*; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public class ThankingServiceIntegrationTest { + + private final static String TRANSLATED = "TRANSLATED"; + + @Test + public void thankWithTranslatedMessage() { + Translator translator = mock(Translator.class); + when(translator.translate("thank you")).thenReturn(TRANSLATED); + ThankingService thankingService = new ThankingService(translator); + assertEquals(TRANSLATED, thankingService.thank()); + } +} \ No newline at end of file diff --git a/spring-core-4/src/test/java/com/baeldung/startup/SpringStartupIntegrationTest.java b/spring-core-4/src/test/java/com/baeldung/startup/SpringStartupIntegrationTest.java new file mode 100644 index 0000000000..b58c093c31 --- /dev/null +++ b/spring-core-4/src/test/java/com/baeldung/startup/SpringStartupIntegrationTest.java @@ -0,0 +1,44 @@ +package com.baeldung.startup; + +import org.assertj.core.api.Assertions; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.BeanCreationException; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.ApplicationContext; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.support.AnnotationConfigContextLoader; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(classes = { SpringStartupConfig.class }, loader = AnnotationConfigContextLoader.class) +public class SpringStartupIntegrationTest { + + @Autowired + private ApplicationContext ctx; + + @Test(expected = BeanCreationException.class) + public void whenInstantiating_shouldThrowBCE() throws Exception { + ctx.getBean(InvalidInitExampleBean.class); + } + + @Test + public void whenPostConstruct_shouldLogEnv() throws Exception { + ctx.getBean(PostConstructExampleBean.class); + } + + @Test + public void whenConstructorInjection_shouldLogEnv() throws Exception { + ctx.getBean(LogicInConstructorExampleBean.class); + } + + @Test + public void whenInitializingBean_shouldLogEnv() throws Exception { + ctx.getBean(InitializingBeanExampleBean.class); + } + + @Test + public void whenApplicationListener_shouldRunOnce() throws Exception { + Assertions.assertThat(StartupApplicationListenerExample.counter).isEqualTo(1); + } +} \ No newline at end of file diff --git a/spring-core-4/src/test/java/com/baeldung/startup/SpringStartupXMLConfigIntegrationTest.java b/spring-core-4/src/test/java/com/baeldung/startup/SpringStartupXMLConfigIntegrationTest.java new file mode 100644 index 0000000000..3dfd4835df --- /dev/null +++ b/spring-core-4/src/test/java/com/baeldung/startup/SpringStartupXMLConfigIntegrationTest.java @@ -0,0 +1,26 @@ +package com.baeldung.startup; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.ApplicationContext; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration("classpath:startupConfig.xml") +public class SpringStartupXMLConfigIntegrationTest { + + @Autowired + private ApplicationContext ctx; + + @Test + public void whenPostConstruct_shouldLogEnv() throws Exception { + ctx.getBean(InitMethodExampleBean.class); + } + + @Test + public void whenAllStrategies_shouldLogOrder() throws Exception { + ctx.getBean(AllStrategiesExampleBean.class); + } +}