Skip to content

Commit 0484b51

Browse files
author
Michiel Van Huyck
committed
springframeworkguru#1.1 : Testing with configBeans
!!!!!! going to autowired
1 parent 368c298 commit 0484b51

File tree

7 files changed

+124
-0
lines changed

7 files changed

+124
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package org.springframework.samples.petclinic.sfg;
2+
3+
import org.springframework.stereotype.Service;
4+
5+
/**
6+
* Created by jt on 2019-02-16.
7+
*/
8+
@Service
9+
public class HearingInterpreter {
10+
11+
private final WordProducer wordProducer;
12+
13+
public HearingInterpreter(WordProducer wordProducer) {
14+
this.wordProducer = wordProducer;
15+
}
16+
17+
public String whatIheard(){
18+
String word = wordProducer.getWord();
19+
20+
System.out.println(word);
21+
22+
return word;
23+
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package org.springframework.samples.petclinic.sfg;
2+
3+
import org.springframework.context.annotation.Primary;
4+
import org.springframework.stereotype.Component;
5+
6+
/**
7+
* Created by jt on 2019-02-16.
8+
*/
9+
@Component
10+
@Primary
11+
public class LaurelWordProducer implements WordProducer {
12+
@Override
13+
public String getWord() {
14+
return "Laurel";
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package org.springframework.samples.petclinic.sfg;
2+
3+
/**
4+
* Created by jt on 2019-02-16.
5+
*/
6+
public interface WordProducer {
7+
8+
String getWord();
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package org.springframework.samples.petclinic.sfg;
2+
3+
import org.springframework.stereotype.Component;
4+
5+
/**
6+
* Created by jt on 2019-02-16.
7+
*/
8+
@Component
9+
public class YannyWordProducer implements WordProducer {
10+
@Override
11+
public String getWord() {
12+
return "Yanny";
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package org.springframework.samples.petclinic.sfg;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.context.annotation.Configuration;
5+
6+
/**
7+
* Created by jt on 2019-02-16.
8+
*/
9+
@Configuration
10+
public class BaseConfig {
11+
12+
@Bean
13+
HearingInterpreter hearingInterpreter(WordProducer wordProducer){
14+
return new HearingInterpreter(wordProducer);
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package org.springframework.samples.petclinic.sfg;
2+
3+
import org.junit.Before;
4+
import org.junit.Test;
5+
import org.junit.runner.RunWith;
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.test.context.ContextConfiguration;
8+
import org.springframework.test.context.junit4.SpringRunner;
9+
10+
import static org.junit.Assert.assertEquals;
11+
12+
13+
public class HearingInterpreterTest {
14+
// USES BEAN HEARINGINTERPRETER FROM BASE CONFIG CLASS
15+
HearingInterpreter hearingInterpreter;
16+
17+
// USES BEAN LAURELWORDPRODUCER FROM LAURALCONFIG.CLASS
18+
@Before
19+
public void setUp() throws Exception{
20+
hearingInterpreter = new HearingInterpreter(new LaurelWordProducer());
21+
}
22+
@Test
23+
public void whatIheard() {
24+
String word = hearingInterpreter.whatIheard();
25+
26+
27+
assertEquals("Laurel", word);
28+
}
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package org.springframework.samples.petclinic.sfg;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.context.annotation.Configuration;
5+
6+
/**
7+
* Created by jt on 2019-02-16.
8+
*/
9+
@Configuration
10+
public class LaurelConfig {
11+
12+
@Bean
13+
LaurelWordProducer laurelWordProducer(){
14+
return new LaurelWordProducer();
15+
}
16+
}

0 commit comments

Comments
 (0)