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;
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() {
try {
MessengerServiceImpl server = new MessengerServiceImpl();
server.createStubAndBind();
} catch (RemoteException e) {
fail("Exception Occurred: " + e);
}
}
@Test
public void whenClientSendsMessageToServer_thenServerSendsResponseMessage() {
try {
Registry registry = LocateRegistry.getRegistry();
MessengerService server = (MessengerService) registry.lookup("MessengerService");
String responseMessage = server.sendMessage("Client Message");
String expectedMessage = "Server Message";
assertEquals(responseMessage, expectedMessage);
} catch (RemoteException | NotBoundException e) {
fail("Exception Occurred: " + e);
};
}
private MessengerServiceImpl messengerService;
@Before
public void init() {
try {
messengerService = new MessengerServiceImpl();
messengerService.createStubAndBind();
} catch (RemoteException e) {
fail("Exception Occurred: " + e);
}
}
@Test
public void whenClientSendsMessageToServer_thenServerSendsResponseMessage() {
try {
Registry registry = LocateRegistry.getRegistry();
MessengerService server = (MessengerService) registry.lookup("MessengerService");
String responseMessage = server.sendMessage("Client Message");
String expectedMessage = "Server Message";
assertEquals(responseMessage, expectedMessage);
} catch (RemoteException | NotBoundException e) {
fail("Exception Occurred: " + e);
}
}
}