Guice Problem with Tests
Hey I wrote a LudoGame and now I like to test it with a little GuiceInjection^^ I have an Interface IDie for my die. Now for the game I only need an IDie instead of a realdie => in tests I simply give the LudoGame a MokeDie to set up t开发者_StackOverflow中文版he Numbers I like to roll. The IDie has only one method: roll() which returns a int. BUT the mokeDie now has another public method: sendNextNumber() (should be clear what this does^^) Now I like to @Inject a Die and if @UseMokeDie is before a Test I'll like to pass the MokeDie but I'm very new to Guice... Need some advices please! Thx for Answers
For tests, your best bet is to manually construct instances. For example:
public void testAdvance() {
MockDie die = new MockDie();
LudoGame game = new LudoGame(die);
die.sendNextNumber(5);
game.advance();
}
You can try:
Injector injector = Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
bind(IDie.class).toInstance(new MockDie());
}
});
System.out.println(injector.getInstance(IDie.class).getClass().toString());
精彩评论