Keep the strong reference to the MessengerService
package com.baeldung.rmi;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.fail;
+import org.junit.Before;
+import org.junit.Test;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
-import org.junit.BeforeClass;
-import org.junit.Test;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.fail;
public class JavaRMIIntegrationTest {
- @BeforeClass
- public static void whenRunServer_thenServerStarts() {
+ private MessengerServiceImpl messengerService;
+ @Before
+ public void init() {
try {
- MessengerServiceImpl server = new MessengerServiceImpl();
- server.createStubAndBind();
+ messengerService = new MessengerServiceImpl();
+ messengerService.createStubAndBind();
} catch (RemoteException e) {
fail("Exception Occurred: " + e);
}
@@ -26,7 +27,6 @@ public class JavaRMIIntegrationTest {
@Test
public void whenClientSendsMessageToServer_thenServerSendsResponseMessage() {
-
try {
Registry registry = LocateRegistry.getRegistry();
MessengerService server = (MessengerService) registry.lookup("MessengerService");
@@ -36,7 +36,7 @@ public class JavaRMIIntegrationTest {
assertEquals(responseMessage, expectedMessage);
} catch (RemoteException | NotBoundException e) {
fail("Exception Occurred: " + e);
- };
+ }
}
}
\ No newline at end of file
