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/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 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 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..5ce44945 --- /dev/null +++ b/src/test/java/guru/springframework/controllers/MyControllerTest.java @@ -0,0 +1,34 @@ +package guru.springframework.controllers; +import org.junit.Test; +import org.junit.runner.RunWith; + + +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; + +import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertEquals; + +@RunWith(SpringRunner.class) +@SpringBootTest +@ActiveProfiles(profiles = "default" ) +public class MyControllerTest { + + @MockBean + 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(null, result); + } +}