tags:

views:

34

answers:

2

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
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
You should really still not be using Guice in plain unit tests - in some integration tests it is viable though.
gpampara
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