2017-10-29 19 views
0

クラスMobileStorageはレトロな携帯電話の受信箱の実装です。それにより、受信ボックスは、メッセージ当たり最大160文字のメッセージの予め定められた最大容量を保持することが示される。以下の操作がサポートされており、テストする必要がある。EclipseのTestNGで単体テストを書く

  1. saveMessage:次の自由な位置にある受信トレイに新しいテキストメッセージを格納します。 メッセージテキストが160文字を超える場合、メッセージは分割され、複数の格納場所に格納されます。
  2. deleteMessage:受信トレイから最も古い(最初の)モバイルメッセージを削除します。
  3. listMessages:現在格納されているすべてのメッセージの読み取り可能な表現を表示します。複数の部分に格納されたメッセージは、結合されて表現されます。

私が付けたこのコードでユニットテストを行う必要があります。私はTestNGと単体テストに精通していませんが、私ができるテストの例を教えてください。

mobile_storage \ SRC \メイン\のJava \ MobileMessage.java - https://pastebin.com/RxNcgnSi

/** 
* Represents a mobile text message. 
*/ 
public class MobileMessage { 
    //stores the content of this messages 
    private final String text; 

    //in case of multi-part-messages, stores the preceding message 
    //is null in case of single message 
    private MobileMessage predecessor; 

    public MobileMessage(String text, MobileMessage predecessor) { 
     this.text = text; 
     this.predecessor = predecessor; 
    } 

    public String getText() { 
     return text; 
    } 

    public MobileMessage getPredecessor() { 
     return predecessor; 
    } 

    public void setPredecessor(MobileMessage predecessor) { 
     this.predecessor = predecessor; 
    } 
} 

mobile_storage \ SRC \メイン\のJava \ MobileStorage.java - https://pastebin.com/wuqKgvFD

import org.apache.commons.lang.StringUtils; 

import java.util.Arrays; 
import java.util.Objects; 
import java.util.stream.IntStream; 

/** 
* Represents the message inbox of a mobile phone. 
* Each storage position in the inbox can store a message with 160 characters at most. 
* Messages are stored with increasing order (oldest first). 
*/ 
public class MobileStorage { 

    final static int MAX_MESSAGE_LENGTH = 160; 

    private MobileMessage[] inbox; 
    private int occupied = 0; 

    /** 
    * Creates a message inbox that can store {@code storageSize} mobile messages. 
    * @throws IllegalArgumentException in case the passed {@code storageSize} is zero or less 
    */ 
    public MobileStorage(int storageSize) { 
     if(storageSize < 1) { 
      throw new IllegalArgumentException("Storage size must be greater than 0"); 
     } 

     this.inbox = new MobileMessage[storageSize]; 
    } 

    /** 
    * Stores a new text message to the inbox. 
    * In case the message text is longer than {@code MAX_MESSAGE_LENGTH}, the message is splitted and stored on multiple storage positions. 
    * @param message a non-empty message text 
    * @throws IllegalArgumentException in case the given message is empty 
    * @throws RuntimeException in case the available storage is too small for storing the complete message text 
    */ 
    public void saveMessage(String message) { 
     if(StringUtils.isBlank(message)) { 
      throw new IllegalArgumentException("Message cannot be null or empty"); 
     } 

     int requiredStorage = (int) Math.ceil((double) message.length()/MAX_MESSAGE_LENGTH); 

     if(requiredStorage > inbox.length || (inbox.length - occupied) <= requiredStorage) { 
      throw new RuntimeException("Storage Overflow"); 
     } 

     MobileMessage predecessor = null; 
     for(int i = 0; i < requiredStorage; i++) { 
      int from = i * MAX_MESSAGE_LENGTH; 
      int to = Math.min((i+1) * MAX_MESSAGE_LENGTH, message.length()); 

      String messagePart = message.substring(from, to); 
      MobileMessage mobileMessage = new MobileMessage(messagePart, predecessor); 
      inbox[occupied] = mobileMessage; 
      occupied++; 
      predecessor = mobileMessage; 
     } 
    } 

    /** 
    * Returns the number of currently stored mobile messages. 
    */ 
    public int getOccupied() { 
     return occupied; 
    } 

    /** 
    * Removes the oldest (first) mobile message from the inbox. 
    * 
    * @return the deleted message 
    * @throws RuntimeException in case there are currently no messages stored 
    */ 
    public String deleteMessage() { 
     if(occupied == 0) { 
      throw new RuntimeException("There are no messages in the inbox"); 
     } 

     MobileMessage first = inbox[0]; 

     IntStream.range(1, occupied).forEach(index -> inbox[index-1] = inbox[index]); 
     inbox[occupied] = null; 
     inbox[0].setPredecessor(null); 
     occupied--; 

     return first.getText(); 
    } 

    /** 
    * Returns a readable representation of all currently stored messages. 
    * Messages that were stored in multiple parts are joined together for representation. 
    * returns an empty String in case there are currently no messages stored 
    */ 
    public String listMessages() { 
     return Arrays.stream(inbox) 
       .filter(Objects::nonNull) 
       .collect(StringBuilder::new, MobileStorage::foldMessage, StringBuilder::append) 
       .toString(); 
    } 

    private static void foldMessage(StringBuilder builder, MobileMessage message) { 
     if (message.getPredecessor() == null && builder.length() != 0) { 
      builder.append('\n'); 
     } 
     builder.append(message.getText()); 
    } 
} 
+0

よう

あなたtest.java見えるかもしれ何かがあなたがテストケースまたはこれらの試験例TestNGの実装を探しているお役に立てば幸いですか! – user1207289

+0

私はテストケース、およびいくつかのテストケースのコーディング例を探しています。 – Allx

+0

@ user1207289お願いします。 – Allx

答えて

0

必要になりますtestNGをセットアップする必要があります。私がtestNGでテストする方法は、Eclipseとmaven(依存関係管理)です。一度それを持っている場合は、のファイルをsrcのmaven-Javaプロジェクト(eclipseの下)にインポートすることができます。

testNGのコードを調整し、必要なクラスをインポートする必要があります。 HereはtestNGの公式文書であり、hereassertクラスです。

私はいくつかのテストケースを含めることを試みました。これは、この

 import yourPackage.MobileStorage; 
     import yourPackage. MobileMessage; 

     public class test{ 

     @BeforeTest 
     public void prepareInstance(){ 

      MobileStorage mobStg = new MobileStorage(); 
      MobileMessage mobMsg = new MobileMessage(); 
     } 

     //test simple msg 
     @Test 
     public void testSave(){ 

      mobStg.saveMessage("hello") 
      assert.assertEquals("hello", mobMsg.getText()) 
     } 

     //test msg with more chars 
     @Test 
     public void testMsgMoreChar(){ 
      mobStg.saveMessage("messageWithMoreThan160Char") 

      //access messagepart here somehow, i am not sure of that 
       assert.assertEquals(mobMsg.getText(), mobStg.messagePart); 

      //access message here somehow. This will test listMessages() and concatenation of msgs 
      assert.assertEquals(mobStg.message, mobStg.listMessages()) 
     } 

//test deletion of msg 
@Test  
public void testDelete(){ 
      mobStg.deleteMessage(); 
       assert.assertEquals(null, mobMsg.getPredecessor()) 
     } 



    } 
関連する問題