Reputation: 755
I am writing a Client-Server system. And I want to write a Cucumber test to test whether the two interact correctly.
But if I put both my server and client into one test, when my server starts accepting connection, it would block and the test would not proceed. This is normal since the server should block when it is listening for connection.
Then how should I approach such a test?
My Cucumber feature is like this:
Feature: Client Can Connect.
Scenario: Client can connect to server.
Given Server is up and running.
When Client is up and tries to connect to server.
Then Server accepts connection.
My step definition for this is:
public class ClientConnectsServerStepDefinition {
@Given("^server is up and running$")
public void server_is_up_and_running() {
TheServer theServer = new TheServer("localhost", 5776);
theServer.start(); //The whole test would block at here and would not proceed
}
@When("^client is up and tries to connect to server$")
public void client_is_up_and_tries_to_connect() {
TheClient theClient = new TheClient("localhost", 5776);
theClient.start();
}
@Then("^server accepts connection.$")
public void the_server_accepts_connection() {
Assert.assertTrue(theServer.isConnected);
}
}
I once tried putting the server and client in separate threads. But this does not help since I cannot get any feedback from server or client since they are separated into two thread.
Do I write the test correctly? Please critic my code. Thanks for any strong hands.
Upvotes: 0
Views: 141
Reputation: 310884
Start the server in a separate thread. Make it a daemon thread so when the client stops everything stops, or have it exit when the (last) client disconnects.
Upvotes: 1