2016-06-16 4 views
-1

のためのJUnitテストを実装する私は、SNMPサーバとSNMPクライアントのための非常に良い例が見つかりましたが、私は1つのテストファイルにJUnitテストを実装できるかどうかはわかりません。私は実行するとSNMPサーバとクライアント

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

答えて

0
@BeforeClassでアノテート方法でサーバを起動

。他のテストが呼び出される前に実行されます。

+0

私は同意するが、睡眠方法は()があるので、コードの実行がハングアップします。コードをどのように設計すれば、両方を実行することができますか? –

+0

スリープ方法? can not find ...私が考えたのは、 'v3.listen();'は、 "junit test session"を実行するスレッド(非ブロッキング)を生成し、クライアントがそのサーバに何かを送ることができるということです – oberbics

+0

はい、どうすればこの問題を解決できますか? –

1

あなたは、クライアントとは、単一のテスト内の別のスレッドとして、それらを開始すると考えることができ、同じテスト内で実行しているサーバーの両方を持っていると思った場合。

私は通常、それがテストにいくつかの複雑さとコンテキスト管理を追加しないため、これを避けるようにしてください。

ご注意:

  • をこのサンプルでは、​​テストされていない、なされる必要がある微調整があるかもしれません。追加のスレッドの処理の要点はほぼ正しいはずです。
  • 私はあなたのテストのために何かを検証していなかったので、これはすべて行い、出力または状態の無い期待して、サーバーとクライアントを実行しています。

    @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(); 
    } 
    
+0

私はトラップを待っている間に中断 '取得:java.lang.InterruptedException' –

+0

あなたは** **あなたが期待していた状態を取得していない場合は、(「trap.doTrap周りのコンテキストアウェアループを作成する必要があります) 'を実行します。 clientTaskから 'finally'ブロックを削除します。割り込みの呼び出しは、2つのスレッドを処理するための「最適なパス」の礼儀でしたが、errorCollectorではうまく動作しません(私はそれを逃しました - ごめんなさい)。 serverExecutorがデーモンとして設定されている限り、影響はありません。 – Jeremiah

+0

答えを更新してください。 –

関連する問題