0

私はJUnitテストに非常に慣れていないので、次の非常に単純なクラスをテストしようとしています。

public interface IItemsService extends IService {

    public final String NAME = "IItemsService";


    /** Places items into the database
     * @return 
     * @throws ItemNotStoredException 
    */
    public boolean storeItem(Items items) throws ItemNotStoredException;




    /** Retrieves items from the database
     * 
     * @param category
     * @param amount
     * @param color
     * @param type
     * @return
     * @throws ItemNotFoundException 
     */
    public Items getItems (String category, float amount, String color, String type) throws ItemNotFoundException;

}

これは私がテストのために持っているものですが、nullポインターを取得し続け、それに関する別のエラーが引数に適用されません...明らかに私は愚かなことをしていますが、それは表示されません。誰かが私を正しい方向に向けることができますか?

public class ItemsServiceTest extends TestCase {



    /**
     * @throws java.lang.Exception
     */

    private Items items;

    private IItemsService itemSrvc;

    protected void setUp() throws Exception {
        super.setUp();

        items = new Items ("red", 15, "pens", "gel");

    }

    IItemsService itemsService;
    @Test
    public void testStore() throws ItemNotStoredException {
            try {
                Assert.assertTrue(itemSrvc.storeItem(items));
            } catch (ItemNotStoredException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                System.out.println ("Item not stored");
            }
    }

    @Test
    public void testGet() throws ItemNotStoredException {
            try {
                Assert.assertFalse(itemSrvc.getItems(getName(), 0, getName(), getName()));
            } catch (ItemNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();

            }
    }

}
4

1 に答える 1

6

テスト対象のクラスのインスタンスを作成するのではなく、インターフェイスとして宣言するだけです。各テストで、テスト対象のクラスのインスタンスを作成し、そのメソッドの実装をテストする必要があります。また、テストは相互に依存してはならないことにも注意してください。それらが特定の順序で実行されていることに依存するべきではありません。テストのセットアップは、別のテストではなく、テストセットアップ方法で行う必要があります。

通常、テストではAAA(Arrange、Act、Assert)パターンを使用します。setUp(配置)とtearDown(アサート)はこれの一部にすることができますが、パターンは各テストメソッドにも反映される必要があります。

@Test
public void testStore() throws ItemNotStoredException {
    // Arrange
    ISomeDependency serviceDependency = // create a mock dependency
    IItemsService itemSvc = new ItemsService(someDependency);

    // Act
    bool result = itemSrvc.storeItem(items);

    // Assert
    Assert.assertTrue(result);
    // assert that your dependency was used properly if appropriate
}
于 2012-06-17T14:22:33.000 に答える