Hi all,
I have implemented a RMI Server and now I wanted to test if the method to start and stop this server are correcly implemented, using a JUnit test.
Here is the (simplified) code of my RMI server:
public void startServer() {
try {
...
registry = LocateRegistry.createRegistry(serverPort);
registry.rebind("foobar", myRemoteObject);
} catch (Exception e) {
e.printStackTrace();
}
}
public void stopServer() {
try {
registry.unbind("foobar");
} catch (Exception e) {
e.printStackTrace();
}
}
Now, I want to make a JUnit test to check if the startServer()
and stopServer()
are correctly working.
Thus, I've implemented this simple JUnit test:
private boolean isRunning(int port) {
boolean portTaken = false;
ServerSocket socket = null;
try {
socket = new ServerSocket(port);
// If we have succesfully created the ServerSocket, then it means that the port is available...
} catch (IOException e) {
portTaken = true;
if (socket != null) {
try {
socket.close();
} catch (IOException e2) {
}
}
}
return portTaken;
}
@Test
public void testStartAndStopServer() {
MyRMIServer server = new MyRMIServer();
assertNotNull(server);
int port = server.getServerPort();
assertTrue(port != -1);
// Check if the current server is NOT running.
assertFalse(isRunning(port));
// Start the server and then check if it is running.
server.startServer();
assertTrue(isRunning(port));
// Now stop the server...
server.stopServer();
assertFalse(isRunning(port));
}
My problem is that the last assert is not valid. If I print the stacktrace in the method isRunning()
, I get the error java.net.BindException: Address already in use: JVM_Bind
.
This means that the port is still used so I guess that my stopServer()
is not correct. What did I get wrong?
Note that if I use the registry.list()
method before and after calling my method stopServer()
, I see:
BEFORE: foobar
AFTER: (nothing)
Edit:
I've also modified my stopServer()
regarding a solution explained here:
public void stopServer() {
try {
registry.unbind("foobar");
UnicastRemoteObject.unexportObject(myRemoteObject, true);
} catch (Exception e) {
e.printStackTrace();
}
}
but it doesn't solve my problem...