Skip to content

Instantly share code, notes, and snippets.

@mikeycmccarthy
Created May 20, 2012 10:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikeycmccarthy/5db35e0d6357728bf415 to your computer and use it in GitHub Desktop.
Save mikeycmccarthy/5db35e0d6357728bf415 to your computer and use it in GitHub Desktop.
Testing components with Mockito
@Mock
private ComponentManager componentManager;
@Mock
private HostDetails hostDetails;
@InjectMocks
private HostInfoComponent testObject = new HostInfoComponent();
@Before
public void setUp() throws Exception {
testObject.initialize(COMPONENT_JID, componentManager);
}
@Test
public void testComponentAutomaticallySendsOutSubscriptionToRosterRequests() throws Exception {
Presence expectedSubscriptionPresence = new Presence(Presence.Type.subscribe);
expectedSubscriptionPresence.setTo(HOST_JID);
expectedSubscriptionPresence.setFrom(COMPONENT_JID);
Presence subscribePresence = new Presence(Presence.Type.subscribe);
subscribePresence.setFrom(HOST_JID);
testObject.handlePresence(subscribePresence);
verify(componentManager).sendPacket(any(Component.class), argThat(equalTo(expectedSubscriptionPresence)));
}
@Test
public void badRequestReturnedWhenRoomTypeNotAValidType() throws Exception {
testObject.handlePresence(createInitialRoomJoinPresenceWithInvalidType());
Packet errorPacket = new Presence();
errorPacket.setError(PacketError.Condition.bad_request);
ArgumentCaptor<Packet> argument = ArgumentCaptor.forClass(Packet.class);
verify(componentManager).sendPacket(Matchers.<Component>anyObject(), argument.capture());
Packet packetToSend = argument.getValue();
assertEquals(PacketError.Condition.bad_request, packetToSend.getError().getCondition());
assertEquals(COMPONENT_JID, packetToSend.getFrom());
assertEquals(FROM_USER, packetToSend.getTo());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment