Java Reference
In-Depth Information
WebSockets testing
We introduced the topic of WebSockets in the earlier chapters. Now let's see how we can
test them. To do this in plain Java, we will need a WebSocket client implementation; be
sure to add Tyrus to your pom.xml file:
<dependency>
<groupId>org.glassfish.tyrus.bundles</groupId>
<artifactId>tyrus-standalone-client</artifactId>
<scope>test</scope>
<version>1.8.3</version>
</dependency>
For this example, we will use Tyrus as a base code from Chapter 8 , Adding WebSockets .
Our test realizes a simple scenario. Using the REST API, we reserve a seat, and as a We-
bSocket client, we wait for a message broadcasting information concerning new reserva-
tions. Let's look at the code:
@RunAsClient
@RunWith(Arquillian.class)
public class TicketServiceTest {
private static final String WEBSOCKET_URL =
"ws://localhost:8080/ticket-agency-test-websockets/tickets";
private static final String SEAT_RESOURCE_URL =
"http://localhost:8080/ticket-agency-test-websockets/rest/
seat";
@Deployment
public static Archive<?> createTestArchive() { // [1]
return
ShrinkWrap.create(MavenImporter.class).loadPomFromFile("pom.xml")
.importBuildOutput().as(WebArchive.class);
}
@Test
public void shouldReceiveMessageOnBooking() throws
Exception {
// given
Search WWH ::




Custom Search