From 5548022a2bd56083bf83987edefff8b0a9d1d2e6 Mon Sep 17 00:00:00 2001 From: Piotr Sliwa Date: Wed, 1 Feb 2017 07:53:17 +0100 Subject: [PATCH] BAEL-628: Added sources for "Constructor Injection in Spring with Lombok" article. (#1064) * BAEL-628: Added sources for "Constructor Injection in Spring with Lombok" article. * BAEL-628: Removed unneccessary @Autowired + Apologizer example. --- spring-core/README.md | 1 + spring-core/pom.xml | 6 +++ .../java/com/baeldung/lombok/Apologizer.java | 22 +++++++++++ .../java/com/baeldung/lombok/Fareweller.java | 18 +++++++++ .../java/com/baeldung/lombok/Greeter.java | 15 ++++++++ .../java/com/baeldung/lombok/Thanker.java | 16 ++++++++ .../java/com/baeldung/lombok/Translator.java | 5 +++ .../lombok/ApologizerAutowiringTest.java | 33 +++++++++++++++++ .../com/baeldung/lombok/ApologizerTest.java | 21 +++++++++++ .../lombok/FarewellAutowiringTest.java | 31 ++++++++++++++++ .../com/baeldung/lombok/FarewellerTest.java | 20 ++++++++++ .../java/com/baeldung/lombok/GreeterTest.java | 37 +++++++++++++++++++ .../java/com/baeldung/lombok/TestConfig.java | 17 +++++++++ .../lombok/ThankerAutowiringTest.java | 31 ++++++++++++++++ .../java/com/baeldung/lombok/ThankerTest.java | 20 ++++++++++ 15 files changed, 293 insertions(+) create mode 100644 spring-core/src/main/java/com/baeldung/lombok/Apologizer.java create mode 100644 spring-core/src/main/java/com/baeldung/lombok/Fareweller.java create mode 100644 spring-core/src/main/java/com/baeldung/lombok/Greeter.java create mode 100644 spring-core/src/main/java/com/baeldung/lombok/Thanker.java create mode 100644 spring-core/src/main/java/com/baeldung/lombok/Translator.java create mode 100644 spring-core/src/test/java/com/baeldung/lombok/ApologizerAutowiringTest.java create mode 100644 spring-core/src/test/java/com/baeldung/lombok/ApologizerTest.java create mode 100644 spring-core/src/test/java/com/baeldung/lombok/FarewellAutowiringTest.java create mode 100644 spring-core/src/test/java/com/baeldung/lombok/FarewellerTest.java create mode 100644 spring-core/src/test/java/com/baeldung/lombok/GreeterTest.java create mode 100644 spring-core/src/test/java/com/baeldung/lombok/TestConfig.java create mode 100644 spring-core/src/test/java/com/baeldung/lombok/ThankerAutowiringTest.java create mode 100644 spring-core/src/test/java/com/baeldung/lombok/ThankerTest.java diff --git a/spring-core/README.md b/spring-core/README.md index 30ba30f889..f6aaaf44a0 100644 --- a/spring-core/README.md +++ b/spring-core/README.md @@ -3,3 +3,4 @@ - [Exploring the Spring BeanFactory API](http://www.baeldung.com/spring-beanfactory) - [How to use the Spring FactoryBean?](http://www.baeldung.com/spring-factorybean) - [Constructor Dependency Injection in Spring](http://www.baeldung.com/constructor-injection-in-spring) +- [Constructor Injection in Spring with Lombok](http://inprogress.baeldung.com/constructor-injection-in-spring-with-lombok) diff --git a/spring-core/pom.xml b/spring-core/pom.xml index bf05f6a6f0..3b423104d7 100644 --- a/spring-core/pom.xml +++ b/spring-core/pom.xml @@ -52,6 +52,11 @@ guava ${guava.version} + + org.projectlombok + lombok + ${lombok.version} + @@ -131,6 +136,7 @@ 4.12 20.0 2.6 + 1.16.12 diff --git a/spring-core/src/main/java/com/baeldung/lombok/Apologizer.java b/spring-core/src/main/java/com/baeldung/lombok/Apologizer.java new file mode 100644 index 0000000000..ddce9cdc52 --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/lombok/Apologizer.java @@ -0,0 +1,22 @@ +package com.baeldung.lombok; + +import lombok.AllArgsConstructor; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +@Component +@AllArgsConstructor +public class Apologizer { + + private final Translator translator; + private final String message; + + @Autowired + public Apologizer(Translator translator) { + this(translator, "sorry"); + } + + public String apologize() { + return translator.translate(message); + } +} diff --git a/spring-core/src/main/java/com/baeldung/lombok/Fareweller.java b/spring-core/src/main/java/com/baeldung/lombok/Fareweller.java new file mode 100644 index 0000000000..b10ebb72b9 --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/lombok/Fareweller.java @@ -0,0 +1,18 @@ +package com.baeldung.lombok; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +@Component +public class Fareweller { + + private final Translator translator; + + public Fareweller(Translator translator) { + this.translator = translator; + } + + public String farewell() { + return translator.translate("bye"); + } +} diff --git a/spring-core/src/main/java/com/baeldung/lombok/Greeter.java b/spring-core/src/main/java/com/baeldung/lombok/Greeter.java new file mode 100644 index 0000000000..ddbc024ce7 --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/lombok/Greeter.java @@ -0,0 +1,15 @@ +package com.baeldung.lombok; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +@Component +public class Greeter { + + @Autowired + private Translator translator; + + public String greet() { + return translator.translate("hello"); + } +} diff --git a/spring-core/src/main/java/com/baeldung/lombok/Thanker.java b/spring-core/src/main/java/com/baeldung/lombok/Thanker.java new file mode 100644 index 0000000000..784100c258 --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/lombok/Thanker.java @@ -0,0 +1,16 @@ +package com.baeldung.lombok; + +import lombok.AllArgsConstructor; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +@Component +@AllArgsConstructor +public class Thanker { + + private final Translator translator; + + public String thank() { + return translator.translate("thank you"); + } +} diff --git a/spring-core/src/main/java/com/baeldung/lombok/Translator.java b/spring-core/src/main/java/com/baeldung/lombok/Translator.java new file mode 100644 index 0000000000..2dea20b726 --- /dev/null +++ b/spring-core/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/src/test/java/com/baeldung/lombok/ApologizerAutowiringTest.java b/spring-core/src/test/java/com/baeldung/lombok/ApologizerAutowiringTest.java new file mode 100644 index 0000000000..3e7c775f91 --- /dev/null +++ b/spring-core/src/test/java/com/baeldung/lombok/ApologizerAutowiringTest.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 ApologizerAutowiringTest { + + private final static String TRANSLATED = "TRANSLATED"; + + @Autowired + private Apologizer apologizer; + + @Autowired + private Translator translator; + + @Test + public void apologizeWithTranslatedMessage() { + when(translator.translate("sorry")).thenReturn(TRANSLATED); + assertEquals(TRANSLATED, apologizer.apologize()); + } + +} \ No newline at end of file diff --git a/spring-core/src/test/java/com/baeldung/lombok/ApologizerTest.java b/spring-core/src/test/java/com/baeldung/lombok/ApologizerTest.java new file mode 100644 index 0000000000..28bd08c89f --- /dev/null +++ b/spring-core/src/test/java/com/baeldung/lombok/ApologizerTest.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 ApologizerTest { + + private final static String MESSAGE = "MESSAGE"; + private final static String TRANSLATED = "TRANSLATED"; + + @Test + public void apologizeWithCustomTranslatedMessage() { + Translator translator = mock(Translator.class); + Apologizer apologizer = new Apologizer(translator, MESSAGE); + when(translator.translate(MESSAGE)).thenReturn(TRANSLATED); + assertEquals(TRANSLATED, apologizer.apologize()); + } +} diff --git a/spring-core/src/test/java/com/baeldung/lombok/FarewellAutowiringTest.java b/spring-core/src/test/java/com/baeldung/lombok/FarewellAutowiringTest.java new file mode 100644 index 0000000000..d55d44fb3e --- /dev/null +++ b/spring-core/src/test/java/com/baeldung/lombok/FarewellAutowiringTest.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 FarewellAutowiringTest { + + @Autowired + private Fareweller fareweller; + + @Autowired + private Translator translator; + + @Test + public void sayByeWithTranslatedMessage() { + String translated = "translated"; + when(translator.translate("bye")).thenReturn(translated); + assertEquals(translated, fareweller.farewell()); + } +} diff --git a/spring-core/src/test/java/com/baeldung/lombok/FarewellerTest.java b/spring-core/src/test/java/com/baeldung/lombok/FarewellerTest.java new file mode 100644 index 0000000000..a02ad1d8ac --- /dev/null +++ b/spring-core/src/test/java/com/baeldung/lombok/FarewellerTest.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 FarewellerTest { + + private final static String TRANSLATED = "TRANSLATED"; + + @Test + public void sayByeWithTranslatedMessage() { + Translator translator = mock(Translator.class); + when(translator.translate("bye")).thenReturn(TRANSLATED); + Fareweller fareweller = new Fareweller(translator); + assertEquals(TRANSLATED, fareweller.farewell()); + } +} \ No newline at end of file diff --git a/spring-core/src/test/java/com/baeldung/lombok/GreeterTest.java b/spring-core/src/test/java/com/baeldung/lombok/GreeterTest.java new file mode 100644 index 0000000000..0f66eaf301 --- /dev/null +++ b/spring-core/src/test/java/com/baeldung/lombok/GreeterTest.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 GreeterTest { + + @Autowired + private Greeter greeter; + + @Autowired + private Translator translator; + + @Test + public void greetWithTranslatedMessage() { + String translated = "translated"; + when(translator.translate("hello")).thenReturn(translated); + assertEquals(translated, greeter.greet()); + } + + @Test(expected = NullPointerException.class) + public void throwWhenInstantiated() { + Greeter greeter = new Greeter(); + greeter.greet(); + } +} \ No newline at end of file diff --git a/spring-core/src/test/java/com/baeldung/lombok/TestConfig.java b/spring-core/src/test/java/com/baeldung/lombok/TestConfig.java new file mode 100644 index 0000000000..3278a8188f --- /dev/null +++ b/spring-core/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/src/test/java/com/baeldung/lombok/ThankerAutowiringTest.java b/spring-core/src/test/java/com/baeldung/lombok/ThankerAutowiringTest.java new file mode 100644 index 0000000000..59aded5831 --- /dev/null +++ b/spring-core/src/test/java/com/baeldung/lombok/ThankerAutowiringTest.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 ThankerAutowiringTest { + + @Autowired + private Thanker thanker; + + @Autowired + private Translator translator; + + @Test + public void thankWithTranslatedMessage() { + String translated = "translated"; + when(translator.translate("thank you")).thenReturn(translated); + assertEquals(translated, thanker.thank()); + } +} \ No newline at end of file diff --git a/spring-core/src/test/java/com/baeldung/lombok/ThankerTest.java b/spring-core/src/test/java/com/baeldung/lombok/ThankerTest.java new file mode 100644 index 0000000000..466762fa50 --- /dev/null +++ b/spring-core/src/test/java/com/baeldung/lombok/ThankerTest.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 ThankerTest { + + private final static String TRANSLATED = "TRANSLATED"; + + @Test + public void thankWithTranslatedMessage() { + Translator translator = mock(Translator.class); + when(translator.translate("thank you")).thenReturn(TRANSLATED); + Thanker thanker = new Thanker(translator); + assertEquals(TRANSLATED, thanker.thank()); + } +} \ No newline at end of file