3

I read several tutorials and manuals but all of them skips the part that I actually need, which is the actual part where you run the stuff.

My scenario is as follows.

I have a Connection interface:

public interface Connection {
   void open(Selector selector);
   void send(NetMessage message);
}

I have a production implementation that needs a SocketFactory:

public class ConnectionImpl implements Connection {
    // members
    @Inject
    public ConnectionImpl(@Assisted SecurityMode securityMode, @Assisted long connectionId,
                          @Assisted EntityAddresses addresses, SocketFactory socketFactory)

So I created a ConnectionFactory:

public interface ConnectionFactory {
    SioConnection create(SecurityMode securityMode, long connectionId, EntityAddresses addresses);
}

Now, I have two implementations of SocketFactory : SocketFactoryProdand SocketFactoryTest .

I am creating a Test for Connection and I want to create a ConnectionImpl with SocketFactoryTest, and I really don't get how I do exactly. This is the part that I keep on missing, what I should write in my test (or in the test class setUp).

slashms
  • 928
  • 9
  • 26
  • 1
    Which class are you trying to unit-test? Could you please post the draft version of the unit-test? – Sergey Vyacheslavovich Brunov Aug 21 '16 at 16:22
  • @SergeyBrunov He didn't say it was a unit test. It's probably a functional or integration test, where he's trying to bind an alternate network. – Gene Aug 21 '16 at 16:28
  • I am trying to test Connection , but I would like to use a connectionFactory to create the connection I try to test and @inject it. So I am trying to understand what I should write (and where) the code that tells connectionsFactory (which is a member in the test class) that the SocketFactory to use is the SocketFactoryTest.class – slashms Aug 21 '16 at 16:46
  • @matansab, could you please post the source code of the test? – Sergey Vyacheslavovich Brunov Aug 24 '16 at 13:58

1 Answers1

2

You choose what to be bound to your interface in the Guice Module:

public class MyTest {

    @Inject
    private ConnectionFactory connectionFactory;

    @Before
    public void setUp() {
        Injector injector = Guice.createInjector(
                <your modules here>,
                new AbstractModule() {
                    @Override
                    protected void configure() {
                        install(new FactoryModuleBuilder()
                                .implement(Connection.class, ConnectionImpl.class)
                                .build(ConnectionFactory.class));
                        bind(SocketFactory.class).to(SocketFactoryTest.class);
                    }
                }
        );
        injector.injectMembers(this);
    }
}

If you want to override an existing binding of the SocketFactory from one of your production modules, you can do this:

public class MyTest {

    @Inject
    private ConnectionFactory connectionFactory;

    @Before
    public void setUp() {
        Injector injector = Guice.createInjector(
                Modules.override(
                    <your modules here. Somewhere the
                     FactoryModuleBuilder is installed here too>
                ).with(
                    new AbstractModule() {
                        @Override
                        protected void configure() {
                            bind(SocketFactory.class).to(SocketFactoryTest.class);
                        }
                    }
               )
        );
        injector.injectMembers(this);
    }
}
JadN
  • 71
  • 1
  • 5
  • I feel it is missing the point. I would expect to @Inject the ConnectionsFactory and not Connection. And that's what i'm missing. – slashms Aug 21 '16 at 16:44
  • Ah yes, apologies, I wrote this hastily. You should inject the ConnectionsFactory not the Connection. Fixed. – JadN Aug 22 '16 at 21:41