JUnit和RMI应用程序的最佳实践,RMI Registry

时间:2009-01-09 16:04:52

标签: java junit rmi rmiregistry

我正在为RMI客户端应用程序编写一组系统集成测试。我想在JUnit 3.8.2中启动RMI服务器,然后运行这些测试。有没有人这样做过?

我在使用main的pojo中使用类似的东西:


    import java.rmi.registry.*;

    ....

    //setup
    try {

        java.rmi.registry.LocateRegistry.createRegistry(1099);
        System.out.println("RMI registry ready.");

        this.StartMyServer();

        // client set up

        MyClient m = null;
        m = (MyClient) Naming.lookup("//MyHost/MyServer", m);

        // tests here

    } catch (MyClientException me) {

        System.out.println("MyClient fall down go boom");

    } catch (MyServerException me) {

        System.out.println("MyServer fall down go boom");

    } catch (Exception e) {

        System.out.println("Exception starting RMI registry:");

    } 


将此转换为junit测试的最佳方法是什么,因此注册表只启动一次,并且仅在注册表实际启动时才运行测试?我想避免让测试类使用一个测试方法,其中包含来自main的所有测试代码,这是我在来到这里之前发现的。

另外,在编写测试时应该注意什么?任何RMI测试都有问题吗?

1 个答案:

答案 0 :(得分:5)

如果您使用的是JDK 5或更高版本,我建议您升级到JUnit 4.4。注释是一个很好的帮助。

我还建议您阅读this。我不明白为什么你要编写一个单独的主类来运行测试。这不是TestRunner应该做的吗?

我会在设置中启动注册表,然后以拆解方式将其关闭。