为SNMP服务器和客户端实施JUNIT测试



我找到了SNMP服务器和SNMP客户端的很好的示例,但我不确定如何将JUNIT测试实现到单个测试文件中:

public class SNMPClientTest
{
    @Test
    public void randomData()
    {
        SnmpTrap trap = new SnmpTrap("127.0.0.1",
            "1.3.6.1.4.1.2789.2005.1={s}WWW Server Has Been Restarted",
            2, "kschmidt", "MD5", "mysecretpass", "DES", "mypassphrase");
        trap.doTrap();
    }
}

public class SNMPServerTest
{
    @Test
    public void randomDatabaseData() throws SQLException, FileNotFoundException, IOException
    {
        V3TrapReceiver v3 = new V3TrapReceiver("127.0.0.1", "kschmidt", "MD5",
            "mysecretpass", "DES", "mypassphrase");
        v3.listen();
    }
}

运行服务器时,我会收到消息Waiting for traps..,并且无法继续Junit测试。但是我可以将它们运到2个单独的文件中。

我如何解决这个问题?您可以在此处找到完整的源代码:http://pastebin.com/zketxqmq

如果要同时在同一测试中运行客户端和服务器,则可以考虑在单个测试中启动它们为单独的线程。

我通常会尝试避免这种情况,因为它确实为测试增加了一些复杂性和上下文管理。

请注意:

  • 该样本尚未测试,可能需要进行调整。处理其他线程的要旨应大约正确。
  • 我没有为您的测试验证任何内容,因此所有这些都是运行服务器,然后是未期望输出或状态的客户端。

    @Rule
    public ErrorCollector collector = new ErrorCollector();
    @Rule
    public Timeout testTimeout = new Timeout(15, TimeUnit.SECONDS);
    @Test
    public void testServerClientCommunication throws Exception () {
        final SnmpTrap trap = new SnmpTrap("127.0.0.1",
                "1.3.6.1.4.1.2789.2005.1={s}WWW Server Has Been Restarted",
                2, "kschmidt", "MD5", "mysecretpass", "DES", "mypassphrase");
        final V3TrapReceiver v3 = new V3TrapReceiver("127.0.0.1", "kschmidt", "MD5",
                "mysecretpass", "DES", "mypassphrase");
        Runnable serverTask = new Runnable() {
            @Override
            public void run() {
                try {
                    while (!Thread.currentThread().isInterrupted()) {
                        v3.listen();
                    }
                } catch (Throwable th) {
                    //Exceptions thrown outside of the main Junit execution won't get propagated back to fail the test
                    //Use the ErrorCollector to maintain awareness
                    collector.addError(th);
                }
            }};
            //Create the Thread to handle the Server execution
            final Thread serverExecutor = new Thread(serverTask, "SNMP Server");
        /*
         * Create the client task and thread.
         */
        Runnable clientTask = new Runnable() {
            @Override
            public void run() {
                try {
                  boolean clientIsDone = false;
                  while (!clientIsDone) {
                        trap.doTrap();
                        //FIXME: Determine the state that matters.
                        clientIsDone = true;
                  }
                } catch (Throwable th) {
                    //Exceptions thrown outside of the main Junit execution won't get propagated back to fail the test
                    //Use the ErrorCollector to maintain awareness
                    collector.addError(th);
                } 
            }};
    
            Thread clientExecutor = new Thread(clientTask, "SNMP Client");
            /*
             * Start the server first
             */
            //Don't hold the JVM if the server is not done.
            serverExecutor.setDaemon(true); 
            serverExecutor.start();
                /*
                 * Now start the client.  Note that after the client traps successfully that it will interrupt the server thread.
                 * The intent is that the interrupt will allow the server thread to die gracefully
                 */
            clientExecutor.setDaemon(true);
            clientExecutor.start();
            //Since we off-threaded the tasks the test will consider itself 'done' unless we join with the client, which basically says
            //"Hold the current thread at this point until the other thread completes."
            clientExecutor.join();
    }
    

以@beforeclass注释的方法启动服务器。这将在调用其他测试之前运行。

最新更新