From bc565cc46e95c76831c841cdc011813899632f30 Mon Sep 17 00:00:00 2001 From: helen Date: Sat, 16 Feb 2019 21:56:03 -0500 Subject: [PATCH 1/4] use mockito to test with primary bean and default profile --- .../controllers/MyControllerTest.java | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 src/test/java/guru/springframework/controllers/MyControllerTest.java diff --git a/src/test/java/guru/springframework/controllers/MyControllerTest.java b/src/test/java/guru/springframework/controllers/MyControllerTest.java new file mode 100644 index 00000000..88be38fa --- /dev/null +++ b/src/test/java/guru/springframework/controllers/MyControllerTest.java @@ -0,0 +1,33 @@ +package guru.springframework.controllers; +import org.junit.Test; +import org.junit.runner.RunWith; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.junit4.SpringRunner; + +import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertEquals; + +@RunWith(SpringRunner.class) +@SpringBootTest +@ActiveProfiles(profiles = "default" ) +public class MyControllerTest { + + @Autowired + private MyController mycontroller; + + + @Test + public void testEnGreeting() throws Exception{ + String result = mycontroller.hello(); + assertNotEquals("Hello - Primary Greeting service", result); + } + + @Test + public void testDeGreeting() throws Exception{ + String result = mycontroller.hello(); + assertEquals("Primärer Grußdienst", result); + } +} From 82fa2b6c6bf5b0d295b6761768dad96aba2b465c Mon Sep 17 00:00:00 2001 From: helen Date: Sat, 16 Feb 2019 21:57:38 -0500 Subject: [PATCH 2/4] set german as default --- .../springframework/services/PrimaryGermanGreetingService.java | 2 +- .../guru/springframework/services/PrimaryGreetingService.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/guru/springframework/services/PrimaryGermanGreetingService.java b/src/main/java/guru/springframework/services/PrimaryGermanGreetingService.java index acdadade..f984ab8d 100644 --- a/src/main/java/guru/springframework/services/PrimaryGermanGreetingService.java +++ b/src/main/java/guru/springframework/services/PrimaryGermanGreetingService.java @@ -9,7 +9,7 @@ */ @Service @Primary -@Profile("de") +@Profile({"de", "default"}) public class PrimaryGermanGreetingService implements GreetingService { @Override public String sayGreeting() { diff --git a/src/main/java/guru/springframework/services/PrimaryGreetingService.java b/src/main/java/guru/springframework/services/PrimaryGreetingService.java index 233034a2..714f416a 100644 --- a/src/main/java/guru/springframework/services/PrimaryGreetingService.java +++ b/src/main/java/guru/springframework/services/PrimaryGreetingService.java @@ -9,7 +9,7 @@ */ @Service @Primary -@Profile({"en", "default"}) +@Profile("en") public class PrimaryGreetingService implements GreetingService { @Override From 9e9df41becda78affd9074326917aafbdcd088bf Mon Sep 17 00:00:00 2001 From: helen Date: Sun, 17 Feb 2019 16:42:36 -0500 Subject: [PATCH 3/4] set English as default --- .../guru/springframework/controllers/MyControllerTest.java | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/test/java/guru/springframework/controllers/MyControllerTest.java b/src/test/java/guru/springframework/controllers/MyControllerTest.java index 88be38fa..5ce44945 100644 --- a/src/test/java/guru/springframework/controllers/MyControllerTest.java +++ b/src/test/java/guru/springframework/controllers/MyControllerTest.java @@ -2,8 +2,9 @@ import org.junit.Test; import org.junit.runner.RunWith; -import org.springframework.beans.factory.annotation.Autowired; + import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.junit4.SpringRunner; @@ -15,7 +16,7 @@ @ActiveProfiles(profiles = "default" ) public class MyControllerTest { - @Autowired + @MockBean private MyController mycontroller; @@ -28,6 +29,6 @@ public void testEnGreeting() throws Exception{ @Test public void testDeGreeting() throws Exception{ String result = mycontroller.hello(); - assertEquals("Primärer Grußdienst", result); + assertEquals(null, result); } } From 0830927a569498209ade8d2b80de19bbc133bdb3 Mon Sep 17 00:00:00 2001 From: helen Date: Thu, 21 Feb 2019 22:20:10 -0500 Subject: [PATCH 4/4] Two constructors, test which one is used. --- .../ConstructorInjectedController.java | 14 +++++++++++ .../services/CongratService.java | 6 +++++ .../services/ConstructorCongratsService.java | 12 +++++++++ .../ConstructorInjectedControllerTest.java | 25 +++++++++++++------ 4 files changed, 49 insertions(+), 8 deletions(-) create mode 100644 src/main/java/guru/springframework/services/CongratService.java create mode 100644 src/main/java/guru/springframework/services/ConstructorCongratsService.java diff --git a/src/main/java/guru/springframework/controllers/ConstructorInjectedController.java b/src/main/java/guru/springframework/controllers/ConstructorInjectedController.java index d21186fb..35b5538b 100644 --- a/src/main/java/guru/springframework/controllers/ConstructorInjectedController.java +++ b/src/main/java/guru/springframework/controllers/ConstructorInjectedController.java @@ -1,6 +1,8 @@ package guru.springframework.controllers; import guru.springframework.services.GreetingService; +import guru.springframework.services.CongratService; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Controller; @@ -12,11 +14,23 @@ public class ConstructorInjectedController { private GreetingService greetingService; + private CongratService congratService; + + + //@Autowired + //IntelliJ says only one constructor can have autowired annotation public ConstructorInjectedController(@Qualifier("constructorGreetingService") GreetingService greetingService) { this.greetingService = greetingService; } + @Autowired + public ConstructorInjectedController (CongratService congratService){ + this.congratService = congratService; + } + public String sayHello(){ return greetingService.sayGreeting(); } + + public String sayCongrat() {return congratService.sayCongrats();} } diff --git a/src/main/java/guru/springframework/services/CongratService.java b/src/main/java/guru/springframework/services/CongratService.java new file mode 100644 index 00000000..1b6605d2 --- /dev/null +++ b/src/main/java/guru/springframework/services/CongratService.java @@ -0,0 +1,6 @@ +package guru.springframework.services; + +public interface CongratService { + + String sayCongrats(); +} diff --git a/src/main/java/guru/springframework/services/ConstructorCongratsService.java b/src/main/java/guru/springframework/services/ConstructorCongratsService.java new file mode 100644 index 00000000..00e21235 --- /dev/null +++ b/src/main/java/guru/springframework/services/ConstructorCongratsService.java @@ -0,0 +1,12 @@ +package guru.springframework.services; + +import org.springframework.stereotype.Service; + +@Service +public class ConstructorCongratsService implements CongratService { + + public String sayCongrats(){ + return "I am constructor injected - congrats."; + } + +} diff --git a/src/test/java/guru/springframework/controllers/ConstructorInjectedControllerTest.java b/src/test/java/guru/springframework/controllers/ConstructorInjectedControllerTest.java index bbf36c6e..25e98203 100644 --- a/src/test/java/guru/springframework/controllers/ConstructorInjectedControllerTest.java +++ b/src/test/java/guru/springframework/controllers/ConstructorInjectedControllerTest.java @@ -1,24 +1,33 @@ package guru.springframework.controllers; -import guru.springframework.services.GreetingServiceImpl; -import org.junit.Before; -import org.junit.Test; +import org.junit.Test; import static org.junit.Assert.assertEquals; +import org.junit.runner.RunWith; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.beans.factory.annotation.Autowired; + /** * Created by jt on 5/24/17. */ +@RunWith(SpringRunner.class) +@SpringBootTest public class ConstructorInjectedControllerTest { + + @Autowired private ConstructorInjectedController constructorInjectedController; - @Before - public void setUp() throws Exception { - this.constructorInjectedController = new ConstructorInjectedController(new GreetingServiceImpl()); + + //The GreetingService is not expected to be injected. + @Test(expected = NullPointerException.class) + public void testGreeting() throws Exception{ + constructorInjectedController.sayHello(); } @Test - public void testGreeting() throws Exception { - assertEquals(GreetingServiceImpl.HELLO_GURUS, constructorInjectedController.sayHello()); + public void testCongrats() throws Exception { + assertEquals("I am constructor injected - congrats." , constructorInjectedController.sayCongrat()); } } \ No newline at end of file