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 the 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
+1
A:
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();
}
Jesse Wilson
2010-05-07 15:43:41
mhm it's working now... I added the sendNextNumber() to the Interface with @ForTestingOnly and then created 2 Modules. 1 For the tests and 1 for the real game (or the SmokeTest)
D3orn
2010-05-07 18:35:04
You should really still not be using Guice in plain unit tests - in some integration tests it is viable though.
gpampara
2010-05-08 06:01:42
A:
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());
AlexV
2010-05-08 17:14:21