### Question:
EngineIoSocket extends Emitter { void init(Transport transport, @SuppressWarnings("unused") HttpServletRequest initialRequest) { setTransport(transport); onOpen(); } EngineIoSocket(Object lockObject, String sid, EngineIoServer server, EngineIoSocketTimeoutHandler pingTimeoutHandler); @SuppressWarnings("WeakerAccess") String getId(); @SuppressWarnings("WeakerAccess") ReadyState getReadyState(); void send(Packet<?> packet); void close();  }### Answer:
@Test public void testInit() { final Transport transport = Mockito.spy(new StubTransport()); final EngineIoSocket socket = Mockito.spy(new EngineIoSocket(new Object(), ServerYeast.yeast(), new EngineIoServer(), mPingTimeoutHandler)); Mockito.doAnswer(invocationOnMock -> { final List<Packet<?>> packetList = invocationOnMock.getArgument(0); for (Packet<?> packet : packetList) { if (packet.type.equals(Packet.OPEN)) { final JSONObject jsonObject = new JSONObject((String) packet.data); Assert.assertTrue(jsonObject.has("sid")); Assert.assertEquals(socket.getId(), jsonObject.getString("sid")); } } return null; }).when(transport).send(Mockito.anyList()); socket.init(transport, null); Assert.assertEquals(SUB_TRANSPORT_NAME, socket.getCurrentTransportName()); Mockito.verify(socket, Mockito.times(1)) .emit(Mockito.eq("open")); Mockito.verify(socket, Mockito.times(1)) .emit(Mockito.eq("flush"), Mockito.any()); Mockito.verify(socket, Mockito.times(1)) .emit(Mockito.eq("drain")); Mockito.verify(transport, Mockito.times(1)) .send(Mockito.anyList()); }
@Test public void testTransportPacket() { final Transport transport = Mockito.spy(new StubTransport()); final EngineIoSocket socket = new EngineIoSocket(new Object(), ServerYeast.yeast(), new EngineIoServer(), mPingTimeoutHandler); socket.init(transport, null); final Packet<String> packet = new Packet<>(Packet.NOOP); final Emitter.Listener packetListener = Mockito.mock(Emitter.Listener.class); socket.on("packet", packetListener); transport.emit("packet", packet); Mockito.verify(packetListener, Mockito.times(1)) .call(Mockito.eq(packet)); }
@Test public void testTransportPacket_message() { final Transport transport = Mockito.spy(new StubTransport()); final EngineIoSocket socket = new EngineIoSocket(new Object(), ServerYeast.yeast(), new EngineIoServer(), mPingTimeoutHandler); socket.init(transport, null); final String packetData = "TestMessage"; final Packet<String> packet = new Packet<>(Packet.MESSAGE, packetData); final Emitter.Listener messageListener = Mockito.mock(Emitter.Listener.class); Mockito.doAnswer(invocation -> { final Object[] args = invocation.getArguments(); Assert.assertEquals(1, args.length); Assert.assertEquals(packetData, args[0]); return null; }).when(messageListener).call(Mockito.any()); socket.on("message", messageListener); socket.on("data", messageListener); transport.emit("packet", packet); Mockito.verify(messageListener, Mockito.times(2)) .call(Mockito.eq(packetData)); }