2016-10-04 9 views
0

私はjUnitテストの初心者です。テストする必要がある以下のコードがあります。 add(Object val)とObject at(int index)をテストしようとしています。 どうすればそれらについて行くのですか?jUnitテスト(java)を使用して - void add(Object val)およびObject(int index)をテストするにはどうすればよいですか?

public class ResizingArray 
{ 
    // declare the one stack. 
    private Object[] Stack;  
    private int length; //array length  
    private int n; //number of elements 

    public ResizingArray(int n) //Constructor 
    { 
     //Create a new stack with n elements of ints. 
     Stack = new Object[n]; 

     length = 0;  //no items yet  
    } 

     //return numbers of elements in stack. 
    public int length() 
    { 
     return length; 
    } 

     //Return the object at the indicated index of the array. 
    public Object at(int index) 
    { 
     if(index < length) 
     { 
     return Stack[index]; 
     } 
     else 
     { 
     throw new ArrayIndexOutOfBoundsException(); 
     } 
    } 

     //add an object at the next available index 
    public void add(Object val) 
    { 
     //increment length 
     if(length() < Stack.length) 
     { 
     length++; 
     } 
    } 

jUnit Test //This is the jUnit testing 

import org.junit.Assert; 
import static org.junit.Assert.*; 
import org.junit.Before; 
import org.junit.Test; 

public class ResizingArrayTest 
{ 
    protected ResizingArray newStack2; 

    public ResizingArrayTest() 
    {  
    } 

    atTest 
    public void testLength() 
    { 
     int[] newStack1 = new int[100]; //a new stack with 100 elements 
     //use the for loop to inialize 100 elements 
     for(int i = 0; i <= newStack1.length; ++i) 
     { 
     assertEquals((100),newStack1.length); //test for length. 
     newStack1.add(i+1);   //test for add 
     } 
    } 
} 
+0

この回答は役に立ちましたか?はいの場合は、助けになった答えを受け入れるように頼むことができますか? – notionquest

答えて

0

一般的に、我々は、ボイドのメソッドをテストするためにMockitoまたはPowerMockライブラリを使用することができます。ただし、add()とメソッドは以下のようにテストできます。 -

これは単なるサンプルテストです。必要に応じて、多くのシナリオをカバーするように拡張できます。

コンセプト: -

追加()からadd()メソッドが正常に実行された場合、長さがインクリメントされるべきです。長さの値をアサートしてください。

() - at()メソッドが正常に実行された場合は、オブジェクトを返す必要があります。したがって、オブジェクトの値をアサーションします。

コンストラクタもadd()も、オブジェクト変数をStack変数に割り当てません。したがって、at()は、インデックス外でない場合は常にnullを返します。

public class ResizingArrayTest { 


@Test 
public void addTestString() { 
    ResizingArray resizingArray = new ResizingArray(2); 

    resizingArray.add(new String("Str1")); 
    Assert.isTrue(resizingArray.length() == 1); 

    resizingArray.add(new String("Str2")); 
    Assert.isTrue(resizingArray.length() == 2); 

    //Max only 2 can be added as per the ResizingArray object creation 
    resizingArray.add(new String("Str3")); 
    Assert.isTrue(resizingArray.length() == 2); 

} 

@Test 
public void addTestDoesnotAddString() { 
    ResizingArray resizingArray = new ResizingArray(0); 

    //Nothing can be added 
    resizingArray.add(new String("Str1")); 
    Assert.isTrue(resizingArray.length() == 0);    

} 

@Test 
public void atTest() { 
    ResizingArray resizingArray = new ResizingArray(1); 

    resizingArray.add(new String("Str1")); 
    Assert.isNull(resizingArray.at(0)); 


} 

@Test(expected = ArrayIndexOutOfBoundsException.class) 
public void atTestThrowException() { 
    ResizingArray resizingArray = new ResizingArray(0); 

    resizingArray.at(1); 


} 


} 
0

オクラホマので、最初にすべての私は、あなたが実際には配列にオブジェクトを追加するようアドイン(Object)メソッドビットを変更すべきだと思う。第二に

//add an object at the next available index 
public void add(Object val) 
{ 
    //increment length 
    if(length() < Stack.length) 
    { 
    Stack[length] = val; 
    length++; 
    } 
} 

、あなたもチェックする必要があります渡されたインデックスが負でない値を取得するとき:

//Return the object at the indicated index of the array. 
public Object at(int index) 
{ 
    if(index >= 0 && index < length) 
    { 
     return Stack[index]; 
    } 
    else 
    { 
     throw new ArrayIndexOutOfBoundsException(); 
    } 
} 

次にテストに行きます。 私の方法をテストする方法は、考えられるシナリオごとに個別の単体テストを作成することです。テストコードが明確になり、維持しやすくなり、Single Responsibility Principleに準拠しています。

そこで彼は、私が実行しますテストのスイートです:

import static org.hamcrest.Matchers.*; 
import static org.hamcrest.MatcherAssert.assertThat; 

import org.junit.Before; 
import org.junit.Test; 

public class ResizingArrayTest { 

    private ResizingArray resizingArray; 
    private int defaultArraySize; 
    Integer valueToAddOne = Integer.valueOf(1); 
    Integer valueToAddTwo = Integer.valueOf(2); 
    Integer valueToAddThree = Integer.valueOf(3); 

    @Before 
    public void init(){ 
     defaultArraySize = 2; 
     resizingArray = new ResizingArray(defaultArraySize); 
    } 

    @Test 
    public void shouldAddValueToTheStack() throws Exception{ 
     // Act 
     resizingArray.add(valueToAddOne); 

     // Assert 
     assertThat("Array Length", resizingArray.length(), equalTo(1)); 
     assertThat("Top Value", (Integer)resizingArray.at(resizingArray.length()-1), equalTo(valueToAddOne)); 

     // Act 
     resizingArray.add(valueToAddTwo); 

     // Assert 
     assertThat("Array Length", resizingArray.length(), equalTo(2)); 
     assertThat("Top Value", (Integer)resizingArray.at(resizingArray.length()-1), equalTo(valueToAddTwo)); 
     } 

    @Test 
    public void shouldNotAddValueToTheStack() throws Exception{ 
     // Arrange 
     resizingArray.add(valueToAddOne); 
     resizingArray.add(valueToAddTwo); 

     // Act 
     resizingArray.add(valueToAddThree); 

     // Assert 
     assertThat("Top Value", (Integer) resizingArray.at(resizingArray.length() - 1), equalTo(valueToAddTwo)); 
} 



    @Test(expected = ArrayIndexOutOfBoundsException.class) 
    public void shouldThrowException_WhenRetrievingValueByNegativeIndex() throws Exception{ 
     // Act 
     resizingArray.at(-1); 
    } 

    @Test(expected = ArrayIndexOutOfBoundsException.class) 
    public void shouldThrowException_WhenRetrievingValueByIndexGreaterThanStackSize() throws Exception{ 
     // Act 
     resizingArray.at(3); 
} 

    @Test 
    public void shouldRetrieveValueByIndex() throws Exception{ 
     // Arrange 
     resizingArray.add(valueToAddOne); 
     resizingArray.add(valueToAddTwo); 

     // Act 
     Integer firstElement = (Integer) resizingArray.at(0); 
     Integer secondElement = (Integer) resizingArray.at(1); 

     // Assert 
     assertThat("First element", (Integer) resizingArray.at(0), equalTo(firstElement)); 
     assertThat("Second element", (Integer) resizingArray.at(1), equalTo(secondElement)); 

    } 

}

いくつかの重要なポイント:

  • @Before - このinitメソッドで私はすべてのテストがプロトタイプとして取るテスト中のオブジェクトの基本的なセットアップを記述します。
  • 各方法を「アレンジ/アクト/アサート」広告に分割すると、はっきりと明瞭になります。結局、すべてのテストケースにこのストラクチャが付いています。
  • 「_」のような珍しい文字であっても、テストに長い名前を選ぶのを恐れてはいけません。これもまた明瞭さを増す。あなたのテストのいくつかが失敗した場合..テストの名前を見るだけで間違ったことをすぐに知ることができます。
関連する問題