Skip to content

Instantly share code, notes, and snippets.

@singun
Last active April 16, 2019 23:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save singun/0130e2e00fb31343bae27e1d5304e55e to your computer and use it in GitHub Desktop.
Save singun/0130e2e00fb31343bae27e1d5304e55e to your computer and use it in GitHub Desktop.
Spring framework testing context
@Service
public class HearingInterpreter {
private final WordProducer wordProducer;
HearingInterpreter(WordProducer wordProducer) {
this.wordProducer = wordProducer;
}
public String whatIHeard() {
String word = wordProducer.getWord();
System.out.println(word);
return word;
}
}
public class HearingInterpreterTest {
HearingInterpreter hearingInterpreter;
@Before
public void setUp() {
hearingInterpreter = new HearingInterpreter(new LaurelWordProducer());
}
@Test
public void whatIHeard() {
String word = hearingInterpreter.whatIHeard();
assertThat(word).isEqualTo("Laurel");
}
}
@Component
@Primary
public class LaurelWordProducer implements WordProducer {
@Override
public String getWord() {
return "Laurel";
}
}
public interface WordProducer {
String getWord();
}
@Component
public class YannyWordProducer implements WordProducer {
@Override
public String getWord() {
return "Yanny";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment