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我读到可以做出以下断言:

  • 失败(字符串) assertTrue(真)
  • assertTrue([消息],布尔条件)
  • assertsEquals([字符串消息],预期,实际)
  • assertsEquals([字符串消息],预期,实际,公差)
  • assertNull([消息],对象)
  • assertNotNull([消息],对象)
  • assertSame([String],预期,实际)
  • assertNotSame([字符串],预期,实际)

我对这个方法使用哪个断言感到困惑。我应该使用几个吗?我应该使用一个吗?应该用这种方法的单元测试来证明什么?

4

3 回答 3

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 回答
1

如果方法是访问数据库,您可以使用内存数据库而不是生产数据库。在@Before 和@Before 方法中,您可以初始化状态或清除状态。

如果难以围绕对象创建上下文,您可以使用 Mockito 或 Powermock 等框架来伪造上下文并仅测试部分(单元)。

我建议先阅读一些教程。

于 2013-02-05T22:27:01.967 回答
1

一般的想法可能是这样的:

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

虽然老实说你应该做一个教程,但这对于一般的单元测试来说是非常基本的东西。

于 2013-02-05T22:18:41.853 回答