3

私はJUnitとユニットテストにまったく慣れていないので、コードのユニットテストを開始したいと思います。次のような方法から始めたいと思います。

    public Store loadStore(Integer customerId,
                Integer storeId){
        //The logic of this method selects a store from a database based on the parameter criteria
    }

この記事からhttp://www.vogella.com/articles/JUnit/article.html次のアサーションを作成できることを読みました。

  • fail(String)assertTrue(true)
  • assertTrue([メッセージ]、ブール条件)
  • assertsEquals([文字列メッセージ]、期待、実際)
  • assertsEquals([文字列メッセージ]、期待される、実際の、許容範囲)
  • assertNull([メッセージ]、オブジェクト)
  • assertNotNull([メッセージ]、オブジェクト)
  • assertSame([String]、expected、actual)
  • assertNotSame([String]、expected、actual)

このメソッドにどのアサーションを使用するかについて混乱しています。いくつか使用する必要がありますか?使用する必要がありますか?この方法の単体テストで何を証明しようとすべきでしょうか?

4

3 に答える 3

1

一般的な考え方は次のようになります。

Store store = loadStore(1, 1);
assertTrue("Test failed", store.somePropertyOrMethodOfYourStore==SomeValueYouExpect);

正直なところ、チュートリアルを行う必要がありますが、これは一般的な単体テストの非常に基本的なものです。

于 2013-02-05T22:18:41.853 に答える
1

メソッドがデータベースにアクセスしている場合は、本番データベースの代わりにインメモリデータベースを使用できます。また、@ Beforeおよび@Beforeメソッドでは、状態を初期化するか、状態をクリーンアップできます。

オブジェクトの周囲にコンテキストを作成するのが難しい場合は、MockitoやPowermockなどのフレームワークを使用してコンテキストを偽造し、パーツ(ユニット)のみをテストできます。

最初にチュートリアルを読むことをお勧めします。

于 2013-02-05T22:27:01.967 に答える
1
/**
 * This is a small example to get you started.
 * This is an example of a test class to test your loadStore
 * It assumes this method is defined in a class called StoreManager.
 * It encapsulates all the tests that are required within the same test method for
 * expediency sake. You generally should restrict one core functionality per test.
 * but if writing functional test, you can groupd core functionality together.
 * 
 * Provide you default input data. Provide data that should pass as well
 * as data that should fail or error according to all the possible conditions you can think off.
 * 
 * it is fine to hardcode the data when when starting out but later you need to look
 * into defining test data in some sort of reusable way as well as easy to maintain as data changes.
 * 
 * it is good practice not to run test against DB databases.
 * 
 * 
 */

import static org.junit.Assert.*;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
public class StoreManagerTest {

@BeforeClass
    public void before()
    {
    //in here do some pre-initilization for each test class if needed
    //database login if needed 
    //mock objects if needed etc
    }

@Before
public void beforeTest()
{
    //in here do some pre-initilization for each test if needed

}

@After
public void afterTest()
{
    //in here do some cleanup for each test if needed
}




@Test
public void testloadStore() {

        StoreManager storeManager = new StoreManager();
        Integer validCustomerId = new Integer(9876);
        Integer invalidCustomerId = new Integer(-10);

        Integer validStoreId = new Integer(2345);
        Integer invalidStoreId = new Integer(-345);

        String validCustomerName = new String("validname);

        String validStoreName = new String("validStoreName);

        //Test first with valid id.
        //expect to return a store. if store is undefined the assertTrue will fail.
        //if you get an error fail. need to invetigate why you are failing with valid store id and valid customer id.
        try{
            Store store = storeManager.loadStore(validCustomerId, validStoreId);
            assertNotNull(store);
            assertTrue(store.getStoreId().equals(validStoreId));
            assertTrue("customer id match : " , store.getCustomerId().equals(validCustomerId));
            //you can also check for other parameters that exist in store object
            assertSame("customer Name match :  ", store.getCustomerName(), validCustomerName);
            assertSame("Store Name match :  ", store.getStoreName(), validStoreName);
        }catch(Exception e){
            fail("testHashCode Failed! " + e.getMessage());
        }


        //Test  with valid id and invalid customerid
        //expect error or null store. if store is undefined the assertTrue will pass.
        //if you get an error fail perfectly valid to assume the test passed but you can write other test to test
        //whether the error condition is what was expected.
        try{
            Store store = storeManager.loadStore(invalidCustomerId, validStoreId);
            assertNull(store);
        }catch(Exception e){
            assertTrue("No Store matching :  "+invalidCustomerId, true);
        }

        //Test  with invalid  storeid and valid customerid
        //expect error or null store. if store is undefined the assertTrue will pass.
        //if you get an error fail perfectly valid to assume the test passed but you can write other test to test
        //whether the error condition is what was expected.
        try{
            Store store = storeManager.loadStore(validCustomerId, invalidStoreId);
            assertNull(store);
        }catch(Exception e){
            assertTrue(true);
        }


        //Test  with invalid  storeid and invalid customerid
        //expect error or null store. if store is undefined the assertTrue will pass.
        //if you get an error fail perfectly valid to assume the test passed but you can write other test to test
        //whether the error condition is what was expected.
        try{
            Store store = storeManager.loadStore(null, null);
            assertNull(store);
        }catch(Exception e){
            assertTrue(true);
        }


}

}
于 2013-02-06T02:29:39.480 に答える