Test RMI with JUnit

2

How to test a client / server application using RMI with JUnit? I've researched a myriad of places and found nothing that would help me.

    
asked by anonymous 28.10.2015 / 09:00

1 answer

0

You can create a RmiRegistry by java. I created a setupClass method with annotation @BeforeClass , in this method I created the Registry and started the server. This annotation means that the method will only execute when loading the class, that is, only once and before the tests run. In my case, I'm passing the Server as argument to the Client. I did it that way to ease the example. Once this is done, the methods can be tested normally.

I've created the following scenario:

Server:

public interface RemoteCalculator extends Remote {

    int somar(int valor1, int valor2) throws RemoteException;
    int subtrair(int valor1, int valor2) throws RemoteException;
    int multiplicar(int valor1, int valor2) throws RemoteException;
    int dividir(int valor1, int valor2) throws RemoteException;
}

Client:

public class Cliente {

    private RemoteCalculator calculator;

    protected Cliente(RemoteCalculator calculator) throws RemoteException {
        super();
        this.calculator = calculator;
    }

    public int calculate(int valor1, int valor2, Operador operador) throws RemoteException {
        switch(operador) {
            case SOMA:
                return calculator.somar(valor1, valor2);
            case SUBSTRACAO:
                return calculator.subtrair(valor1, valor2);
            case MULTIPLICACAO:
                return calculator.multiplicar(valor1, valor2);
            case DIVISAO:
                return calculator.dividir(valor1, valor2);
            default:
                throw new RemoteException("Argumento Inválido");
        }
    }
}

Your test would be:

public class IntegrationRmiTest {


    @BeforeClass
    public static void setupClass() throws RemoteException, AlreadyBoundException {
        CalculatorImpl calculator = new CalculatorImpl();
        RemoteCalculator stub = (RemoteCalculator) UnicastRemoteObject.exportObject(calculator, 0);

        Registry registry = LocateRegistry.createRegistry(36973);
        registry.bind("calculator", stub);
    }

    @Test
    public void somarTest() throws RemoteException, AlreadyBoundException, NotBoundException {
        Registry registry = LocateRegistry.getRegistry(36973);
        RemoteCalculator remoteCalculator = (RemoteCalculator) registry.lookup("calculator");
        Cliente cliente = new Cliente(remoteCalculator);
        Assert.assertEquals(12, cliente.calculate(5, 7, Operador.SOMA));
    }

    @Test
    public void subtrairTest() throws RemoteException, AlreadyBoundException, NotBoundException {
        Registry registry = LocateRegistry.getRegistry(36973);
        RemoteCalculator remoteCalculator = (RemoteCalculator) registry.lookup("calculator");
        Cliente cliente = new Cliente(remoteCalculator);
        Assert.assertEquals(-5, cliente.calculate(4, 9, Operador.SUBSTRACAO));
    }

    @Test
    public void multiplicarTest() throws RemoteException, AlreadyBoundException, NotBoundException {
        Registry registry = LocateRegistry.getRegistry(36973);
        RemoteCalculator remoteCalculator = (RemoteCalculator) registry.lookup("calculator");
        Cliente cliente = new Cliente(remoteCalculator);
        Assert.assertEquals(6, cliente.calculate(3, 2, Operador.MULTIPLICACAO));
    }

    @Test
    public void dividirTest() throws RemoteException, AlreadyBoundException, NotBoundException {
        Registry registry = LocateRegistry.getRegistry(36973);
        RemoteCalculator remoteCalculator = (RemoteCalculator) registry.lookup("calculator");
        Cliente cliente = new Cliente(remoteCalculator);
        Assert.assertEquals(2, cliente.calculate(8, 4, Operador.DIVISAO));
    }
}
    
30.11.2017 / 03:51