Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default #20

Open
wants to merge 4 commits into
base: default
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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();}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package guru.springframework.services;

public interface CongratService {

String sayCongrats();
}
Original file line number Diff line number Diff line change
@@ -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.";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*/
@Service
@Primary
@Profile("de")
@Profile({"de", "default"})
public class PrimaryGermanGreetingService implements GreetingService {
@Override
public String sayGreeting() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*/
@Service
@Primary
@Profile({"en", "default"})
@Profile("en")
public class PrimaryGreetingService implements GreetingService {

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -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());
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}