BAEL-4904: Keep the strong reference to the MessengerService (#10602)

Co-authored-by: Krzysztof Woyke <krzysztof.woyke.sp@lhsystems.com>
This commit is contained in:
kwoyke 2021-03-28 15:26:28 +02:00 committed by GitHub
parent 51bc24aa53
commit b7cbb5428d
1 changed files with 31 additions and 31 deletions

View File

@ -1,42 +1,42 @@
package com.baeldung.rmi; package com.baeldung.rmi;
import static org.junit.Assert.assertEquals; import org.junit.Before;
import static org.junit.Assert.fail; import org.junit.Test;
import java.rmi.NotBoundException; import java.rmi.NotBoundException;
import java.rmi.RemoteException; import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry; import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry; import java.rmi.registry.Registry;
import org.junit.BeforeClass; import static org.junit.Assert.assertEquals;
import org.junit.Test; import static org.junit.Assert.fail;
public class JavaRMIIntegrationTest { public class JavaRMIIntegrationTest {
@BeforeClass private MessengerServiceImpl messengerService;
public static void whenRunServer_thenServerStarts() {
@Before
try { public void init() {
MessengerServiceImpl server = new MessengerServiceImpl(); try {
server.createStubAndBind(); messengerService = new MessengerServiceImpl();
} catch (RemoteException e) { messengerService.createStubAndBind();
fail("Exception Occurred: " + e); } catch (RemoteException e) {
} fail("Exception Occurred: " + e);
} }
}
@Test
public void whenClientSendsMessageToServer_thenServerSendsResponseMessage() { @Test
public void whenClientSendsMessageToServer_thenServerSendsResponseMessage() {
try { try {
Registry registry = LocateRegistry.getRegistry(); Registry registry = LocateRegistry.getRegistry();
MessengerService server = (MessengerService) registry.lookup("MessengerService"); MessengerService server = (MessengerService) registry.lookup("MessengerService");
String responseMessage = server.sendMessage("Client Message"); String responseMessage = server.sendMessage("Client Message");
String expectedMessage = "Server Message"; String expectedMessage = "Server Message";
assertEquals(responseMessage, expectedMessage); assertEquals(responseMessage, expectedMessage);
} catch (RemoteException | NotBoundException e) { } catch (RemoteException | NotBoundException e) {
fail("Exception Occurred: " + e); fail("Exception Occurred: " + e);
}; }
} }
} }