Remote Method Call এর জন্য Test Case তৈরি

Testing এবং Debugging RMI Applications - জাভা আরএমআই (Java RMI) - Java Technologies

350

Java RMI (Remote Method Invocation) অ্যাপ্লিকেশনগুলোতে Remote Method Call এর কার্যকারিতা নিশ্চিত করতে Test Case তৈরি করা খুবই গুরুত্বপূর্ণ। নিচে RMI এর Remote Method Call এর জন্য একটি উদাহরণসহ Test Case এর স্টেপ-বাই-স্টেপ প্রক্রিয়া দেখানো হলো।


পরিকল্পনা: কী পরীক্ষা করা হবে?

১. Remote Method Call সফলভাবে কাজ করছে কিনা।

  • Remote method এর রিটার্ন ভ্যালু সঠিক কিনা।

২. Exception Handling সঠিকভাবে কাজ করছে কিনা।

  • RemoteException বা Custom Exception পরিচালনা।

৩. Server এবং Client এর মধ্যে সংযোগ ঠিকভাবে হচ্ছে কিনা।


RMI Remote Interface

import java.rmi.Remote;
import java.rmi.RemoteException;

public interface CalculatorService extends Remote {
    int add(int a, int b) throws RemoteException;
    int subtract(int a, int b) throws RemoteException;
}

RMI Server Implementation

import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

public class CalculatorServiceImpl extends UnicastRemoteObject implements CalculatorService {

    protected CalculatorServiceImpl() throws RemoteException {
        super();
    }

    @Override
    public int add(int a, int b) throws RemoteException {
        return a + b;
    }

    @Override
    public int subtract(int a, int b) throws RemoteException {
        return a - b;
    }
}

Server Main Class

import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

public class RMIServer {
    public static void main(String[] args) {
        try {
            CalculatorService service = new CalculatorServiceImpl();
            Registry registry = LocateRegistry.createRegistry(1099);
            registry.rebind("CalculatorService", service);
            System.out.println("RMI Server is running...");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Client Implementation

import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

public class RMIClient {
    public static void main(String[] args) {
        try {
            Registry registry = LocateRegistry.getRegistry("localhost", 1099);
            CalculatorService service = (CalculatorService) registry.lookup("CalculatorService");

            int result = service.add(10, 20);
            System.out.println("Addition Result: " + result);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

JUnit Test Case

১. Maven Dependency

Ensure you have the JUnit dependency in your pom.xml:

<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter</artifactId>
    <version>5.8.2</version>
    <scope>test</scope>
</dependency>

২. Test Class

import org.junit.jupiter.api.*;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

import static org.junit.jupiter.api.Assertions.*;

public class CalculatorServiceTest {

    private static Registry registry;
    private static CalculatorService service;

    @BeforeAll
    public static void setUp() {
        try {
            // Start RMI Registry
            registry = LocateRegistry.createRegistry(1099);

            // Bind the service
            CalculatorService calculatorService = new CalculatorServiceImpl();
            registry.rebind("CalculatorService", calculatorService);

            // Lookup the service
            service = (CalculatorService) registry.lookup("CalculatorService");
        } catch (Exception e) {
            fail("Setup failed: " + e.getMessage());
        }
    }

    @AfterAll
    public static void tearDown() {
        try {
            // Unbind the service
            registry.unbind("CalculatorService");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Test
    public void testAddMethod() {
        try {
            int result = service.add(10, 20);
            assertEquals(30, result, "Addition result is incorrect");
        } catch (Exception e) {
            fail("Exception occurred during add method test: " + e.getMessage());
        }
    }

    @Test
    public void testSubtractMethod() {
        try {
            int result = service.subtract(20, 10);
            assertEquals(10, result, "Subtraction result is incorrect");
        } catch (Exception e) {
            fail("Exception occurred during subtract method test: " + e.getMessage());
        }
    }

    @Test
    public void testRemoteException() {
        try {
            service = null; // Simulate null service
            assertThrows(RemoteException.class, () -> service.add(10, 20));
        } catch (Exception e) {
            fail("Unexpected exception: " + e.getMessage());
        }
    }
}

Test Execution

  1. Run Tests: Execute the test using:

    mvn test
    
  2. Expected Output:
    • Test cases for add and subtract methods should pass.
    • The test for RemoteException should handle the exception properly.

Key Considerations

  1. Set Up Properly:
    • Ensure RMI Registry is running before executing tests.
    • Properly bind and unbind services in the setup and teardown phases.
  2. Handle Exceptions:
    • Verify that exceptions (e.g., RemoteException) are handled correctly.
  3. Test Both Success and Failure Cases:
    • Test for valid inputs and edge cases.
    • Simulate failure scenarios (e.g., null service or network issues).
  4. Parallel Testing:
    • Use @TestInstance(TestInstance.Lifecycle.PER_CLASS) for parallel testing to avoid conflicts.

এই JUnit Test Cases ব্যবহার করে RMI Remote Method Call এর কার্যকারিতা এবং স্থায়িত্ব নিশ্চিত করা যায়। এটি ক্লায়েন্ট এবং সার্ভারের মধ্যে সঠিক যোগাযোগ পরীক্ষা করার পাশাপাশি Exception Handling এর কার্যকারিতা নিশ্চিত করে। এই টেস্ট কাঠামো বাস্তবায়ন করলে RMI অ্যাপ্লিকেশনের মান উন্নত করা যায়।

Content added By
Promotion

Are you sure to start over?

Loading...