Testing RMI with Junit

Asked

Viewed 73 times

2

How to test a client/server application using RMI with Junit? I’ve searched a plethora of places and found nothing to help me.

1 answer

0


You can create an Rmiregistry from java. I created a method setupClass with the Annotation @BeforeClass, In this method I created Registry and started the server. This annotation means that the method will run only on class loading, that is, only once and before the tests run. In my case, I am passing the Server as argument to the Client. I did so to facilitate the example. Once done, the methods can be tested normally.

I 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));
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.